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

Add complete option #334

Merged
merged 2 commits into from
Feb 5, 2017
Merged
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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ vue init ~/fs/path/to-custom-template my-project

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

- `complete`: Instead of using `completeMessage`, you can use a function to run stuffs when the template has been generated.

#### prompts

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:
Expand Down Expand Up @@ -195,6 +197,34 @@ The `skipInterpolation` field in the metadata file should be a [minimatch glob p
}
```

### `complete` function

Arguments:
- `data`: the same data you can access in `completeMessage`:
```js
{
complete(data) {
if (!data.inPlace) {
console.log(`cd ${data.destDirName}`)
}
}
}
```
- `helpers`: some helpers you can use to log results.
- `chalk`: the `chalk` module
- `logger`: [the built-in vue-cli logger](/lib/logger.js)
- `files`: An array of generated files
```js
{
complete(data, {logger, chalk}) {
if (!data.inPlace) {
logger.log(`cd ${chalk.yellow(data.destDirName)}`)
}
}
}
```
}

### Installing a specific template version

`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 (`#`).
Expand Down
11 changes: 9 additions & 2 deletions lib/generate.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var chalk = require('chalk')
var Metalsmith = require('metalsmith')
var Handlebars = require('handlebars')
var async = require('async')
Expand All @@ -7,6 +8,7 @@ var multimatch = require('multimatch')
var getOptions = require('./options')
var ask = require('./ask')
var filter = require('./filter')
var logger = require('./logger')

// register handlebars helper
Handlebars.registerHelper('if_eq', function (a, b, opts) {
Expand Down Expand Up @@ -48,9 +50,14 @@ module.exports = function generate (name, src, dest, done) {
.clean(false)
.source('.') // start from template root instead of `./src` which is Metalsmith's default for `source`
.destination(dest)
.build(function (err) {
.build(function (err, files) {
done(err)
logMessage(opts.completeMessage, data)
if (typeof opts.complete === 'function') {
var helpers = {chalk, logger, files}
opts.complete(data, helpers)
} else {
logMessage(opts.completeMessage, data)
}
})

return data
Expand Down