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

Prevent bin process from crashing on sigint when sent during npm init dialog #55

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/commands/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ module.exports = async ({ cwd, dir, ctx }) => {
Helpers.writeFile(Path.join(dir, 'README.md'), '')
]);

const ignore = () => {

/* $lab:coverage:off$ */
ctx.options.out.write('\n');
/* $lab:coverage:on$ */
};

try {
process.on('SIGINT', ignore);
await internals.npmInit(dir, ctx);
}
catch (ignoreErr) {
Expand All @@ -69,6 +77,9 @@ module.exports = async ({ cwd, dir, ctx }) => {
) + '\n'
);
}
finally {
process.removeListener('SIGINT', ignore);
}

let finalPkg = await Helpers.readFile(Path.join(dir, 'package.json'));

Expand Down Expand Up @@ -156,6 +167,7 @@ internals.npmInit = (cwd, ctx) => {

subproc.once('close', (code) => {

// code is null on Ctrl-C (SIGINT)
if (code !== 0) {
return reject(new Error(`Failed with code: ${code}`));
}
Expand Down
53 changes: 53 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,59 @@ describe('hpal', () => {
expect(`${logError}`).to.contain('your current branch \'master\' does not have any commits');
});

it('creates a new pal project when bailing on `npm init` by ^C press (SIGINT).', { timeout: 5000, skip: process.platform === 'win32' }, async (flags) => {

flags.onCleanup = async () => await rimraf('new/sigint-on-npm-init');

const result = await RunUtil.bin(['new', 'closet/new/sigint-on-npm-init'], null, true);

expect(result.output).to.contain('New pal project created in closet/new/sigint-on-npm-init');
expect(result.errorOutput).to.contain('Bailed on `npm init`, but continuing to setup your project with an incomplete package.json file.');

const results = await Promise.all([
read('new/sigint-on-npm-init/package.json'),
read('new/sigint-on-npm-init/README.md'),
exists('new/sigint-on-npm-init/lib/index.js'),
exists('new/sigint-on-npm-init/test/index.js'),
exec('git remote', 'new/sigint-on-npm-init'),
exec('git tag', 'new/sigint-on-npm-init'),
exec('git ls-files -m', 'new/sigint-on-npm-init'),
returnError(exec('git log', 'new/sigint-on-npm-init'))
]);

const pkgAsString = results[0];
const pkg = JSON.parse(pkgAsString);
const readme = results[1];
const readmeH1 = readme.trim().substring(2);
const lib = results[2];
const test = results[3];
const remotes = results[4][0].split('\n');
const tags = results[5][0].split('\n');
const modifiedFiles = results[6][0].trim();
const logError = results[7];

expect(pkg.name).to.not.exist();
expect(pkg.version).to.not.exist();
expect(pkg.dependencies).to.exist();
expect(pkg.devDependencies).to.exist();
expect(Object.keys(pkg)).to.equal(bailedPkgKeysOrder);
expect(Object.keys(pkg.dependencies)).to.equal(Object.keys(pkg.dependencies).sort());
expect(Object.keys(pkg.devDependencies)).to.equal(Object.keys(pkg.devDependencies).sort());
expect(pkgAsString.endsWith('\n')).to.equal(true);
expect(readmeH1).to.equal('sigint-on-npm-init');
expect(lib).to.exist();
expect(test).to.exist();
expect(remotes).to.contain('pal');
expect(tags).to.contain('swagger');
expect(tags).to.contain('custom-swagger');
expect(tags).to.contain('deployment');
expect(tags).to.contain('objection');
expect(tags).to.contain('templated-site');
expect(tags).to.contain('fancy-templated-site');
expect(modifiedFiles).to.equal('');
expect(`${logError}`).to.contain('your current branch \'master\' does not have any commits');
});

it('fails in a friendly way when trying to create a project in a non-empty directory.', async () => {

const result = await RunUtil.cli(['new', 'project-already-exists'], 'new');
Expand Down
10 changes: 8 additions & 2 deletions test/run-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const Stream = require('stream');
const DisplayError = require('../lib/display-error');
const Hpal = require('..');

exports.bin = (argv, cwd) => {
exports.bin = (argv, cwd, bailOnNpmInit) => {

return new Promise((resolve, reject) => {

const path = Path.join(__dirname, '..', 'bin', 'hpal');
const cli = ChildProcess.spawn('node', [].concat(path, argv), { cwd: cwd || __dirname });
const cli = ChildProcess.spawn('node', [].concat(path, argv), { cwd: cwd || __dirname, ...(bailOnNpmInit && { detached: true }) });

let output = '';
let errorOutput = '';
Expand All @@ -21,6 +21,12 @@ exports.bin = (argv, cwd) => {

output += data;
combinedOutput += data;

if (bailOnNpmInit && ~data.toString().indexOf('Press ^C at any time to quit.')) {
// negative process id kills all processes led by the CLI process (process group id = cli.pid)
// this group includes the npm init child process spawned by the new command
process.kill(-cli.pid, 'SIGINT');
}
});

cli.stderr.on('data', (data) => {
Expand Down