-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
60 lines (50 loc) · 1.87 KB
/
index.mjs
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
55
56
57
58
59
60
import path from 'path'
import cp from 'child_process'
class TerminalBannerPlugin {
constructor() {
this.isDevMode = false
}
getTag = () => {
try {
return cp.execSync('git describe --abbrev=0 --tags', {encoding: 'utf8'}).toString().trim()
} catch (error) {
console.log('Got error with message:', error.stderr.toString())
return '<unknown>'
}
}
getBranch = () => {
try {
return cp.execSync('git rev-parse --abbrev-ref HEAD', {encoding: 'utf8'}).toString().trim()
} catch (error) {
console.log('Got error with message:', error.stderr.toString())
return '<unknown>'
}
}
apply = compiler => {
if (compiler.options.mode == 'development') {
this.isDevMode = true
}
compiler.hooks.done.tap('TerminalBannerPlugin', this.compilationDone)
}
compilationDone = () => {
if (!this.isDevMode) {
return
}
// COLORS https://stackoverflow.com/questions/9781218/how-to-change-node-jss-console-font-color
const RED = '\x1b[41m'
const GREEN = '\x1b[32m'
const BLUE = '\x1b[44m'
const NC = '\x1b[0m' // no color
const tag = this.getTag()
const branch = this.getBranch()
const folder = path.basename(process.cwd())
const str = `⌫ ${tag} │ ⎇ ${branch}`
setTimeout(() => { //hack: print after all
console.log(`\n${GREEN}┏${'━'.repeat(tag.length + 4)}┯${'━'.repeat(branch.length + 4)}┓`)
console.log(`┃ ${str} ┃`)
console.log(`┗${'━'.repeat(tag.length + 4)}┷${'━'.repeat(branch.length + 4)}┛${NC}`)
console.log(`${BLUE} ${folder} ${NC}${RED} ${(new Date).toLocaleTimeString()} ${NC}\n`)
}, 0)
}
}
export default TerminalBannerPlugin