Skip to content

Commit 1f661f6

Browse files
BYKgetsentry-bot
andauthored
ref(inquirer): Replace inquirer with prompts (#217)
* ref(inquirer): Replace inquirer with prompts 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. * ref: Lint fixes * add @types/prompts * remove obsolete function Co-authored-by: getsentry-bot <[email protected]>
1 parent ca4165a commit 1f661f6

File tree

6 files changed

+38
-291
lines changed

6 files changed

+38
-291
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131
"@types/async": "^3.0.1",
3232
"@types/cli-table": "^0.3.0",
3333
"@types/form-data": "^2.2.1",
34-
"@types/inquirer": "^0.0.41",
3534
"@types/jest": "^26.0.14",
3635
"@types/js-yaml": "^3.11.1",
3736
"@types/mkdirp": "^1.0.0",
3837
"@types/node": "^12.11.1",
3938
"@types/node-fetch": "^2.1.1",
4039
"@types/once": "^1.4.0",
4140
"@types/ora": "^1.3.4",
41+
"@types/prompts": "^2.0.11",
4242
"@types/request": "^2.47.1",
4343
"@types/rimraf": "^2.0.2",
4444
"@types/shell-quote": "^1.6.0",
@@ -57,7 +57,6 @@
5757
"esbuild": "^0.11.6",
5858
"eslint": "^7.2.0",
5959
"eslint-config-prettier": "^6.11.0",
60-
"inquirer": "6.2.1",
6160
"jest": "^26.5.3",
6261
"js-yaml": "3.12.0",
6362
"json-schema-to-typescript": "5.7.0",
@@ -67,6 +66,7 @@
6766
"once": "1.4.0",
6867
"ora": "2.1.0",
6968
"prettier": "^2.2.1",
69+
"prompts": "2.4.1",
7070
"request": "2.88.0",
7171
"rimraf": "2.7.1",
7272
"shell-quote": "1.6.1",

src/commands/publish.ts

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Github from '@octokit/rest';
2-
import * as inquirer from 'inquirer';
2+
import prompts from 'prompts';
33
import { Arguments, Argv, CommandBuilder } from 'yargs';
44
import chalk from 'chalk';
55
import {
@@ -28,7 +28,7 @@ import {
2828
SpecialTarget,
2929
} from '../targets';
3030
import { BaseTarget } from '../targets/base';
31-
import { coerceType, handleGlobalError, reportError } from '../utils/errors';
31+
import { handleGlobalError, reportError } from '../utils/errors';
3232
import { withTempDir } from '../utils/files';
3333
import { stringToRegexp } from '../utils/filters';
3434
import { getGithubClient, mergeReleaseBranch } from '../utils/githubApi';
@@ -215,19 +215,15 @@ async function promptConfirmation(targetList: BaseTarget[]): Promise<void> {
215215
logger.info(' ');
216216

217217
if (hasInput()) {
218-
const questions = [
219-
{
220-
message: 'Is everything OK? Type "yes" to proceed:',
221-
name: 'readyToPublish',
222-
type: 'input',
223-
// Force the user to type something that is not empty or one letter such
224-
// as y/n to make sure this is a concious choice.
225-
validate: (input: string) =>
226-
input.length >= 2 || 'Please type "yes" to proceed',
227-
},
228-
];
229-
const answers = (await inquirer.prompt(questions)) as any;
230-
const readyToPublish = coerceType<string>(answers.readyToPublish, 'string');
218+
const { readyToPublish } = await prompts({
219+
message: 'Is everything OK? Type "yes" to proceed:',
220+
name: 'readyToPublish',
221+
type: 'text',
222+
// Force the user to type something that is not empty or one letter such
223+
// as y/n to make sure this is a concious choice.
224+
validate: (input: string) =>
225+
input.length >= 2 || 'Please type "yes" to proceed',
226+
});
231227
if (readyToPublish.toLowerCase() !== 'yes') {
232228
logger.error('Oh, okay. Aborting.');
233229
process.exit(1);

src/targets/npm.ts

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SpawnOptions, spawnSync } from 'child_process';
2-
import * as inquirer from 'inquirer';
2+
import prompts from 'prompts';
33

44
import { logger as loggerRaw } from '../logger';
55
import { TargetConfig } from '../schemas/project_config';
@@ -114,17 +114,14 @@ export class NpmTarget extends BaseTarget {
114114
* Ask the user for the OTP value
115115
*/
116116
protected async requestOtp(): Promise<string> {
117-
const questions = [
118-
{
119-
message: 'Looks like your NPM account uses 2FA. Enter OTP:',
120-
name: 'otp',
121-
type: 'input',
122-
validate: (input: string) =>
123-
(input.length > 3 && input.length < 10) || 'Valid OTP, please',
124-
},
125-
];
126-
const answers = (await inquirer.prompt(questions)) as any;
127-
return answers.otp;
117+
const { otp } = await prompts({
118+
message: 'Looks like your NPM account uses 2FA. Enter OTP:',
119+
name: 'otp',
120+
type: 'text',
121+
validate: (input: string) =>
122+
(input.length > 3 && input.length < 10) || 'Valid OTP, please',
123+
});
124+
return otp;
128125
}
129126

130127
/**

src/utils/__tests__/errors.test.ts

-20
This file was deleted.

src/utils/errors.ts

-32
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,6 @@ export function reportError(
4444
}
4545
}
4646

47-
/**
48-
* Checks at runtime that the type of the object is what we expect it is
49-
*
50-
* Throws an error if the type of the object is different.
51-
*
52-
* @param obj Object, which type we're checking
53-
* @param typeName Expected type name
54-
* @param message Optional error message
55-
*/
56-
export function coerceType<T>(
57-
obj: T,
58-
typeName:
59-
| 'string'
60-
| 'number'
61-
| 'bigint'
62-
| 'boolean'
63-
| 'symbol'
64-
| 'undefined'
65-
| 'object'
66-
| 'function',
67-
message?: string
68-
): T | never {
69-
const objType = typeof obj;
70-
if (objType !== typeName) {
71-
throw new TypeError(
72-
message ||
73-
`${String(obj)} is of type "${objType}" (expected: "${typeName}")`
74-
);
75-
}
76-
return obj;
77-
}
78-
7947
/**
8048
* Processes an uncaught exception on the global level
8149
*

0 commit comments

Comments
 (0)