-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
71 lines (58 loc) · 2.29 KB
/
index.mjs
File metadata and controls
71 lines (58 loc) · 2.29 KB
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
61
62
63
64
65
66
67
68
69
70
71
import path from 'path'
import cp from 'child_process'
import { styleText } from 'node:util'
class TerminalBannerPlugin {
constructor({ emptyLineBefore, emptyLineAfter } = {}) {
this.isDevMode = false
this.putEmptyLineBefore = emptyLineBefore ?? true
this.putEmptyLineAfter = emptyLineAfter ?? true
}
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
}
}
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
}
}
apply = compiler => {
if (compiler.options.mode == 'development') {
this.isDevMode = true
}
compiler.hooks.done.tap('TerminalBannerPlugin', this.compilationDone)
}
compilationDone = () => {
if (!this.isDevMode) {
return
}
const tag = this.getTag() ?? '<unknown>'
const branch = this.getBranch() ?? '<unknown>'
const folder = path.basename(process.cwd())
const topBorder = styleText('green', `┏${'━'.repeat(tag.length + 4)}┯${'━'.repeat(branch.length + 4)}┓`)
const contentLine = styleText('green', `┃ ⌫ ${tag} │ ⎇ ${branch} ┃`)
const bottomBorder = styleText('green', `┗${'━'.repeat(tag.length + 4)}┷${'━'.repeat(branch.length + 4)}┛`)
const folderText = styleText('bgBlue', styleText('whiteBright', ` ${folder} `))
const dateText = styleText('bgRed', styleText('whiteBright', ` ${(new Date).toLocaleTimeString()} `))
setTimeout(() => { //hack: print after all
if (this.putEmptyLineBefore) {
console.log('\n')
}
console.log(`${topBorder}`)
console.log(contentLine)
console.log(bottomBorder)
console.log(`${folderText}${dateText}`)
if (this.putEmptyLineAfter) {
console.log('\n')
}
}, 0)
}
}
export default TerminalBannerPlugin