Skip to content

Commit 88f962a

Browse files
authored
Merge pull request #7 from hkdobrev/micromatch
Use micromatcher for pattern matching
2 parents 9382462 + 32c73b4 commit 88f962a

File tree

10 files changed

+72
-54
lines changed

10 files changed

+72
-54
lines changed

bin/git-run-if-changed.sh

Lines changed: 0 additions & 19 deletions
This file was deleted.

index.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

package.json

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
"name": "@hkdobrev/run-if-changed",
33
"version": "0.2.4",
44
"description": "Run a command if a file changes via Git hooks",
5-
"bin": "index.js",
6-
"main": "index.js",
5+
"bin": "run-if-changed.js",
6+
"main": "src/runForMatchingPatterns",
77
"repository": "https://github.com/hkdobrev/run-if-changed",
88
"author": "Haralan Dobrev <hkdobrev@gmail.com>",
99
"license": "MIT",
@@ -12,6 +12,7 @@
1212
"commander": "^2.19.0",
1313
"cosmiconfig": "^5.0.7",
1414
"execa": "^1.0.0",
15+
"micromatch": "^3.1.10",
1516
"npm-which": "^3.0.1",
1617
"string-argv": "^0.1.1"
1718
},
@@ -26,15 +27,16 @@
2627
"test": "eslint .",
2728
"test-ci": "yarn test",
2829
"preversion": "yarn test",
29-
"postversion": "git push --tags && yarn publish . --new-version $npm_package_version && git push && echo \"Successfully released version $npm_package_version!\""
30+
"postversion": "git push --tags && yarn publish . --new-version $npm_package_version && git push && echo \"Successfully released version $npm_package_version!\"",
31+
"run-if-changed": "./run-if-changed.js"
3032
},
3133
"husky": {
3234
"hooks": {
3335
"pre-commit": "lint-staged",
34-
"post-commit": "./index.js",
35-
"post-checkout": "./index.js",
36-
"post-merge": "./index.js",
37-
"post-rewrite": "./index.js"
36+
"post-commit": "yarn run-if-changed",
37+
"post-checkout": "yarn run-if-changed",
38+
"post-merge": "yarn run-if-changed",
39+
"post-rewrite": "yarn run-if-changed"
3840
}
3941
},
4042
"run-if-changed": {
@@ -49,9 +51,6 @@
4951
]
5052
},
5153
"eslintConfig": {
52-
"extends": "airbnb-base",
53-
"rules": {
54-
"no-console": "off"
55-
}
54+
"extends": "airbnb-base"
5655
}
5756
}

run-if-changed.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env node
2+
3+
require('./src/cliInit')();
4+
5+
const config = require('./src/configLoad')();
6+
const changedFiles = require('./src/gitChangedFilesSinceLastHead')();
7+
8+
if (changedFiles.length === 0) {
9+
process.exit(0);
10+
}
11+
12+
require('./src/runForMatchingPatterns')(changedFiles, config);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const execa = require('execa');
2+
3+
function getFilesFromGit() {
4+
const { stdout } = execa.sync('git', [
5+
'diff-tree',
6+
'--name-only',
7+
'--no-commit-id',
8+
'HEAD@{1}',
9+
'HEAD',
10+
]);
11+
12+
return stdout;
13+
}
14+
15+
module.exports = function gitChangedFilesSinceLastHead() {
16+
const changedFiles = getFilesFromGit();
17+
18+
return changedFiles.split('\n').filter(file => !!file);
19+
};

src/matcher.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const micromatch = require('micromatch');
2+
3+
module.exports = function matcher(changedFiles, patterns) {
4+
return micromatch.some(changedFiles, patterns);
5+
};

src/runAll.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/runCommands.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const execa = require('execa');
2+
const findBinary = require('./findBinary');
3+
4+
function runBinary(command) {
5+
const { binaryPath, args } = command;
6+
const result = execa(binaryPath, args);
7+
result.stdout.pipe(process.stdout);
8+
result.stderr.pipe(process.stderr);
9+
}
10+
11+
function normaliseCommands(commands) {
12+
return Array.isArray(commands) ? commands : [commands].filter(x => !!x);
13+
}
14+
15+
module.exports = function runCommands(commands) {
16+
normaliseCommands(commands).map(findBinary).forEach(runBinary);
17+
};

src/runForMatchingPatterns.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const matcher = require('./matcher');
2+
const runCommands = require('./runCommands');
3+
4+
module.exports = function runForMatchingPatterns(files, config) {
5+
Object.entries(config)
6+
.filter(([pattern]) => matcher(files, [pattern]))
7+
.forEach(([, commands]) => runCommands(commands));
8+
};

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ matcher@^1.0.0:
15081508
dependencies:
15091509
escape-string-regexp "^1.0.4"
15101510

1511-
micromatch@^3.1.8:
1511+
micromatch@^3.1.10, micromatch@^3.1.8:
15121512
version "3.1.10"
15131513
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-3.1.10.tgz#70859bc95c9840952f359a068a3fc49f9ecfac23"
15141514
integrity sha512-MWikgl9n9M3w+bpsY3He8L+w9eF9338xRl8IAO5viDizwSzziFEyUzo2xrrloB64ADbTf8uA8vRqqttDTOmccg==

0 commit comments

Comments
 (0)