Skip to content

Make prompts when support function. #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions lib/ask.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ module.exports = function ask (prompts, data, done) {
*/

function prompt (data, key, prompt, done) {
// skip prompts whose when condition is not met
if (prompt.when && !evaluate(prompt.when, data)) {
if (prompt.when && !(typeof prompt.when === 'function' ? prompt.when(data) : evaluate(prompt.when, data))) {
return done()
}

var promptDefault = prompt.default
if (typeof prompt.default === 'function') {
promptDefault = function () {
Expand Down
13 changes: 13 additions & 0 deletions test/e2e/mock-metadata-repo-js/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ module.exports = {
validate: function (input) {
return input === 'custom' ? 'can not input `custom`' : true
}
},
q1: {
type: 'list',
required: true,
label: 'Which choice?',
choices: ['a1', 'a2']
},
q2: {
when: function (answers) {
return answers.q1 === 'a1'
},
label: 'Which choice',
type: 'confirm'
}
},
helpers: {
Expand Down
19 changes: 18 additions & 1 deletion test/e2e/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const extend = Object.assign || require('util')._extend
const generate = require('../../lib/generate')
const metadata = require('../../lib/options')
const { isLocalPath, getTemplatePath } = require('../../lib/local-path')
const ask = require('../../lib/ask')

const MOCK_META_JSON_PATH = path.resolve('./test/e2e/mock-meta-json')
const MOCK_TEMPLATE_REPO_PATH = path.resolve('./test/e2e/mock-template-repo')
Expand Down Expand Up @@ -225,7 +226,23 @@ describe('vue-cli', () => {
expect(getTemplatePath('../template')).to.equal(path.join(__dirname, '/../../../template'))
})

it.only('points out the file in the error', done => {
it('"when" set function', () => {
var meta = require(path.join(MOCK_METADATA_REPO_JS_PATH, 'meta.js'))

var collectQa1 = extend({}, answers, { q1: 'a1', q2: 'aq2' })
monkeyPatchInquirer(collectQa1)
var data = {}
ask(meta.prompts, data, function () {
expect(data).to.have.property('q2')
})
var data2 = {}
var collectQa2 = extend({}, answers, { q1: 'a2', q2: 'aq2' })
monkeyPatchInquirer(collectQa2)
ask(meta.prompts, data2, function () {
expect(data2).to.not.have.property('q2')
})
})
it('points out the file in the error', done => {
monkeyPatchInquirer(answers)
generate('test', MOCK_ERROR, MOCK_TEMPLATE_BUILD_PATH, err => {
expect(err.message).to.match(/^\[readme\.md\] Parse error/)
Expand Down