将 util.promisify 与 exec 一起使用未解决 git 挂钩中的承诺
Using util.promisify with exec is not resolving promise in git hook
根据 docs,您可以通过执行以下操作来 promisify NodeJS' require('child_process').exec
:
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function lsExample() {
const { stdout, stderr } = await exec('ls');
console.log('stdout:', stdout);
console.error('stderr:', stderr);
}
lsExample();
然而,当我在 'prepare-commit-msg' git 钩子中使用这种方法来获取当前分支名称时,stdout
常量是一个 promise 对象,而不是分支名称值,因为我期待。我错过了什么吗?
const util = require('util');
const stdExec = require('child_process').exec;
const exec = util.promisify(stdExec);
async function getBranchName() {
const { err, stdout, stderr } = await exec('git rev-parse --abbrev-ref HEAD');
console.log(err, stdout, stderr); // Returns correct stdout
if (err) {
// "git branch" will fail if initial commit hasn't been done, in that case, skip this hook
process.exit(0);
}
return stdout;
}
function validateBranchName(name) {
console.log(name);
)
// Call get branchName
if(condition) {
const branchName = getBranchName(); // branchName is promise object
validateBranchName(branchName);
}
我通过将调用函数的位置重构为另一个异步函数来修复此问题:
const util = require('util');
const stdExec = require('child_process').exec;
const exec = util.promisify(stdExec);
async function getBranchName() {
const { err, stdout, stderr } = await exec('git rev-parse --abbrev-ref HEAD');
console.log(err, stdout, stderr);
if (err) {
// "git branch" will fail if initial commit hasn't been done, in that case, skip this hook
process.exit(0);
}
return stdout;
}
function validateBranchName(name) {
console.log(name);
)
function async prepareCommitMsg() {
const branchName = await getBranchName();
validateBranchName(branchName);
}
// Call get branchName
if(condition) {
prepareCommitMsg().then(() => {console.log('DONE!')});
}
根据 docs,您可以通过执行以下操作来 promisify NodeJS' require('child_process').exec
:
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function lsExample() {
const { stdout, stderr } = await exec('ls');
console.log('stdout:', stdout);
console.error('stderr:', stderr);
}
lsExample();
然而,当我在 'prepare-commit-msg' git 钩子中使用这种方法来获取当前分支名称时,stdout
常量是一个 promise 对象,而不是分支名称值,因为我期待。我错过了什么吗?
const util = require('util');
const stdExec = require('child_process').exec;
const exec = util.promisify(stdExec);
async function getBranchName() {
const { err, stdout, stderr } = await exec('git rev-parse --abbrev-ref HEAD');
console.log(err, stdout, stderr); // Returns correct stdout
if (err) {
// "git branch" will fail if initial commit hasn't been done, in that case, skip this hook
process.exit(0);
}
return stdout;
}
function validateBranchName(name) {
console.log(name);
)
// Call get branchName
if(condition) {
const branchName = getBranchName(); // branchName is promise object
validateBranchName(branchName);
}
我通过将调用函数的位置重构为另一个异步函数来修复此问题:
const util = require('util');
const stdExec = require('child_process').exec;
const exec = util.promisify(stdExec);
async function getBranchName() {
const { err, stdout, stderr } = await exec('git rev-parse --abbrev-ref HEAD');
console.log(err, stdout, stderr);
if (err) {
// "git branch" will fail if initial commit hasn't been done, in that case, skip this hook
process.exit(0);
}
return stdout;
}
function validateBranchName(name) {
console.log(name);
)
function async prepareCommitMsg() {
const branchName = await getBranchName();
validateBranchName(branchName);
}
// Call get branchName
if(condition) {
prepareCommitMsg().then(() => {console.log('DONE!')});
}