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

fix: handle SIGINT with prompt #2887

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
17 changes: 16 additions & 1 deletion packages/webpack-cli/lib/utils/prompt.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const utils = require("./index");

const prompt = ({ message, defaultResponse, stream }) => {
const readline = require("readline");
const rl = readline.createInterface({
Expand All @@ -9,7 +11,6 @@ const prompt = ({ message, defaultResponse, stream }) => {
rl.question(`${message} `, (answer) => {
// Close the stream
rl.close();

const response = (answer || defaultResponse).toLowerCase();

// Resolve with the input response
Expand All @@ -19,6 +20,20 @@ const prompt = ({ message, defaultResponse, stream }) => {
resolve(false);
}
});

const handleSIGINT = () => {
rl.close();
process.stdout.write("\n");
utils.logger.warn("Operation canceled.");
process.exit(0);
};

rl.on("SIGINT", () => {
handleSIGINT();
});
process.on("SIGINT", () => {
handleSIGINT();
});
});
};

Expand Down
9 changes: 9 additions & 0 deletions test/api/helpers/runAndKillPrompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const prompt = require("../../../packages/webpack-cli/lib/utils/prompt");

setTimeout(() => process.kill(process.pid, "SIGINT"), 1000);

prompt({
message: "Would you like to install package 'test'? (Yes/No):",
defaultResponse: "No",
stream: process.stdout,
});
33 changes: 32 additions & 1 deletion test/api/prompt.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const prompt = require("../../packages/webpack-cli/lib/utils/prompt");
const { resolve } = require("path");
// eslint-disable-next-line node/no-unpublished-require
const execa = require("execa");
const { Writable } = require("stream");
const { isWindows } = require("../utils/test-utils");

describe("prompt", () => {
class MyWritable extends Writable {
Expand Down Expand Up @@ -32,9 +36,10 @@ describe("prompt", () => {
expect(resultFail).toBe(false);
});

it('should work with "yes" && "y" response', async () => {
it('should work with "yes", "YES"," and y" response', async () => {
const myWritable1 = new MyWritable("yes\r");
const myWritable2 = new MyWritable("y\r");
const myWritable3 = new MyWritable("YES\r");

const resultSuccess1 = await prompt({
message: "message",
Expand All @@ -48,8 +53,15 @@ describe("prompt", () => {
stream: myWritable2,
});

const resultSuccess3 = await prompt({
message: "message",
defaultResponse: "no",
stream: myWritable3,
});

expect(resultSuccess1).toBe(true);
expect(resultSuccess2).toBe(true);
expect(resultSuccess3).toBe(true);
});

it("should work with unknown response", async () => {
Expand All @@ -63,4 +75,23 @@ describe("prompt", () => {

expect(result).toBe(false);
});

it("should respond to SIGINT", async () => {
const runAndKillPrompt = resolve(__dirname, "./helpers/runAndKillPrompt.js");

const { exitCode, stderr, stdout } = await execa("node", [runAndKillPrompt], {
cwd: resolve(__dirname),
reject: false,
maxBuffer: Infinity,
});

// TODO: fix for windows
if (isWindows) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not good to skip windows test here...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will see if I can test it on a windows machine locally.

expect(true).toBe(true);
} else {
expect(exitCode).toBe(0);
expect(stderr).toContain("[webpack-cli] Operation canceled.");
expect(stdout).toContain("Would you like to install package 'test'? (Yes/No):");
}
});
});