Skip to content

Commit

Permalink
refactor: use own inquirer implementation (#2538)
Browse files Browse the repository at this point in the history
  • Loading branch information
anshumanv authored Apr 7, 2021
1 parent 7c6f390 commit a3eaeee
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 58 deletions.
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: `[webpack-cli] Would you like to install '${colors.green(packageName)}' package? (That will run '${colors.green(
commandToBeRun,
)}') (${colors.yellow('Y/n')})`,
defaultResponse: 'Y',
stream: process.stderr,
});
} catch (error) {
utils.logger.error(error);
process.exit(2);
Expand Down
25 changes: 25 additions & 0 deletions packages/webpack-cli/lib/utils/prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const prompt = ({ message, defaultResponse, stream }) => {
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: stream,
});

return new Promise((resolve) => {
rl.question(`${message} `, (answer) => {
// Close the stream
rl.close();

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

// Resolve with the input response
if (response === 'y' || response === 'yes') {
resolve(true);
} else {
resolve(false);
}
});
});
};

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

0 comments on commit a3eaeee

Please sign in to comment.