Skip to content

Commit

Permalink
ref(inquirer): Replace inquirer with prompts
Browse files Browse the repository at this point in the history
Inspired by facebook/create-react-app#10083. `prompts` is a modern and
lighter alternative to `inquirer` and it is almost a drop-in
replacement. This change shaves off 0.7MB from our minified builds.
  • Loading branch information
BYK committed May 4, 2021
1 parent ca4165a commit cecfdcb
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 238 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"@types/async": "^3.0.1",
"@types/cli-table": "^0.3.0",
"@types/form-data": "^2.2.1",
"@types/inquirer": "^0.0.41",
"@types/jest": "^26.0.14",
"@types/js-yaml": "^3.11.1",
"@types/mkdirp": "^1.0.0",
Expand All @@ -57,7 +56,7 @@
"esbuild": "^0.11.6",
"eslint": "^7.2.0",
"eslint-config-prettier": "^6.11.0",
"inquirer": "6.2.1",
"prompts": "2.4.1",
"jest": "^26.5.3",
"js-yaml": "3.12.0",
"json-schema-to-typescript": "5.7.0",
Expand Down
24 changes: 10 additions & 14 deletions src/commands/publish.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Github from '@octokit/rest';
import * as inquirer from 'inquirer';
import prompts from 'prompts';
import { Arguments, Argv, CommandBuilder } from 'yargs';
import chalk from 'chalk';
import {
Expand Down Expand Up @@ -215,19 +215,15 @@ async function promptConfirmation(targetList: BaseTarget[]): Promise<void> {
logger.info(' ');

if (hasInput()) {
const questions = [
{
message: 'Is everything OK? Type "yes" to proceed:',
name: 'readyToPublish',
type: 'input',
// Force the user to type something that is not empty or one letter such
// as y/n to make sure this is a concious choice.
validate: (input: string) =>
input.length >= 2 || 'Please type "yes" to proceed',
},
];
const answers = (await inquirer.prompt(questions)) as any;
const readyToPublish = coerceType<string>(answers.readyToPublish, 'string');
const { readyToPublish } = await prompts({
message: 'Is everything OK? Type "yes" to proceed:',
name: 'readyToPublish',
type: 'text',
// Force the user to type something that is not empty or one letter such
// as y/n to make sure this is a concious choice.
validate: (input: string) =>
input.length >= 2 || 'Please type "yes" to proceed',
});
if (readyToPublish.toLowerCase() !== 'yes') {
logger.error('Oh, okay. Aborting.');
process.exit(1);
Expand Down
21 changes: 9 additions & 12 deletions src/targets/npm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SpawnOptions, spawnSync } from 'child_process';
import * as inquirer from 'inquirer';
import prompts from 'prompts';

import { logger as loggerRaw } from '../logger';
import { TargetConfig } from '../schemas/project_config';
Expand Down Expand Up @@ -114,17 +114,14 @@ export class NpmTarget extends BaseTarget {
* Ask the user for the OTP value
*/
protected async requestOtp(): Promise<string> {
const questions = [
{
message: 'Looks like your NPM account uses 2FA. Enter OTP:',
name: 'otp',
type: 'input',
validate: (input: string) =>
(input.length > 3 && input.length < 10) || 'Valid OTP, please',
},
];
const answers = (await inquirer.prompt(questions)) as any;
return answers.otp;
const { otp } = await prompts({
message: 'Looks like your NPM account uses 2FA. Enter OTP:',
name: 'otp',
type: 'text',
validate: (input: string) =>
(input.length > 3 && input.length < 10) || 'Valid OTP, please',
});
return otp;
}

/**
Expand Down
Loading

0 comments on commit cecfdcb

Please sign in to comment.