Inquirer.js icon indicating copy to clipboard operation
Inquirer.js copied to clipboard

Is there a way to get this to work with a pre-commit hook?

Open stretch0 opened this issue 3 years ago • 2 comments

I have a simple pre-commit hook that asks the user for the semantic version of their commit:

'use strict';
const { exec } = require('child_process');
const { promisify } = require('util')
const inquirer = require('inquirer');

const asyncExec = promisify(exec);

const question = {
  type: 'list',
  name: 'version',
  message: 'Semantic Version?',
  choices: ['Patch', 'Minor', 'Major'],
  filter(val) {
    return val.toLowerCase();
  },
};

(async () => {
  const { version } = await inquirer.prompt(question);
  await asyncExec(`npm version ${version} --no-git-tag-version`);
})();

It appears the pre-commit hook doesn't wait for inquirer to finish. It shows the prompt but doesn't allow the user to interact with it as the hook thinks it's finished. Screenshot 2022-03-26 at 12 40 04

The pre-commit hook is using husky which calls "version:select": "node ./scripts/versionSelect.js",.

Maybe this is an issue with Husky? I would have thought this might be a common use case for this library though so maybe others have come across this issue?

Thanks

stretch0 avatar Mar 26 '22 13:03 stretch0

You'll need to make sure the sub-process you boot has access to the stdin/out of the main process for it to be interactive. This might only work with spawnSync passing the right stdio option.

And it might need some special configuration on Husky; but I'm not 100% sure how it works for them.

SBoudrias avatar Mar 28 '22 16:03 SBoudrias

@stretch0 you can try to check this past issue: https://github.com/SBoudrias/Inquirer.js/issues/518

The exec < /dev/tty trick worked for me.

nicco88 avatar May 25 '22 17:05 nicco88

Moved instructions for this to the README https://github.com/SBoudrias/Inquirer.js#using-as-pre-commitgit-hooks-or-scripts

Feel free to send a PR to clarify the doc if anything would've helped you when you ran into this problem! Cheers

SBoudrias avatar Jun 03 '23 21:06 SBoudrias