Skip to content

Commit

Permalink
feat: generatorAPI.exitLog (#935)
Browse files Browse the repository at this point in the history
* feat(logger): add tag argument

* feat(generator): add `exitLog` api

* fix(generator): extract toShortId into a util function
  • Loading branch information
Akryum authored and yyx990803 committed Mar 4, 2018
1 parent 43971d8 commit 0f2ee80
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/@vue/cli-shared-utils/lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions packages/@vue/cli/lib/Creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ module.exports = class Creator {
chalk.cyan(` ${chalk.gray('$')} ${packageManager === 'yarn' ? 'yarn serve' : 'npm run serve'}`)
)
log()

generator.printExitLogs()
}

async promptAndResolvePreset () {
Expand Down
27 changes: 27 additions & 0 deletions packages/@vue/cli/lib/Generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ 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 { toShortId } = require('./util/pluginResolution')

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 = []) {
Expand All @@ -20,6 +30,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
Expand Down Expand Up @@ -135,4 +147,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 = toShortId(id)
const logFn = logTypes[type]
if (!logFn) {
logger.error(`Invalid api.exitLog type '${type}'.`, shortId)
} else {
logFn(msg, msg && shortId)
}
})
logger.log()
}
}
}
10 changes: 10 additions & 0 deletions packages/@vue/cli/lib/GeneratorAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,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 () {
Expand Down
2 changes: 2 additions & 0 deletions packages/@vue/cli/lib/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
7 changes: 7 additions & 0 deletions packages/@vue/cli/lib/util/pluginResolution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function toShortId (id) {
id.replace('@vue/cli-plugin-', '').replace('vue-cli-plugin-', '')
}

module.exports = {
toShortId
}

0 comments on commit 0f2ee80

Please sign in to comment.