ProjectHelper.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* eslint-disable
  2. handle-callback-err,
  3. max-len,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS101: Remove unnecessary use of Array.from
  10. * DS102: Remove unnecessary code created because of implicit returns
  11. * DS207: Consider shorter variations of null checks
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. let ProjectHelper
  15. const ENGINE_TO_COMPILER_MAP = {
  16. latex_dvipdf: 'latex',
  17. pdflatex: 'pdflatex',
  18. xelatex: 'xelatex',
  19. lualatex: 'lualatex'
  20. }
  21. module.exports = ProjectHelper = {
  22. compilerFromV1Engine(engine) {
  23. return ENGINE_TO_COMPILER_MAP[engine]
  24. },
  25. ensureNameIsUnique(nameList, name, suffixes, maxLength, callback) {
  26. // create a set of all project names
  27. if (suffixes == null) {
  28. suffixes = []
  29. }
  30. if (callback == null) {
  31. callback = function(error, name, changed) {}
  32. }
  33. const allNames = new Set(nameList)
  34. const isUnique = x => !allNames.has(x)
  35. // check if the supplied name is already unique
  36. if (isUnique(name)) {
  37. return callback(null, name, false)
  38. }
  39. // the name already exists, try adding the user-supplied suffixes to generate a unique name
  40. for (let suffix of Array.from(suffixes)) {
  41. const candidateName = ProjectHelper._addSuffixToProjectName(
  42. name,
  43. suffix,
  44. maxLength
  45. )
  46. if (isUnique(candidateName)) {
  47. return callback(null, candidateName, true)
  48. }
  49. }
  50. // if there are no (more) suffixes, use a numeric one
  51. const uniqueName = ProjectHelper._addNumericSuffixToProjectName(
  52. name,
  53. allNames,
  54. maxLength
  55. )
  56. if (uniqueName != null) {
  57. return callback(null, uniqueName, true)
  58. } else {
  59. return callback(
  60. new Error(`Failed to generate a unique name for: ${name}`)
  61. )
  62. }
  63. },
  64. _addSuffixToProjectName(name, suffix, maxLength) {
  65. // append the suffix and truncate the project title if needed
  66. if (suffix == null) {
  67. suffix = ''
  68. }
  69. const truncatedLength = maxLength - suffix.length
  70. return name.substr(0, truncatedLength) + suffix
  71. },
  72. _addNumericSuffixToProjectName(name, allProjectNames, maxLength) {
  73. const NUMERIC_SUFFIX_MATCH = / \((\d+)\)$/
  74. const suffixedName = function(basename, number) {
  75. const suffix = ` (${number})`
  76. return basename.substr(0, maxLength - suffix.length) + suffix
  77. }
  78. const match = name.match(NUMERIC_SUFFIX_MATCH)
  79. let basename = name
  80. let n = 1
  81. const last = allProjectNames.size + n
  82. if (match != null) {
  83. basename = name.replace(NUMERIC_SUFFIX_MATCH, '')
  84. n = parseInt(match[1])
  85. }
  86. while (n <= last) {
  87. const candidate = suffixedName(basename, n)
  88. if (!allProjectNames.has(candidate)) {
  89. return candidate
  90. }
  91. n += 1
  92. }
  93. return null
  94. }
  95. }