match-tasks.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @module match-tasks
  3. * @author Toru Nagashima
  4. * @copyright 2015 Toru Nagashima. All rights reserved.
  5. * See LICENSE file in root directory for full license.
  6. */
  7. "use strict"
  8. //------------------------------------------------------------------------------
  9. // Requirements
  10. //------------------------------------------------------------------------------
  11. const Minimatch = require("minimatch").Minimatch
  12. //------------------------------------------------------------------------------
  13. // Helpers
  14. //------------------------------------------------------------------------------
  15. const COLON_OR_SLASH = /[:/]/g
  16. const CONVERT_MAP = { ":": "/", "/": ":" }
  17. /**
  18. * Swaps ":" and "/", in order to use ":" as the separator in minimatch.
  19. *
  20. * @param {string} s - A text to swap.
  21. * @returns {string} The text which was swapped.
  22. */
  23. function swapColonAndSlash(s) {
  24. return s.replace(COLON_OR_SLASH, (matched) => CONVERT_MAP[matched])
  25. }
  26. /**
  27. * Creates a filter from user-specified pattern text.
  28. *
  29. * The task name is the part until the first space.
  30. * The rest part is the arguments for this task.
  31. *
  32. * @param {string} pattern - A pattern to create filter.
  33. * @returns {{match: function, task: string, args: string}} The filter object of the pattern.
  34. */
  35. function createFilter(pattern) {
  36. const trimmed = pattern.trim()
  37. const spacePos = trimmed.indexOf(" ")
  38. const task = spacePos < 0 ? trimmed : trimmed.slice(0, spacePos)
  39. const args = spacePos < 0 ? "" : trimmed.slice(spacePos)
  40. const matcher = new Minimatch(swapColonAndSlash(task), { nonegate: true })
  41. const match = matcher.match.bind(matcher)
  42. return { match, task, args }
  43. }
  44. /**
  45. * The set to remove overlapped task.
  46. */
  47. class TaskSet {
  48. /**
  49. * Creates a instance.
  50. */
  51. constructor() {
  52. this.result = []
  53. this.sourceMap = Object.create(null)
  54. }
  55. /**
  56. * Adds a command (a pattern) into this set if it's not overlapped.
  57. * "Overlapped" is meaning that the command was added from a different source.
  58. *
  59. * @param {string} command - A pattern text to add.
  60. * @param {string} source - A task name to check.
  61. * @returns {void}
  62. */
  63. add(command, source) {
  64. const sourceList = this.sourceMap[command] || (this.sourceMap[command] = [])
  65. if (sourceList.length === 0 || sourceList.indexOf(source) !== -1) {
  66. this.result.push(command)
  67. }
  68. sourceList.push(source)
  69. }
  70. }
  71. //------------------------------------------------------------------------------
  72. // Public Interface
  73. //------------------------------------------------------------------------------
  74. /**
  75. * Enumerates tasks which matches with given patterns.
  76. *
  77. * @param {string[]} taskList - A list of actual task names.
  78. * @param {string[]} patterns - Pattern texts to match.
  79. * @returns {string[]} Tasks which matches with the patterns.
  80. * @private
  81. */
  82. module.exports = function matchTasks(taskList, patterns) {
  83. const filters = patterns.map(createFilter)
  84. const candidates = taskList.map(swapColonAndSlash)
  85. const taskSet = new TaskSet()
  86. const unknownSet = Object.create(null)
  87. // Take tasks while keep the order of patterns.
  88. for (const filter of filters) {
  89. let found = false
  90. for (const candidate of candidates) {
  91. if (filter.match(candidate)) {
  92. found = true
  93. taskSet.add(
  94. swapColonAndSlash(candidate) + filter.args,
  95. filter.task
  96. )
  97. }
  98. }
  99. // Built-in tasks should be allowed.
  100. if (!found && (filter.task === "restart" || filter.task === "env")) {
  101. taskSet.add(filter.task + filter.args, filter.task)
  102. found = true
  103. }
  104. if (!found) {
  105. unknownSet[filter.task] = true
  106. }
  107. }
  108. const unknownTasks = Object.keys(unknownSet)
  109. if (unknownTasks.length > 0) {
  110. throw new Error(`Task not found: "${unknownTasks.join("\", ")}"`)
  111. }
  112. return taskSet.result
  113. }