Skip to content

Commit 8ec3be1

Browse files
zigomiryyx990803
authored andcommitted
Metalsmith and inquirer
* Use inquirer and metalsmith instead of Khaos and prompt-for. * This should work. * Reuse name var. * Just use dest to preserve current vue behaviour. Document .source option.
1 parent 6f439dc commit 8ec3be1

File tree

3 files changed

+89
-24
lines changed

3 files changed

+89
-24
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ You can also create your own template from scratch:
5959

6060
- All template files will be piped through Handlebars for simple templating - `vue-cli` will automatically infer the prompts based on `{{}}` interpolations found in the files.
6161

62-
- A template repo **may** have a `meta.json` file that provides a schema for the prompts. The schema will be passed to [prompt-for](https://github.com/segmentio/prompt-for#prompt-for) as options. See [example](https://github.com/vuejs-templates/webpack/blob/master/meta.json).
62+
- A template repo **may** have a `meta.json` file that provides a schema for the prompts. The schema will be passed to [inquirer](https://github.com/SBoudrias/Inquirer.js). See [example](https://github.com/vuejs-templates/webpack/blob/master/meta.json). We only support `string` and `boolean` types at the moment.
6363

6464
While developing your template you can test via `vue-cli` with:
6565

bin/vue-init

+82-17
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#!/usr/bin/env node
22

3-
var Khaos = require('khaos-patched')
43
var metadata = require('read-metadata')
4+
var async = require('async')
5+
var Metalsmith = require('metalsmith')
6+
var render = require('consolidate').handlebars.render
57
var download = require('download-git-repo')
68
var program = require('commander')
79
var exists = require('fs').existsSync
810
var path = require('path')
911
var rm = require('rimraf').sync
1012
var uid = require('uid')
1113
var chalk = require('chalk')
12-
var prompt = require('prompt-for-patched')
14+
var inquirer = require('inquirer')
1315
var logger = require('../lib/logger')
1416
var getGitUser = require('../lib/git-user')
1517
var Spinner = require('../lib/spinner')
@@ -62,21 +64,17 @@ var hasSlash = template.indexOf('/') > -1
6264
var rawName = program.args[1]
6365
var inPlace = !rawName || rawName === '.'
6466
var name = inPlace ? path.relative('../', process.cwd()) : rawName
65-
var dir = program.directory
6667
var to = path.resolve(rawName || '.')
6768
var clone = program.clone || false
6869

6970
if (exists(to)) {
70-
prompt({
71-
ok: {
72-
type: 'boolean',
73-
default: true,
74-
label: inPlace
75-
? 'Generate project in current directory?'
76-
: 'Target directory exists. Continue?'
77-
}
78-
}, function (err, answers) {
79-
if (err) throw err
71+
inquirer.prompt([{
72+
type: 'confirm',
73+
message: inPlace
74+
? 'Generate project in current directory?'
75+
: 'Target directory exists. Continue?',
76+
name: 'ok'
77+
}], function (answers) {
8078
if (answers.ok) {
8179
run()
8280
}
@@ -119,6 +117,66 @@ function run () {
119117
}
120118
}
121119

120+
var promptInquirerTypeMapping = {
121+
string: 'input',
122+
boolean: 'confirm'
123+
}
124+
125+
/**
126+
* Prompt plugin.
127+
*
128+
* @param {Object} files
129+
* @param {Metalsmith} metalsmith
130+
* @param {Function} done
131+
*/
132+
133+
function ask (files, metalsmith, done) {
134+
var opts = options(metalsmith._directory + '/..')
135+
136+
var prompts = Object.keys(opts.schema)
137+
var metalsmithMetadata = metalsmith.metadata()
138+
139+
async.eachSeries(prompts, run, done)
140+
141+
function run(key, done) {
142+
var prompt = opts.schema[key]
143+
144+
inquirer.prompt([{
145+
type: promptInquirerTypeMapping[prompt.type],
146+
name: key,
147+
message: prompt.label || key,
148+
default: prompt.default
149+
}], function (answers) {
150+
metalsmithMetadata[key] = answers[key]
151+
done()
152+
})
153+
}
154+
}
155+
156+
/**
157+
* Template in place plugin.
158+
*
159+
* @param {Object} files
160+
* @param {Metalsmith} metalsmith
161+
* @param {Function} done
162+
*/
163+
164+
function renderTemplateFiles (files, metalsmith, done) {
165+
var keys = Object.keys(files)
166+
var metalsmithMetadata = metalsmith.metadata()
167+
168+
async.each(keys, run, done)
169+
170+
function run(file, done){
171+
var str = files[file].contents.toString()
172+
render(str, metalsmithMetadata, function (err, res) {
173+
if (err) return done(err)
174+
files[file].contents = new Buffer(res)
175+
done()
176+
});
177+
}
178+
}
179+
122180
/**
123181
* Generate a template given a `src` and `dest`.
124182
*
@@ -129,11 +187,18 @@ function run () {
129187

130188
function generate (src, dest, fn) {
131189
var template = path.join(src, 'template')
132-
var khaos = new Khaos(template)
133-
var opts = options(src)
134190

135-
khaos.schema(opts.schema)
136-
khaos.generate(dest, fn)
191+
Metalsmith(template)
192+
.use(ask)
193+
.use(renderTemplateFiles)
194+
.clean(false)
195+
.source('.') // start from template root instead of `./src` which is Metalsmith's default for `source`
196+
.destination(dest)
197+
.build(function (err) {
198+
if (err) throw err
199+
200+
fn()
201+
})
137202
}
138203

139204
/**

package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
{
22
"name": "vue-cli",
3-
"version": "1.4.0",
3+
"version": "1.5.0",
44
"description": "A simple CLI for scaffolding Vue.js projects.",
55
"preferGlobal": true,
66
"bin": {
77
"vue": "bin/vue"
88
},
9-
"scripts": {
10-
"test": "mocha"
11-
},
129
"repository": {
1310
"type": "git",
1411
"url": "git+https://github.com/vuejs/vue-cli.git"
@@ -26,11 +23,14 @@
2623
"homepage": "https://github.com/vuejs/vue-cli#readme",
2724
"main": "lib/index.js",
2825
"dependencies": {
26+
"async": "^2.0.0-rc.2",
2927
"chalk": "^1.1.1",
3028
"commander": "^2.9.0",
29+
"consolidate": "^0.14.0",
3130
"download-git-repo": "^0.1.1",
32-
"khaos-patched": "^0.9.3",
33-
"prompt-for-patched": "^1.1.3",
31+
"handlebars": "^4.0.5",
32+
"inquirer": "^0.12.0",
33+
"metalsmith": "^2.1.0",
3434
"read-metadata": "^1.0.0",
3535
"request": "^2.67.0",
3636
"rimraf": "^2.5.0",

0 commit comments

Comments
 (0)