gpt4 book ai didi

javascript - 在 package.json 中执行 precommit hook 时如何获取文件路径?

转载 作者:行者123 更新时间:2023-11-29 20:58:14 25 4
gpt4 key购买 nike

现在在 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 repo 。

最后,请使用 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 case spawnSync) 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com