Skip to content

Commit

Permalink
ref(inquirer): Replace inquirer with prompts (#217)
Browse files Browse the repository at this point in the history
* 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 <bot@sentry.io>
  • Loading branch information
BYK and getsentry-bot authored May 4, 2021
1 parent ca4165a commit 1f661f6
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 291 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
"@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",
"@types/node": "^12.11.1",
"@types/node-fetch": "^2.1.1",
"@types/once": "^1.4.0",
"@types/ora": "^1.3.4",
"@types/prompts": "^2.0.11",
"@types/request": "^2.47.1",
"@types/rimraf": "^2.0.2",
"@types/shell-quote": "^1.6.0",
Expand All @@ -57,7 +57,6 @@
"esbuild": "^0.11.6",
"eslint": "^7.2.0",
"eslint-config-prettier": "^6.11.0",
"inquirer": "6.2.1",
"jest": "^26.5.3",
"js-yaml": "3.12.0",
"json-schema-to-typescript": "5.7.0",
Expand All @@ -67,6 +66,7 @@
"once": "1.4.0",
"ora": "2.1.0",
"prettier": "^2.2.1",
"prompts": "2.4.1",
"request": "2.88.0",
"rimraf": "2.7.1",
"shell-quote": "1.6.1",
Expand Down
26 changes: 11 additions & 15 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 @@ -28,7 +28,7 @@ import {
SpecialTarget,
} from '../targets';
import { BaseTarget } from '../targets/base';
import { coerceType, handleGlobalError, reportError } from '../utils/errors';
import { handleGlobalError, reportError } from '../utils/errors';
import { withTempDir } from '../utils/files';
import { stringToRegexp } from '../utils/filters';
import { getGithubClient, mergeReleaseBranch } from '../utils/githubApi';
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
20 changes: 0 additions & 20 deletions src/utils/__tests__/errors.test.ts

This file was deleted.

32 changes: 0 additions & 32 deletions src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,6 @@ export function reportError(
}
}

/**
* Checks at runtime that the type of the object is what we expect it is
*
* Throws an error if the type of the object is different.
*
* @param obj Object, which type we're checking
* @param typeName Expected type name
* @param message Optional error message
*/
export function coerceType<T>(
obj: T,
typeName:
| 'string'
| 'number'
| 'bigint'
| 'boolean'
| 'symbol'
| 'undefined'
| 'object'
| 'function',
message?: string
): T | never {
const objType = typeof obj;
if (objType !== typeName) {
throw new TypeError(
message ||
`${String(obj)} is of type "${objType}" (expected: "${typeName}")`
);
}
return obj;
}

/**
* Processes an uncaught exception on the global level
*
Expand Down
Loading

0 comments on commit 1f661f6

Please sign in to comment.