Antfish
Antfish

Reputation: 1493

Using util.promisify with exec is not resolving promise in git hook

According to the docs you can promisify NodeJS' require('child_process').exec by doing the following:

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();

However when I use this approach in a 'prepare-commit-msg' git hook to get the current branch name, the stdout constant is a promise object, not the branch name value as I'd expect. Am I missing something?

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);
}

Upvotes: 2

Views: 2328

Answers (1)

Antfish
Antfish

Reputation: 1493

I fixed this by refactoring where the function was being called into another async function:

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!')});
}

Upvotes: 0

Related Questions