Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spawn(hookName, args, {stdio: 'inherit'}) will skip user input by inquirer.js #63

Open
shiftj2019 opened this issue Nov 13, 2019 · 1 comment

Comments

@shiftj2019
Copy link

shiftj2019 commented Nov 13, 2019

same as SBoudrias/Inquirer.js#518,
spawn(hookName, args, {stdio: 'inherit'}) will finish, not wait for user input(by inquirer.js).

my code is:

// .githooks/pre-commit/hello.js
#!/usr/bin/env node 
console.log('start select ...');

'use strict';
var inquirer = require('inquirer');
var config = require('../../config/var-app');
var fs = require('fs');
var path = require('path');

inquirer
  .prompt([
    {
      type: 'list',
      name: 'appid',
      message: 'What mini application do you want?',
      choices: config.APPS
    }
  ])
  .then(answers => {
    var appId = answers.appid;
    var filePath = path.resolve(__dirname, '../../config/var-app.js');
    console.log('appid: ' + appId);

    fs.readFile(filePath, 'utf8', function(err, data) {
      if (err) return console.log(err);
      var res = data.replace(/\$appid\s*=\s*\'[^']*\'/g, "$appid = '" + appId + "'");
      fs.writeFile(filePath, res, 'utf8', function(err) {
        if (err) return console.log(err);
        console.log('select mini application finished!')
      });
    });
  });

entry code is:

git add -A && git commit -m "test"  // this will skip user input; but use 'node ./.githooks/pre-commit/hello.js', everything is ok.
@shiftj2019
Copy link
Author

same as SBoudrias/Inquirer.js#518,
spawn(hookName, args, {stdio: 'inherit'}) will finish, not wait for user input(by inquirer.js).

my code is:

// .githooks/pre-commit/hello.js
#!/usr/bin/env node 
console.log('start select ...');

'use strict';
var inquirer = require('inquirer');
var config = require('../../config/var-app');
var fs = require('fs');
var path = require('path');

inquirer
  .prompt([
    {
      type: 'list',
      name: 'appid',
      message: 'What mini application do you want?',
      choices: config.APPS
    }
  ])
  .then(answers => {
    var appId = answers.appid;
    var filePath = path.resolve(__dirname, '../../config/var-app.js');
    console.log('appid: ' + appId);

    fs.readFile(filePath, 'utf8', function(err, data) {
      if (err) return console.log(err);
      var res = data.replace(/\$appid\s*=\s*\'[^']*\'/g, "$appid = '" + appId + "'");
      fs.writeFile(filePath, res, 'utf8', function(err) {
        if (err) return console.log(err);
        console.log('select mini application finished!')
      });
    });
  });

entry code is:

git add -A && git commit -m "test"  // this will skip user input; but use 'node ./.githooks/pre-commit/hello.js', everything is ok.

Can solve it temporarily like:

// .githooks/pre-commit/hello
#!/bin/sh
exec < /dev/tty
node ./githooks/scripts/pre-commit.js
// .githooks/scripts/pre-commit.js
#!/usr/bin/env node 

'use strict';
var inquirer = require('inquirer');

inquirer
  .prompt([
    ...
  ])
  .then(answers => {
    ...
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant