From 1e11dfb7a4c389c373d9746e6a05c6878ff2af77 Mon Sep 17 00:00:00 2001 From: Alex Scott Date: Mon, 8 Jan 2018 01:43:00 +0000 Subject: [PATCH] Issue #708: Add '--force' flag to 'vue init' command --- bin/vue-init | 9 +++++---- lib/ask.js | 15 ++++++++++++--- lib/generate.js | 9 +++++---- package-lock.json | 5 +++++ package.json | 5 +++-- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/bin/vue-init b/bin/vue-init index 8a94fa7996..80a349f696 100755 --- a/bin/vue-init +++ b/bin/vue-init @@ -27,12 +27,12 @@ program .usage(' [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')) @@ -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) { @@ -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) @@ -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) diff --git a/lib/ask.js b/lib/ask.js index 9c3be69901..eaa2d10405 100644 --- a/lib/ask.js +++ b/lib/ask.js @@ -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 = { @@ -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) } @@ -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() @@ -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, diff --git a/lib/generate.js b/lib/generate.js index 8493f96f74..368e1c73d3 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -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(), { @@ -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)) @@ -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) } } diff --git a/package-lock.json b/package-lock.json index 8ddb17e550..cc476c4dd1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2217,6 +2217,11 @@ } } }, + "mock-stdin": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/mock-stdin/-/mock-stdin-0.3.1.tgz", + "integrity": "sha1-xlfZZC2QeGQ1xkyl6Zu9TQm9fdM=" + }, "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", diff --git a/package.json b/package.json index 982a300a29..0051a78d54 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "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", @@ -38,6 +39,7 @@ "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", @@ -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",