Skip to content

Commit

Permalink
fix: compatibility with inquirer
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Jan 9, 2019
1 parent 184d64b commit eb90042
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 9 deletions.
2 changes: 1 addition & 1 deletion __test__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test('simple', async () => {
name: 'name',
type: 'input',
message: 'what is your name',
initial: 'kevin'
default: 'kevin'
}
]
})
Expand Down
2 changes: 1 addition & 1 deletion docs/generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Emulate running the generator.

- Type: `(prompt: enquirer.prompt) => void`

By default, running `generator.emulate()` will emulate using the initial values for prompts, the `emulator` it uses looks like:
By default, running `generator.emulate()` will emulate using the default values for prompts, the `emulator` it uses looks like:

```js
const defaultEmulator = prompt => {
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="viewport" content="width=device-width, default-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Kopy</title>
<link rel="stylesheet" href="https://unpkg.com/docute@4/dist/docute.css">
Expand Down
5 changes: 4 additions & 1 deletion docs/prompts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Prompts

Check out the documentation for [prompts](https://github.com/enquirer/enquirer#prompt-options) in Enquirer.
The prompt object is mostly compatiable with [Inquirer.js](https://github.com/SBoudrias/Inquirer.js/) with some exceptions:

- NOT supports `rawlist`, `expand`, `editor` prompts.
- NOT supports separator.

We also have a few addtional prompt options listed below.

Expand Down
2 changes: 1 addition & 1 deletion lib/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module.exports = class Generator {
}
const cp = await spawnPromise('git', ['init'], cpOpts)
if (cp.exitCode === 0) {
logger.success('Initialized empty Git repository')
logger.success('defaultized empty Git repository')
if (commit) {
await this.gitCommit(commit)
}
Expand Down
57 changes: 57 additions & 0 deletions lib/enquirerCompat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const Enquirer = require('enquirer')

const enquirer = new Enquirer()

function convertOptions(options) {
if (options.type === 'list') {
options.type = 'select'
} else if (options.type === 'checkbox') {
options.type = 'multiselect'
}

options.limit = options.pageSize

const prefix = options.prefix || ''
const suffix = options.suffix || ''
options.message = prefix + options.message + suffix

if (typeof options.when === 'function') {
options.skip = ({ answers }) => options.when(answers)
}

return options
}

function createCompatPrompt(questions = []) {
questions = questions.map(convertOptions)

enquirer.on('prompt', (prompt, answers) => {
if (typeof prompt.options.transformer === 'function') {
prompt.format = input =>
prompt.options.transformer.call(prompt, input, answers, prompt.options)
}

if (typeof prompt.options.filter === 'function') {
prompt.result = input =>
prompt.options.filter.call(prompt, input, answers)
}

if (typeof prompt.options.default === 'function') {
prompt.default = () => prompt.options.default.call(prompt, answers)
}

if (typeof prompt.options.message === 'function') {
prompt.message = () => prompt.options.message.call(prompt, answers)
}

if (typeof prompt.options.validate === 'function') {
prompt.validate = (input, state) =>
prompt.options.validate.call(prompt, input, answers, state)
}
})

return enquirer.prompt(questions)
}

exports.prompt = createCompatPrompt
exports.enquirer = enquirer
3 changes: 3 additions & 0 deletions lib/runAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const logger = require('./logger')
module.exports = async (action, generator) => {
// Not sure which name is better, let's support both for now
if (action.type === 'copy' || action.type === 'add') {
if (!action.cwd) {
throw new Error(`"cwd" is required for "copy" action!`)
}
const stream = majo()
stream.source(action.files, { baseDir: action.cwd })
if (action.filters) {
Expand Down
6 changes: 3 additions & 3 deletions lib/runPrompts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { prompt } = require('enquirer')
const { fs } = require('majo')
const { prompt, enquirer } = require('./enquirerCompat')

module.exports = async (questions, generator) => {
const cacheData = await readCacheFile(generator)
Expand All @@ -10,7 +10,7 @@ module.exports = async (questions, generator) => {
if (cacheAnswers) {
const answer = cacheAnswers[q.name]
if (q.cache && answer !== undefined) {
q.initial = answer
q.default = answer
}
}

Expand All @@ -27,7 +27,7 @@ module.exports = async (questions, generator) => {
})

if (typeof generator.opts.emulator === 'function') {
generator.opts.emulator(prompt)
generator.opts.emulator(enquirer)
}

const answers = await prompt(questions)
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "strictPropertydefaultization": true, /* Enable strict checking of property defaultization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true /* Parse in strict mode and emit "use strict" for each source file. */,

Expand Down

0 comments on commit eb90042

Please sign in to comment.