现在在package.json
中我设置了"precommit": "lint node internal/scripts/precommit"
,我想获取所有提交的文件路径我的precommit.js,有什么办法解决这个问题吗?
最佳答案
从“Git pre-commit hook : changed/added files ”开始,实际的 Git 命令是:
git diff --cached --name-only --diff-filter=ACM -z | xargs -0 ...
您可以看到来自 templates/hooks--pre-commit.sample
的一种可能用法.
它实际上也用于 JavaScript Standard Style GitHub 存储库。
最后,请使用spawnSync
,如“Catching Fatal Errors with Node.js child_process
”
The best way to catch errors without letting your app brick is by using the process'
spawn
(or in this casespawnSync
) method:
var childProcess = require('child_process');
var commitMessage = (function() {
var spawn = childProcess.spawnSync('git', ['log', '--format=%B', '-n', '1']);
var errorText = spawn.stderr.toString().trim();
if (errorText) {
console.log('Fatal error from `git log`. You must have one commit before deploying.');
throw new Error(errorText);
}
else {
return spawn.stdout.toString().trim();
}
})();
With this method you can check the stderr buffer first; if there's a String from it you know it errored out, if no error text then the process was clean!
根据您的钩子(Hook)情况调整此方法,并按照上面建议的命令更改其 Git 命令。
关于javascript - 在package.json中执行precommit hook时如何获取文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48136097/