Skip to content

Commit 1ec3ead

Browse files
author
Guillaume Chau
committed
feat(generator): add exitLog api
1 parent e20c49c commit 1ec3ead

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

packages/@vue/cli/lib/Creator.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ module.exports = class Creator {
176176
chalk.cyan(` ${chalk.gray('$')} ${packageManager === 'yarn' ? 'yarn serve' : 'npm run serve'}`)
177177
)
178178
log()
179+
180+
generator.printExitLogs()
179181
}
180182

181183
async promptAndResolvePreset () {

packages/@vue/cli/lib/Generator.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ const GeneratorAPI = require('./GeneratorAPI')
55
const sortObject = require('./util/sortObject')
66
const writeFileTree = require('./util/writeFileTree')
77
const configTransforms = require('./util/configTransforms')
8+
const logger = require('@vue/cli-shared-utils/lib/logger')
9+
10+
const logTypes = {
11+
log: logger.log,
12+
info: logger.info,
13+
done: logger.done,
14+
warn: logger.warn,
15+
error: logger.error
16+
}
817

918
module.exports = class Generator {
1019
constructor (context, pkg, plugins, completeCbs = []) {
@@ -20,6 +29,8 @@ module.exports = class Generator {
2029
this.files = {}
2130
this.fileMiddlewares = []
2231
this.postProcessFilesCbs = []
32+
// exit messages
33+
this.exitLogs = []
2334

2435
const cliService = plugins.find(p => p.id === '@vue/cli-service')
2536
const rootOptions = cliService && cliService.options
@@ -135,4 +146,19 @@ module.exports = class Generator {
135146
return id === _id || id.replace(prefixRE, '') === _id
136147
})
137148
}
149+
150+
printExitLogs () {
151+
if (this.exitLogs.length) {
152+
this.exitLogs.forEach(({ id, msg, type }) => {
153+
const shortId = id.replace('@vue/cli-plugin-', '').replace('vue-cli-plugin-', '')
154+
const logFn = logTypes[type]
155+
if (!logFn) {
156+
logger.error(`Invalid api.exitLog type '${type}'.`, shortId)
157+
} else {
158+
logFn(msg, msg && shortId)
159+
}
160+
})
161+
logger.log()
162+
}
163+
}
138164
}

packages/@vue/cli/lib/GeneratorAPI.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@ class GeneratorAPI {
196196
onCreateComplete (cb) {
197197
this.generator.completeCbs.push(cb)
198198
}
199+
200+
/**
201+
* Add a message to be printed when the generator exits (after any other standard messages).
202+
*
203+
* @param {} msg String or value to print after the generation is completed
204+
* @param {('log'|'info'|'done'|'warn'|'error')} [type='log'] Type of message
205+
*/
206+
exitLog (msg, type = 'log') {
207+
this.generator.exitLogs.push({ id: this.id, msg, type })
208+
}
199209
}
200210

201211
function extractCallDir () {

packages/@vue/cli/lib/invoke.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ async function invoke (pluginName, options = {}, context = process.cwd()) {
125125
}
126126
log(` You should review and commit the changes.`)
127127
log()
128+
129+
generator.printExitLogs()
128130
}
129131

130132
module.exports = (...args) => {

0 commit comments

Comments
 (0)