From e20c49cbdf37487ab66c57030d0f510b8346d365 Mon Sep 17 00:00:00 2001 From: Guillaume Chau Date: Sat, 3 Mar 2018 13:13:58 +0100 Subject: [PATCH 1/3] feat(logger): add tag argument --- packages/@vue/cli-shared-utils/lib/logger.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/@vue/cli-shared-utils/lib/logger.js b/packages/@vue/cli-shared-utils/lib/logger.js index 64342a9b64..c03005c421 100644 --- a/packages/@vue/cli-shared-utils/lib/logger.js +++ b/packages/@vue/cli-shared-utils/lib/logger.js @@ -10,22 +10,24 @@ const format = (label, msg) => { }).join('\n') } -exports.log = msg => console.log(msg || '') +const chalkTag = msg => chalk.bgBlackBright.white.dim(` ${msg} `) -exports.info = msg => { - console.log(format(chalk.bgBlue.black(' INFO '), msg)) +exports.log = (msg = '', tag = null) => tag ? console.log(format(chalkTag(tag), msg)) : console.log(msg) + +exports.info = (msg, tag = null) => { + console.log(format(chalk.bgBlue.black(' INFO ') + (tag ? chalkTag(tag) : ''), msg)) } -exports.done = msg => { - console.log(format(chalk.bgGreen.black(' DONE '), msg)) +exports.done = (msg, tag = null) => { + console.log(format(chalk.bgGreen.black(' DONE ') + (tag ? chalkTag(tag) : ''), msg)) } -exports.warn = msg => { - console.warn(format(chalk.bgYellow.black(' WARN '), chalk.yellow(msg))) +exports.warn = (msg, tag = null) => { + console.warn(format(chalk.bgYellow.black(' WARN ') + (tag ? chalkTag(tag) : ''), chalk.yellow(msg))) } -exports.error = msg => { - console.error(format(chalk.bgRed(' ERROR '), chalk.red(msg))) +exports.error = (msg, tag = null) => { + console.error(format(chalk.bgRed(' ERROR ') + (tag ? chalkTag(tag) : ''), chalk.red(msg))) if (msg instanceof Error) { console.error(msg.stack) } From 1ec3ead73ba659df1fcec73c03b84e15d292540d Mon Sep 17 00:00:00 2001 From: Guillaume Chau Date: Sat, 3 Mar 2018 13:14:29 +0100 Subject: [PATCH 2/3] feat(generator): add `exitLog` api --- packages/@vue/cli/lib/Creator.js | 2 ++ packages/@vue/cli/lib/Generator.js | 26 ++++++++++++++++++++++++++ packages/@vue/cli/lib/GeneratorAPI.js | 10 ++++++++++ packages/@vue/cli/lib/invoke.js | 2 ++ 4 files changed, 40 insertions(+) diff --git a/packages/@vue/cli/lib/Creator.js b/packages/@vue/cli/lib/Creator.js index 9b636fd556..300f57e6ba 100644 --- a/packages/@vue/cli/lib/Creator.js +++ b/packages/@vue/cli/lib/Creator.js @@ -176,6 +176,8 @@ module.exports = class Creator { chalk.cyan(` ${chalk.gray('$')} ${packageManager === 'yarn' ? 'yarn serve' : 'npm run serve'}`) ) log() + + generator.printExitLogs() } async promptAndResolvePreset () { diff --git a/packages/@vue/cli/lib/Generator.js b/packages/@vue/cli/lib/Generator.js index a9f35fbfe5..cf69e2c136 100644 --- a/packages/@vue/cli/lib/Generator.js +++ b/packages/@vue/cli/lib/Generator.js @@ -5,6 +5,15 @@ const GeneratorAPI = require('./GeneratorAPI') const sortObject = require('./util/sortObject') const writeFileTree = require('./util/writeFileTree') const configTransforms = require('./util/configTransforms') +const logger = require('@vue/cli-shared-utils/lib/logger') + +const logTypes = { + log: logger.log, + info: logger.info, + done: logger.done, + warn: logger.warn, + error: logger.error +} module.exports = class Generator { constructor (context, pkg, plugins, completeCbs = []) { @@ -20,6 +29,8 @@ module.exports = class Generator { this.files = {} this.fileMiddlewares = [] this.postProcessFilesCbs = [] + // exit messages + this.exitLogs = [] const cliService = plugins.find(p => p.id === '@vue/cli-service') const rootOptions = cliService && cliService.options @@ -135,4 +146,19 @@ module.exports = class Generator { return id === _id || id.replace(prefixRE, '') === _id }) } + + printExitLogs () { + if (this.exitLogs.length) { + this.exitLogs.forEach(({ id, msg, type }) => { + const shortId = id.replace('@vue/cli-plugin-', '').replace('vue-cli-plugin-', '') + const logFn = logTypes[type] + if (!logFn) { + logger.error(`Invalid api.exitLog type '${type}'.`, shortId) + } else { + logFn(msg, msg && shortId) + } + }) + logger.log() + } + } } diff --git a/packages/@vue/cli/lib/GeneratorAPI.js b/packages/@vue/cli/lib/GeneratorAPI.js index a825df262c..8f7717ac1a 100644 --- a/packages/@vue/cli/lib/GeneratorAPI.js +++ b/packages/@vue/cli/lib/GeneratorAPI.js @@ -196,6 +196,16 @@ class GeneratorAPI { onCreateComplete (cb) { this.generator.completeCbs.push(cb) } + + /** + * Add a message to be printed when the generator exits (after any other standard messages). + * + * @param {} msg String or value to print after the generation is completed + * @param {('log'|'info'|'done'|'warn'|'error')} [type='log'] Type of message + */ + exitLog (msg, type = 'log') { + this.generator.exitLogs.push({ id: this.id, msg, type }) + } } function extractCallDir () { diff --git a/packages/@vue/cli/lib/invoke.js b/packages/@vue/cli/lib/invoke.js index 5a8f8a922f..e4ba5374ad 100644 --- a/packages/@vue/cli/lib/invoke.js +++ b/packages/@vue/cli/lib/invoke.js @@ -125,6 +125,8 @@ async function invoke (pluginName, options = {}, context = process.cwd()) { } log(` You should review and commit the changes.`) log() + + generator.printExitLogs() } module.exports = (...args) => { From 54d7209369540e241aa5e28e58b997efa525d168 Mon Sep 17 00:00:00 2001 From: Guillaume Chau Date: Sun, 4 Mar 2018 00:46:25 +0100 Subject: [PATCH 3/3] fix(generator): extract toShortId into a util function --- packages/@vue/cli/lib/Generator.js | 3 ++- packages/@vue/cli/lib/util/pluginResolution.js | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 packages/@vue/cli/lib/util/pluginResolution.js diff --git a/packages/@vue/cli/lib/Generator.js b/packages/@vue/cli/lib/Generator.js index cf69e2c136..c80b4bcb1f 100644 --- a/packages/@vue/cli/lib/Generator.js +++ b/packages/@vue/cli/lib/Generator.js @@ -6,6 +6,7 @@ const sortObject = require('./util/sortObject') const writeFileTree = require('./util/writeFileTree') const configTransforms = require('./util/configTransforms') const logger = require('@vue/cli-shared-utils/lib/logger') +const { toShortId } = require('./util/pluginResolution') const logTypes = { log: logger.log, @@ -150,7 +151,7 @@ module.exports = class Generator { printExitLogs () { if (this.exitLogs.length) { this.exitLogs.forEach(({ id, msg, type }) => { - const shortId = id.replace('@vue/cli-plugin-', '').replace('vue-cli-plugin-', '') + const shortId = toShortId(id) const logFn = logTypes[type] if (!logFn) { logger.error(`Invalid api.exitLog type '${type}'.`, shortId) diff --git a/packages/@vue/cli/lib/util/pluginResolution.js b/packages/@vue/cli/lib/util/pluginResolution.js new file mode 100644 index 0000000000..6896b02daa --- /dev/null +++ b/packages/@vue/cli/lib/util/pluginResolution.js @@ -0,0 +1,7 @@ +function toShortId (id) { + id.replace('@vue/cli-plugin-', '').replace('vue-cli-plugin-', '') +} + +module.exports = { + toShortId +}