diff --git a/README.md b/README.md index 0153ece78a..5e20402170 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 (`#`). diff --git a/lib/generate.js b/lib/generate.js index 26447dc8a5..bc73cec05d 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -1,3 +1,4 @@ +var chalk = require('chalk') var Metalsmith = require('metalsmith') var Handlebars = require('handlebars') var async = require('async') @@ -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) { @@ -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