Skip to content

Commit 67d4d18

Browse files
authored
Add complete option (#334)
* add `complete` option * fix typo
1 parent 40c0526 commit 67d4d18

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ vue init ~/fs/path/to-custom-template my-project
8080

8181
- `completeMessage`: the message to be displayed to the user when the template has been generated. You can include custom instruction here.
8282

83+
- `complete`: Instead of using `completeMessage`, you can use a function to run stuffs when the template has been generated.
84+
8385
#### prompts
8486

8587
The `prompts` field in the metadata file should be an object hash containing prompts for the user. For each entry, the key is the variable name and the value is an [Inquirer.js question object](https://github.com/SBoudrias/Inquirer.js/#question). Example:
@@ -195,6 +197,34 @@ The `skipInterpolation` field in the metadata file should be a [minimatch glob p
195197
}
196198
```
197199

200+
### `complete` function
201+
202+
Arguments:
203+
- `data`: the same data you can access in `completeMessage`:
204+
```js
205+
{
206+
complete(data) {
207+
if (!data.inPlace) {
208+
console.log(`cd ${data.destDirName}`)
209+
}
210+
}
211+
}
212+
```
213+
- `helpers`: some helpers you can use to log results.
214+
- `chalk`: the `chalk` module
215+
- `logger`: [the built-in vue-cli logger](/lib/logger.js)
216+
- `files`: An array of generated files
217+
```js
218+
{
219+
complete(data, {logger, chalk}) {
220+
if (!data.inPlace) {
221+
logger.log(`cd ${chalk.yellow(data.destDirName)}`)
222+
}
223+
}
224+
}
225+
```
226+
}
227+
198228
### Installing a specific template version
199229

200230
`vue-cli` uses the tool [`download-git-repo`](https://github.com/flipxfx/download-git-repo) to download the official templates used. The `download-git-repo` tool allows you to indicate a specific branch for a given repository by providing the desired branch name after a pound sign (`#`).

lib/generate.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var chalk = require('chalk')
12
var Metalsmith = require('metalsmith')
23
var Handlebars = require('handlebars')
34
var async = require('async')
@@ -7,6 +8,7 @@ var multimatch = require('multimatch')
78
var getOptions = require('./options')
89
var ask = require('./ask')
910
var filter = require('./filter')
11+
var logger = require('./logger')
1012

1113
// register handlebars helper
1214
Handlebars.registerHelper('if_eq', function (a, b, opts) {
@@ -48,9 +50,14 @@ module.exports = function generate (name, src, dest, done) {
4850
.clean(false)
4951
.source('.') // start from template root instead of `./src` which is Metalsmith's default for `source`
5052
.destination(dest)
51-
.build(function (err) {
53+
.build(function (err, files) {
5254
done(err)
53-
logMessage(opts.completeMessage, data)
55+
if (typeof opts.complete === 'function') {
56+
var helpers = {chalk, logger, files}
57+
opts.complete(data, helpers)
58+
} else {
59+
logMessage(opts.completeMessage, data)
60+
}
5461
})
5562

5663
return data

0 commit comments

Comments
 (0)