Skip to content

Commit

Permalink
Revert failed process mgmt refactor, skip SIGINT test on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
zemccartney committed May 4, 2021
1 parent 3cfbd52 commit f0126c5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
29 changes: 11 additions & 18 deletions lib/commands/new.js
Expand Up @@ -59,12 +59,16 @@ module.exports = async ({ cwd, dir, ctx }) => {
Helpers.writeFile(Path.join(dir, 'README.md'), '')
]);

let reject;
const ignore = () => {

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

try {
const dialog = internals.npmInit(dir, ctx);
reject = dialog.reject;
process.on('SIGINT', reject);
await dialog.promise;
process.on('SIGINT', ignore);
await internals.npmInit(dir, ctx);
}
catch (ignoreErr) {
ctx.options.err.write(
Expand All @@ -74,7 +78,7 @@ module.exports = async ({ cwd, dir, ctx }) => {
);
}
finally {
process.removeListener('SIGINT', reject);
process.removeListener('SIGINT', ignore);
}

let finalPkg = await Helpers.readFile(Path.join(dir, 'package.json'));
Expand Down Expand Up @@ -139,8 +143,7 @@ internals.ensureGitAndNpm = async ({ colors }) => {

internals.npmInit = (cwd, ctx) => {

const npmInitDialog = {};
npmInitDialog.promise = new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {

// There is no way to cover this on a single platform
/* $lab:coverage:off$ */
Expand All @@ -149,14 +152,6 @@ internals.npmInit = (cwd, ctx) => {

const subproc = ChildProcess.spawn(npmCmd, ['init'], { cwd });

/* $lab:coverage:off$ */
npmInitDialog.reject = () => {

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

subproc.stdout.pipe(ctx.options.out, { end: false });
ctx.options.in.pipe(subproc.stdin);
subproc.stderr.pipe(ctx.options.err, { end: false });
Expand All @@ -180,6 +175,4 @@ internals.npmInit = (cwd, ctx) => {
return resolve();
});
});

return npmInitDialog;
};
2 changes: 1 addition & 1 deletion test/index.js
Expand Up @@ -825,7 +825,7 @@ 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 }, async (flags) => {
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');

Expand Down
6 changes: 4 additions & 2 deletions test/run-util.js
Expand Up @@ -11,7 +11,7 @@ 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 @@ -23,7 +23,9 @@ exports.bin = (argv, cwd, bailOnNpmInit) => {
combinedOutput += data;

if (bailOnNpmInit && ~data.toString().indexOf('Press ^C at any time to quit.')) {
cli.kill(process.platform === 'win32' ? undefined : 'SIGINT');
// 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');
}
});

Expand Down

0 comments on commit f0126c5

Please sign in to comment.