Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/ask.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ function prompt (data, key, prompt, done) {
message: prompt.message || prompt.label || key,
default: prompt.default,
choices: prompt.choices || [],
validate: prompt.validate || function () { return true }
validate: prompt.validate || function () { return true },
license: prompt.license || 'MIT'
}], function (answers) {
if (Array.isArray(answers[key])) {
data[key] = {}
Expand Down
26 changes: 26 additions & 0 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var metadata = require('read-metadata')
var exists = require('fs').existsSync
var getGitUser = require('./git-user')
var validateName = require('validate-npm-package-name')
var validateLicense = require('validate-npm-package-license')

/**
* Read prompts metadata.
Expand All @@ -18,7 +19,9 @@ module.exports = function options (name, dir) {
: {}

setDefault(opts, 'name', name)
setDefault(opts, 'license', 'MIT')
setValidateName(opts)
setValidateLicense(opts)

var author = getGitUser()
if (author) {
Expand Down Expand Up @@ -52,6 +55,12 @@ function setDefault (opts, key, val) {
}
}

/**
* Set validation for package name
*
* @param {Object} opts
*/

function setValidateName (opts) {
opts.prompts.name.validate = function (name) {
var its = validateName(name)
Expand All @@ -62,3 +71,20 @@ function setValidateName (opts) {
return true
}
}

/**
* Set validation for license
*
* @param {Object} opts
*/

function setValidateLicense (opts) {
opts.prompts.license.validate = function (data) {
var its = validateLicense(data)
if (!its.validForNewPackages) {
var errors = (its.errors || []).concat(its.warnings || [])
return 'Sorry, ' + errors.join(' and ') + '.'
}
return true
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"rimraf": "^2.5.0",
"semver": "^5.1.0",
"uid": "0.0.2",
"validate-npm-package-license": "^3.0.1",
"validate-npm-package-name": "^2.2.2"
},
"devDependencies": {
Expand Down
15 changes: 13 additions & 2 deletions test/e2e/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ describe('vue-cli', () => {
less: true,
sass: true
},
pick: 'no'
pick: 'no',
license: 'MIT'
}

it('template generation', done => {
Expand Down Expand Up @@ -90,7 +91,7 @@ describe('vue-cli', () => {
})
})

it('validate input value', done => {
it('validate invalid name', done => {
// deep copy
var invalidName = extend({}, answers, {name: 'INVALID-NAME'})
monkeyPatchInquirer(invalidName)
Expand All @@ -99,4 +100,14 @@ describe('vue-cli', () => {
done()
})
})

it('validate invalid lisence', done => {
// deep copy
var invalidLicense = extend({}, answers, {license: 'INVALID-LICENSE'})
monkeyPatchInquirer(invalidLicense)
generate('INVALID-LICENSE', MOCK_TEMPLATE_REPO_PATH, MOCK_TEMPLATE_BUILD_PATH, err => {
expect(err).to.be.an('error');
done()
})
})
})