Skip to content
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

Issue #708: Add '--force' flag to 'vue init' command #709

Closed
wants to merge 1 commit 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
9 changes: 5 additions & 4 deletions bin/vue-init
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ program
.usage('<template-name> [project-name]')
.option('-c, --clone', 'use git clone')
.option('--offline', 'use cached template')

.option('-f, --force', 'skip all prompts')
/**
* Help.
*/

program.on('--help', () => {
console.log()
console.log(' Examples:')
console.log()
console.log(chalk.gray(' # create a new project with an official template'))
Expand Down Expand Up @@ -64,6 +64,7 @@ const inPlace = !rawName || rawName === '.'
const name = inPlace ? path.relative('../', process.cwd()) : rawName
const to = path.resolve(rawName || '.')
const clone = program.clone || false
const force = program.force || program.f || false

const tmp = path.join(home, '.vue-templates', template.replace(/\//g, '-'))
if (program.offline) {
Expand Down Expand Up @@ -105,7 +106,7 @@ function run () {
if (isLocalPath(template)) {
const templatePath = getTemplatePath(template)
if (exists(templatePath)) {
generate(name, templatePath, to, err => {
generate(name, templatePath, to, force, err => {
if (err) logger.fatal(err)
console.log()
logger.success('Generated "%s".', name)
Expand Down Expand Up @@ -150,7 +151,7 @@ function downloadAndGenerate (template) {
download(template, tmp, { clone }, err => {
spinner.stop()
if (err) logger.fatal('Failed to download repo ' + template + ': ' + err.message.trim())
generate(name, tmp, to, err => {
generate(name, tmp, to, force, err => {
if (err) logger.fatal(err)
console.log()
logger.success('Generated "%s".', name)
Expand Down
15 changes: 12 additions & 3 deletions lib/ask.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const async = require('async')
const inquirer = require('inquirer')
const evaluate = require('./eval')
const stdin = require('mock-stdin').stdin()

// Support types from prompt-for which was used before
const promptMapping = {
Expand All @@ -14,11 +15,12 @@ const promptMapping = {
* @param {Object} prompts
* @param {Object} data
* @param {Function} done
* @param {Boolean} force
*/

module.exports = function ask (prompts, data, done) {
module.exports = function ask (prompts, data, done, force) {
async.eachSeries(Object.keys(prompts), (key, next) => {
prompt(data, key, prompts[key], next)
prompt(data, key, prompts[key], next, force)
}, done)
}

Expand All @@ -29,9 +31,10 @@ module.exports = function ask (prompts, data, done) {
* @param {String} key
* @param {Object} prompt
* @param {Function} done
* @param {Boolean} force
*/

function prompt (data, key, prompt, done) {
function prompt (data, key, prompt, done, force) {
// skip prompts whose when condition is not met
if (prompt.when && !evaluate(prompt.when, data)) {
return done()
Expand All @@ -44,6 +47,12 @@ function prompt (data, key, prompt, done) {
}
}

if (force) {
setTimeout(function () {
stdin.send('\n')
}, 0)
}

inquirer.prompt([{
type: promptMapping[prompt.type] || prompt.type,
name: key,
Expand Down
9 changes: 5 additions & 4 deletions lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ Handlebars.registerHelper('unless_eq', function (a, b, opts) {
* @param {String} name
* @param {String} src
* @param {String} dest
* @param {Boolean} force
* @param {Function} done
*/

module.exports = function generate (name, src, dest, done) {
module.exports = function generate (name, src, dest, force, done) {
const opts = getOptions(name, src)
const metalsmith = Metalsmith(path.join(src, 'template'))
const data = Object.assign(metalsmith.metadata(), {
Expand All @@ -50,7 +51,7 @@ module.exports = function generate (name, src, dest, done) {
opts.metalsmith.before(metalsmith, opts, helpers)
}

metalsmith.use(askQuestions(opts.prompts))
metalsmith.use(askQuestions(opts.prompts, force))
.use(filterFiles(opts.filters))
.use(renderTemplateFiles(opts.skipInterpolation))

Expand Down Expand Up @@ -83,9 +84,9 @@ module.exports = function generate (name, src, dest, done) {
* @return {Function}
*/

function askQuestions (prompts) {
function askQuestions (prompts, force) {
return (files, metalsmith, done) => {
ask(prompts, metalsmith.metadata(), done)
ask(prompts, metalsmith.metadata(), done, force)
}
}

Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
"dependencies": {
"async": "^2.4.0",
"chalk": "^2.1.0",
"coffee-script": "1.12.7",
"commander": "^2.9.0",
"consolidate": "^0.14.0",
"download-git-repo": "^1.0.1",
"handlebars": "^4.0.5",
"inquirer": "^3.3.0",
"metalsmith": "^2.1.0",
"minimatch": "^3.0.0",
"mock-stdin": "^0.3.1",
"multimatch": "^2.1.0",
"ora": "^1.3.0",
"read-metadata": "^1.0.0",
Expand All @@ -47,8 +49,7 @@
"tildify": "^1.2.0",
"uid": "0.0.2",
"user-home": "^2.0.0",
"validate-npm-package-name": "^3.0.0",
"coffee-script": "1.12.7"
"validate-npm-package-name": "^3.0.0"
},
"devDependencies": {
"chai": "^4.1.2",
Expand Down