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

feat: use own inquirer implementation #2538

Merged
merged 6 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ const getPackageManager = require('../get-package-manager');
jest.mock('../get-package-manager', () => jest.fn());
const globalModulesNpmValue = 'test-npm';
jest.setMock('global-modules', globalModulesNpmValue);
jest.setMock('enquirer', {
prompt: jest.fn(),
});
jest.setMock('../prompt', jest.fn());

describe('packageUtils', () => {
describe('getPackageManager', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const stripAnsi = require('strip-ansi');
const globalModulesNpmValue = 'test-npm';

jest.setMock('global-modules', globalModulesNpmValue);
jest.setMock('enquirer', { prompt: jest.fn() });
jest.setMock('../prompt', jest.fn());
jest.setMock('../run-command', jest.fn());
jest.setMock('../package-exists', jest.fn());
jest.setMock('../get-package-manager', jest.fn());
Expand All @@ -14,7 +14,7 @@ const getPackageManager = require('../get-package-manager');
const packageExists = require('../package-exists');
const promptInstallation = require('../prompt-installation');
const runCommand = require('../run-command');
const { prompt } = require('enquirer');
const prompt = require('../prompt');

describe('promptInstallation', () => {
beforeAll(() => {
Expand All @@ -26,7 +26,7 @@ describe('promptInstallation', () => {
});

it('should prompt to install using npm if npm is package manager', async () => {
prompt.mockReturnValue({ installConfirm: true });
prompt.mockReturnValue(true);

getPackageManager.mockReturnValue('npm');

Expand All @@ -37,7 +37,7 @@ describe('promptInstallation', () => {
expect(preMessage.mock.calls.length).toEqual(1);
expect(prompt.mock.calls.length).toEqual(1);
expect(runCommand.mock.calls.length).toEqual(1);
expect(stripAnsi(prompt.mock.calls[0][0][0].message)).toContain(
expect(stripAnsi(prompt.mock.calls[0][0].message)).toContain(
"Would you like to install 'test-package' package? (That will run 'npm install -D test-package')",
);

Expand All @@ -55,7 +55,7 @@ describe('promptInstallation', () => {
expect(promptResult).toBeTruthy();
expect(prompt.mock.calls.length).toEqual(1);
expect(runCommand.mock.calls.length).toEqual(1);
expect(stripAnsi(prompt.mock.calls[0][0][0].message)).toContain(
expect(stripAnsi(prompt.mock.calls[0][0].message)).toContain(
"Would you like to install 'test-package' package? (That will run 'yarn add -D test-package')",
);

Expand All @@ -73,7 +73,7 @@ describe('promptInstallation', () => {
expect(promptResult).toBeTruthy();
expect(prompt.mock.calls.length).toEqual(1);
expect(runCommand.mock.calls.length).toEqual(1);
expect(stripAnsi(prompt.mock.calls[0][0][0].message)).toContain(
expect(stripAnsi(prompt.mock.calls[0][0].message)).toContain(
"Would you like to install 'test-package' package? (That will run 'pnpm install -D test-package')",
);

Expand All @@ -93,7 +93,7 @@ describe('promptInstallation', () => {
expect(preMessage.mock.calls.length).toEqual(1);
expect(prompt.mock.calls.length).toEqual(1);
expect(runCommand.mock.calls.length).toEqual(1);
expect(stripAnsi(prompt.mock.calls[0][0][0].message)).toContain(
expect(stripAnsi(prompt.mock.calls[0][0].message)).toContain(
"Would you like to install 'test-package' package? (That will run 'npm install -D test-package')",
);

Expand All @@ -102,7 +102,7 @@ describe('promptInstallation', () => {
});

it('should not install if install is not confirmed', async () => {
prompt.mockReturnValue({ installConfirm: false });
prompt.mockReturnValue(false);

const mockExit = jest.spyOn(process, 'exit').mockImplementation(() => {});
const promptResult = await promptInstallation('test-package');
Expand Down
20 changes: 8 additions & 12 deletions packages/webpack-cli/lib/utils/prompt-installation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { prompt } = require('enquirer');
const utils = require('./index');
const prompt = require('./prompt');

/**
*
Expand All @@ -25,17 +25,13 @@ async function promptInstallation(packageName, preMessage) {
let installConfirm;

try {
({ installConfirm } = await prompt([
{
type: 'confirm',
name: 'installConfirm',
message: `Would you like to install '${colors.green(packageName)}' package? (That will run '${colors.green(
commandToBeRun,
)}')`,
initial: 'Y',
stdout: process.stderr,
},
]));
installConfirm = await prompt({
message: `Would you like to install '${colors.green(packageName)}' package? (That will run '${colors.green(
commandToBeRun,
)}') (${colors.yellow('Y/n')})`,
defaultResponse: 'Y',
stdout: process.stderr,
});
} catch (error) {
utils.logger.error(error);
process.exit(2);
Expand Down
19 changes: 19 additions & 0 deletions packages/webpack-cli/lib/utils/prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const prompt = ({ message, defaultResponse, stdout }) => {
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: stdout,
});

return new Promise((resolve) => {
rl.question(message, function (answer) {
// close the stream
rl.close();
// resolve with the input response
if (['y', 'yes'].includes((answer || defaultResponse).toLowerCase())) resolve(true);
anshumanv marked this conversation as resolved.
Show resolved Hide resolved
resolve(false);
anshumanv marked this conversation as resolved.
Show resolved Hide resolved
});
});
};

module.exports = prompt;
1 change: 0 additions & 1 deletion packages/webpack-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"@webpack-cli/serve": "^1.3.1",
"colorette": "^1.2.1",
"commander": "^7.0.0",
"enquirer": "^2.3.6",
"execa": "^5.0.0",
"fastest-levenshtein": "^1.0.12",
"import-local": "^3.0.2",
Expand Down
34 changes: 0 additions & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2020,14 +2020,6 @@
"@typescript-eslint/typescript-estree" "4.21.0"
debug "^4.1.1"

"@typescript-eslint/[email protected]":
version "4.20.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.20.0.tgz#953ecbf3b00845ece7be66246608be9d126d05ca"
integrity sha512-/zm6WR6iclD5HhGpcwl/GOYDTzrTHmvf8LLLkwKqqPKG6+KZt/CfSgPCiybshmck66M2L5fWSF/MKNuCwtKQSQ==
dependencies:
"@typescript-eslint/types" "4.20.0"
"@typescript-eslint/visitor-keys" "4.20.0"

"@typescript-eslint/[email protected]":
version "4.21.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-4.21.0.tgz#c81b661c4b8af1ec0c010d847a8f9ab76ab95b4d"
Expand All @@ -2036,29 +2028,11 @@
"@typescript-eslint/types" "4.21.0"
"@typescript-eslint/visitor-keys" "4.21.0"

"@typescript-eslint/[email protected]":
version "4.20.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.20.0.tgz#c6cf5ef3c9b1c8f699a9bbdafb7a1da1ca781225"
integrity sha512-cYY+1PIjei1nk49JAPnH1VEnu7OYdWRdJhYI5wiKOUMhLTG1qsx5cQxCUTuwWCmQoyriadz3Ni8HZmGSofeC+w==

"@typescript-eslint/[email protected]":
version "4.21.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-4.21.0.tgz#abdc3463bda5d31156984fa5bc316789c960edef"
integrity sha512-+OQaupjGVVc8iXbt6M1oZMwyKQNehAfLYJJ3SdvnofK2qcjfor9pEM62rVjBknhowTkh+2HF+/KdRAc/wGBN2w==

"@typescript-eslint/[email protected]":
version "4.20.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.20.0.tgz#8b3b08f85f18a8da5d88f65cb400f013e88ab7be"
integrity sha512-Knpp0reOd4ZsyoEJdW8i/sK3mtZ47Ls7ZHvD8WVABNx5Xnn7KhenMTRGegoyMTx6TiXlOVgMz9r0pDgXTEEIHA==
dependencies:
"@typescript-eslint/types" "4.20.0"
"@typescript-eslint/visitor-keys" "4.20.0"
debug "^4.1.1"
globby "^11.0.1"
is-glob "^4.0.1"
semver "^7.3.2"
tsutils "^3.17.1"

"@typescript-eslint/[email protected]":
version "4.21.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-4.21.0.tgz#3817bd91857beeaeff90f69f1f112ea58d350b0a"
Expand All @@ -2072,14 +2046,6 @@
semver "^7.3.2"
tsutils "^3.17.1"

"@typescript-eslint/[email protected]":
version "4.20.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.20.0.tgz#1e84db034da13f208325e6bfc995c3b75f7dbd62"
integrity sha512-NXKRM3oOVQL8yNFDNCZuieRIwZ5UtjNLYtmMx2PacEAGmbaEYtGgVHUHVyZvU/0rYZcizdrWjDo+WBtRPSgq+A==
dependencies:
"@typescript-eslint/types" "4.20.0"
eslint-visitor-keys "^2.0.0"

"@typescript-eslint/[email protected]":
version "4.21.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-4.21.0.tgz#990a9acdc124331f5863c2cf21c88ba65233cd8d"
Expand Down