Skip to content

Commit 0f2ee80

Browse files
Akryumyyx990803
authored andcommitted
feat: generatorAPI.exitLog (#935)
* feat(logger): add tag argument * feat(generator): add `exitLog` api * fix(generator): extract toShortId into a util function
1 parent 43971d8 commit 0f2ee80

File tree

6 files changed

+59
-9
lines changed

6 files changed

+59
-9
lines changed

Diff for: packages/@vue/cli-shared-utils/lib/logger.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,24 @@ const format = (label, msg) => {
1010
}).join('\n')
1111
}
1212

13-
exports.log = msg => console.log(msg || '')
13+
const chalkTag = msg => chalk.bgBlackBright.white.dim(` ${msg} `)
1414

15-
exports.info = msg => {
16-
console.log(format(chalk.bgBlue.black(' INFO '), msg))
15+
exports.log = (msg = '', tag = null) => tag ? console.log(format(chalkTag(tag), msg)) : console.log(msg)
16+
17+
exports.info = (msg, tag = null) => {
18+
console.log(format(chalk.bgBlue.black(' INFO ') + (tag ? chalkTag(tag) : ''), msg))
1719
}
1820

19-
exports.done = msg => {
20-
console.log(format(chalk.bgGreen.black(' DONE '), msg))
21+
exports.done = (msg, tag = null) => {
22+
console.log(format(chalk.bgGreen.black(' DONE ') + (tag ? chalkTag(tag) : ''), msg))
2123
}
2224

23-
exports.warn = msg => {
24-
console.warn(format(chalk.bgYellow.black(' WARN '), chalk.yellow(msg)))
25+
exports.warn = (msg, tag = null) => {
26+
console.warn(format(chalk.bgYellow.black(' WARN ') + (tag ? chalkTag(tag) : ''), chalk.yellow(msg)))
2527
}
2628

27-
exports.error = msg => {
28-
console.error(format(chalk.bgRed(' ERROR '), chalk.red(msg)))
29+
exports.error = (msg, tag = null) => {
30+
console.error(format(chalk.bgRed(' ERROR ') + (tag ? chalkTag(tag) : ''), chalk.red(msg)))
2931
if (msg instanceof Error) {
3032
console.error(msg.stack)
3133
}

Diff for: packages/@vue/cli/lib/Creator.js

+2
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 () {

Diff for: packages/@vue/cli/lib/Generator.js

+27
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ 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+
const { toShortId } = require('./util/pluginResolution')
10+
11+
const logTypes = {
12+
log: logger.log,
13+
info: logger.info,
14+
done: logger.done,
15+
warn: logger.warn,
16+
error: logger.error
17+
}
818

919
module.exports = class Generator {
1020
constructor (context, pkg, plugins, completeCbs = []) {
@@ -20,6 +30,8 @@ module.exports = class Generator {
2030
this.files = {}
2131
this.fileMiddlewares = []
2232
this.postProcessFilesCbs = []
33+
// exit messages
34+
this.exitLogs = []
2335

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

Diff for: packages/@vue/cli/lib/GeneratorAPI.js

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

202212
function extractCallDir () {

Diff for: packages/@vue/cli/lib/invoke.js

+2
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) => {

Diff for: packages/@vue/cli/lib/util/pluginResolution.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function toShortId (id) {
2+
id.replace('@vue/cli-plugin-', '').replace('vue-cli-plugin-', '')
3+
}
4+
5+
module.exports = {
6+
toShortId
7+
}

0 commit comments

Comments
 (0)