Skip to content

Commit

Permalink
feat: enquirer (#81)
Browse files Browse the repository at this point in the history
Use https://www.npmjs.com/package/enquirer instead of https://www.npmjs.com/package/inquirer to save some bytes and increase loading speed.

* feat: move to enquirer

* chore: unpin typed-emitter
  • Loading branch information
hongaar authored Jul 28, 2020
1 parent c0e0a9c commit 7891e70
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 286 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
### TODOs
| Filename | line # | TODO
|:------|:------:|:------
| [src/prompter.ts](src/prompter.ts#L75) | 75 | ignoring type error here, probably need another type
| [src/prompter.ts](src/prompter.ts#L16) | 16 | Wait for upstream change to import types from enquirer
| [src/prompter.ts](src/prompter.ts#L77) | 77 | ignoring type error here, probably need another type
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@
"author": "",
"license": "MIT",
"dependencies": {
"@types/inquirer": "^7.0.0",
"@types/yargs": "^15.0.5",
"inquirer": "^7.3.0",
"enquirer": "^2.3.6",
"string-argv": "^0.3.1",
"typed-emitter": "1.2.0",
"typed-emitter": "^1.2.0",
"yargs": "^15.4.0"
},
"devDependencies": {
Expand Down
18 changes: 10 additions & 8 deletions src/prompter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { prompt, Question as BaseQuestion, ListQuestion } from 'inquirer'
import { prompt } from 'enquirer'
import { Argument } from './argument'
import { Option } from './option'

Expand All @@ -13,7 +13,9 @@ type PromptType =
| 'password'
| 'editor'

type Question = BaseQuestion | ListQuestion
// @todo Wait for upstream change to import types from enquirer
// @link https://github.com/enquirer/enquirer/pull/258
type Question = any

/**
* Creates a new command, which can be added to a program.
Expand Down Expand Up @@ -69,9 +71,9 @@ export class Prompter<T = {}> {
// Use checkbox question type
questions.push({
name,
type: 'checkbox',
type: 'multiselect',
message: arg.getPrompt(),
default: defaultValue,
initial: defaultValue,
// @todo ignoring type error here, probably need another type
// than Question[]
// @ts-ignore
Expand All @@ -83,9 +85,9 @@ export class Prompter<T = {}> {
// Use list question type
questions.push({
name,
type: 'list',
type: 'select',
message: arg.getPrompt(),
default: defaultValue,
initial: defaultValue,
choices: arg.getChoices() as string[],
})
break
Expand All @@ -96,7 +98,7 @@ export class Prompter<T = {}> {
name,
type: 'confirm',
message: arg.getPrompt(),
default: defaultValue,
initial: defaultValue,
})
break

Expand All @@ -106,7 +108,7 @@ export class Prompter<T = {}> {
name,
type: 'input',
message: arg.getPrompt(),
default: defaultValue,
initial: defaultValue,
})
}
}
Expand Down
10 changes: 5 additions & 5 deletions tests/command.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import Enquirer from 'enquirer'
import { command, Command, program } from '../src'
import inquirer = require('inquirer')

const prompt = jest.fn()
jest.mock('enquirer')

const prompt = (Enquirer.prompt = jest.fn())

let outputSpy: jest.MockInstance<any, any>
let errorSpy: jest.MockInstance<any, any>
let promptSpy: jest.MockInstance<any, any>

beforeEach(() => {
outputSpy = jest.spyOn(console, 'log').mockImplementation(() => {})
errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
promptSpy = jest.spyOn(inquirer, 'prompt').mockImplementation(prompt)
prompt.mockClear()
})

afterEach(() => {
outputSpy.mockRestore()
errorSpy.mockRestore()
promptSpy.mockRestore()
})

test('command should return new Command object', () => {
Expand Down
Loading

0 comments on commit 7891e70

Please sign in to comment.