diff --git a/run-if-changed.js b/run-if-changed.js index a47ccc2..563248d 100755 --- a/run-if-changed.js +++ b/run-if-changed.js @@ -7,4 +7,7 @@ if (changedFiles.length === 0) { process.exit(0); } -require('./src/runForMatchingPatterns')(changedFiles, config); +const resolveMatchingPatterns = require('./src/resolveMatchingPatterns'); +const runCommands = require('./src/runCommands'); + +runCommands(resolveMatchingPatterns(changedFiles, config)); diff --git a/src/findBinary.js b/src/findBinary.js index b5dba09..03a4cfd 100644 --- a/src/findBinary.js +++ b/src/findBinary.js @@ -1,3 +1,5 @@ +// This code is adapted from https://git.io/fhsKi + const parseStringArgv = require('string-argv'); const which = require('npm-which')(process.cwd()); diff --git a/src/resolveMatchingPatterns.js b/src/resolveMatchingPatterns.js new file mode 100644 index 0000000..e5b451d --- /dev/null +++ b/src/resolveMatchingPatterns.js @@ -0,0 +1,13 @@ +const matcher = require('./matcher'); + +module.exports = function resolveMatchingPatterns(list, config) { + const commandsToRun = Object.entries(config) + .filter(([pattern]) => matcher(list, [pattern])) + .map(([, commands]) => commands) + // Flatten array + .reduce((acc, val) => acc.concat(val), []); + + // unique commands, do not run them multiple times if they are the same + // for multiple patterns or they are repeated in a list for a single pattern + return [...new Set(commandsToRun)]; +}; diff --git a/src/runForMatchingPatterns.js b/src/runForMatchingPatterns.js deleted file mode 100644 index f2dc921..0000000 --- a/src/runForMatchingPatterns.js +++ /dev/null @@ -1,8 +0,0 @@ -const matcher = require('./matcher'); -const runCommands = require('./runCommands'); - -module.exports = function runForMatchingPatterns(files, config) { - Object.entries(config) - .filter(([pattern]) => matcher(files, [pattern])) - .forEach(([, commands]) => runCommands(commands)); -};