This repository has been archived by the owner on Mar 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.ts
54 lines (43 loc) · 1.68 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import path from 'path'
import EventEmitter from 'events'
import chalk from 'chalk'
import StackUtils from 'stack-utils'
type StartError = Error | string[] | string | null
export default (taskName: string) => {
const emitter = new EventEmitter()
emitter.on('start', (pluginName: string) => {
console.log(`${chalk.yellow(`${taskName}.${pluginName}`)}: start`)
})
emitter.on('message', (pluginName: string, message: string) => {
console.log(`${chalk.cyan(`${taskName}.${pluginName}`)}: ${message}`)
})
emitter.on('path', (pluginName: string, pathToLog: string) => {
const relativeFile = path.relative(process.cwd(), pathToLog)
console.log(`${chalk.blue(`${taskName}.${pluginName}`)}: ${relativeFile}`)
})
emitter.on('done', (pluginName: string) => {
console.log(`${chalk.green(`${taskName}.${pluginName}`)}: done`)
})
emitter.on('error', (pluginName: string, error: StartError) => {
// hard error
if (error instanceof Error) {
const stackUtils = new StackUtils({
cwd: process.cwd(),
internals: StackUtils.nodeInternals()
})
const stack = stackUtils.clean(error.stack!)
console.error(`${chalk.red(`${taskName}.${pluginName}`)}: ${error.message}`)
console.error(`\n${chalk.red(stack)}`)
// array of "soft" errors
} else if (Array.isArray(error)) {
error.forEach((message) => {
console.error(`${chalk.red(`${taskName}.${pluginName}`)}: ${message}`)
})
// "soft" error
} else if (typeof error === 'string') {
console.error(`${chalk.red(`${taskName}.${pluginName}`)}: ${error}`)
}
console.error(`${chalk.red(`${taskName}.${pluginName}`)}: error`)
})
return emitter
}