-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command): add a command method to convert flags to prompts
expose a top level method on command class to convert its flags to an array of inquirer prompt objects. When you need to delegate to some other instance of inquirer, this will make it easy to pass off what would done internally by the the interactive handler.
- Loading branch information
1 parent
eebfd07
commit 5817b34
Showing
12 changed files
with
233 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict' | ||
|
||
const flagType = require('./flag-type') | ||
|
||
module.exports = flagToPrompt | ||
|
||
function flagToPrompt(name, opt) { | ||
const display = name.replace(':', ' ') | ||
return { | ||
'type': flagType(opt) | ||
, 'name': name | ||
, 'message': display + ': ' + (opt.description || '(no description)') | ||
, 'choices': opt.choices | ||
, 'default': opt.default || null | ||
, 'when': opt.when | ||
, 'validate': opt.validate | ||
, 'filter': opt.filter | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
'use strict' | ||
|
||
module.exports = flagType | ||
|
||
function flagType(flag) { | ||
if (Array.isArray(flag.type)) return flagType(unnest(flag)) | ||
if (flag.type === Boolean) return 'confirm' | ||
if (flag.type === Number) return 'number' | ||
if (flag.mask) return 'password' | ||
if (flag.choices) { | ||
if (flag.multi) return 'checkbox' | ||
return 'list' | ||
} | ||
|
||
return 'input' | ||
} | ||
|
||
function unnest(flag) { | ||
const types = flag.type.filter((type) => { | ||
return type !== Array | ||
}) | ||
|
||
return {type: types[0]} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict' | ||
|
||
const {test, threw} = require('tap') | ||
const flagToPrompt = require('../lib/command/flag-to-prompt.js') | ||
|
||
test('flagToPrompt', async (t) => { | ||
|
||
t.test('no description', async (t) => { | ||
const out = flagToPrompt('test:flag', { | ||
type: Boolean | ||
}) | ||
|
||
t.match(out, { | ||
name: 'test:flag' | ||
, type: 'confirm' | ||
, message: 'test flag: (no description)' | ||
, when: undefined | ||
, validate: undefined | ||
, filter: undefined | ||
}) | ||
}) | ||
|
||
t.test('description w/ input functions', async (t) => { | ||
const out = flagToPrompt('foobar', { | ||
type: String | ||
, description: 'hello world' | ||
, multi: true | ||
, choices: ['one'] | ||
, when: () => {} | ||
, validate: () => {} | ||
, filter: () => {} | ||
}) | ||
|
||
t.match(out, { | ||
name: 'foobar' | ||
, type: 'checkbox' | ||
, message: 'foobar: hello world' | ||
, choices: ['one'] | ||
, when: Function | ||
, validate: Function | ||
, filter: Function | ||
}) | ||
}) | ||
}).catch(threw) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict' | ||
|
||
const path = require('path') | ||
const url = require('url') | ||
const {test, threw} = require('tap') | ||
const flagType = require('../lib/command/flag-type') | ||
|
||
test('flagType', async (t) => { | ||
|
||
const cases = [ | ||
[{type: Boolean}, 'confirm', 'Boolean === confirm'] | ||
, [{type: Number}, 'number', 'Number === number'] | ||
, [{type: String}, 'input', 'String === input'] | ||
, [{type: path}, 'input', 'path === input'] | ||
, [{type: url}, 'input', 'url === input'] | ||
, [{type: [Number, Array]}, 'number', '[Number, Array] === number'] | ||
, [{type: String, mask: true}, 'password', 'mask=true === password'] | ||
, [{type: String, choices: []}, 'list', 'choices === list'] | ||
, [{type: String, choices: [], multi: true}, 'checkbox', 'choices + multi === checkbox'] | ||
, [{type: Function}, 'input', 'unexpected type === input'] | ||
] | ||
|
||
for (const [flag, expected, msg] of cases) { | ||
const actual = flagType(flag) | ||
t.equal(actual, expected, msg) | ||
} | ||
}).catch(threw) |
Oops, something went wrong.