Skip to content

Commit 611bc45

Browse files
committed
fix: handle SIGINT with prompt
1 parent 9c9a783 commit 611bc45

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

packages/webpack-cli/lib/utils/prompt.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,19 @@ const prompt = ({ message, defaultResponse, stream }) => {
2020
resolve(false);
2121
}
2222
});
23-
rl.on("SIGINT", () => {
23+
24+
const handleSIGINT = () => {
2425
rl.close();
2526
process.stdout.write("\n");
2627
utils.logger.warn("Operation canceled.");
2728
process.exit(0);
29+
};
30+
31+
rl.on("SIGINT", () => {
32+
handleSIGINT();
33+
});
34+
process.on("SIGINT", () => {
35+
handleSIGINT();
2836
});
2937
});
3038
};

test/api/helpers/runAndKillPrompt.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const prompt = require("../../../packages/webpack-cli/lib/utils/prompt");
2+
3+
setTimeout(() => process.kill(process.pid, "SIGINT"), 1000);
4+
5+
prompt({
6+
message: "Would you like to install package 'test'? (Yes/No):",
7+
defaultResponse: "No",
8+
stream: process.stdout,
9+
});

test/api/prompt.test.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const prompt = require("../../packages/webpack-cli/lib/utils/prompt");
2+
const { resolve } = require("path");
3+
// eslint-disable-next-line node/no-unpublished-require
4+
const execa = require("execa");
25
const { Writable } = require("stream");
36

47
describe("prompt", () => {
@@ -32,9 +35,10 @@ describe("prompt", () => {
3235
expect(resultFail).toBe(false);
3336
});
3437

35-
it('should work with "yes" && "y" response', async () => {
38+
it('should work with "yes", "YES"," and y" response', async () => {
3639
const myWritable1 = new MyWritable("yes\r");
3740
const myWritable2 = new MyWritable("y\r");
41+
const myWritable3 = new MyWritable("YES\r");
3842

3943
const resultSuccess1 = await prompt({
4044
message: "message",
@@ -48,8 +52,15 @@ describe("prompt", () => {
4852
stream: myWritable2,
4953
});
5054

55+
const resultSuccess3 = await prompt({
56+
message: "message",
57+
defaultResponse: "no",
58+
stream: myWritable3,
59+
});
60+
5161
expect(resultSuccess1).toBe(true);
5262
expect(resultSuccess2).toBe(true);
63+
expect(resultSuccess3).toBe(true);
5364
});
5465

5566
it("should work with unknown response", async () => {
@@ -63,4 +74,19 @@ describe("prompt", () => {
6374

6475
expect(result).toBe(false);
6576
});
77+
78+
it("should respond to SIGINT", async () => {
79+
const test = resolve(__dirname, "./helpers/runAndKillPrompt.js");
80+
81+
const { exitCode, stderr, stdout } = await execa("node", [test], {
82+
cwd: resolve(__dirname),
83+
reject: false,
84+
maxBuffer: Infinity,
85+
killSignal: "SIGINT",
86+
});
87+
88+
expect(exitCode).toBe(0);
89+
expect(stderr).toContain("[webpack-cli] Operation canceled.");
90+
expect(stdout).toContain("Would you like to install package 'test'? (Yes/No):");
91+
});
6692
});

0 commit comments

Comments
 (0)