forked from mbnuqw/sidebery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
122 lines (104 loc) · 3.2 KB
/
utils.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const fs = require('fs')
const path = require('path')
const ts = require('typescript')
const IS_DEV = process.argv.includes('--dev')
const ADDON_PATH = (IS_DEV && process.env.SIDEBERY_DEV_DIR) || path.resolve('./addon')
const VUE_DIST = IS_DEV ? 'vue.runtime.esm-browser.js' : 'vue.runtime.esm-browser.prod.js'
const FMT_HOST = {
getCanonicalFileName: path => path,
getCurrentDirectory: ts.sys.getCurrentDirectory,
getNewLine: () => ts.sys.newLine,
}
const WATCH_DEBOUNCE_DELAY = 640
function getTime() {
const t = new Date()
return `\x1b[90m${[t.getHours(), t.getMinutes(), t.getSeconds()]
.map(t => `${t}`.padStart(2, '0'))
.join(':')}\x1b[0m`
}
async function treeToList(dir, list = []) {
list.push({ dir })
for await (const d of await fs.promises.opendir(dir)) {
if (d.isDirectory()) await treeToList(path.join(dir, d.name), list)
else if (d.isFile()) list.push({ dir, file: d.name })
}
return list
}
/**
* Watch files
* TODO: use chokidar?
*
* - tasks: { files: ['./path', ...], ... }[]
* - onChange: (affectedTasks) => void
* - onRename: (affectedTask, renamedFileOldPath) => void
*/
function watch(tasks, onChange, onRename) {
const ctx = {}
ctx.timeout = null
ctx.changed = {}
for (const task of tasks) {
if (!task.id) task.id = Math.random().toString(16)
task.watchers = []
for (const file of task.files) {
const watcher = fs.watch(file)
task.watchers.push(watcher)
watcher.addListener('change', changeType => {
if (changeType === 'rename') return onRename(task, file)
ctx.changed[task.id] = task
if (ctx.timeout) clearTimeout(ctx.timeout)
ctx.timeout = setTimeout(() => {
onChange(Object.values(ctx.changed))
ctx.changed = {}
ctx.timeout = null
}, WATCH_DEBOUNCE_DELAY)
})
}
}
return ctx
}
function colorize(str) {
str = str.replace(/\|x\|/g, '\x1b[0m')
str = str.replace(/\|w>/g, '\x1b[37m')
str = str.replace(/\|_>/g, '\x1b[90m')
str = str.replace(/\|r>/g, '\x1b[31m')
str = str.replace(/\|y>/g, '\x1b[33m')
str = str.replace(/\|g>/g, '\x1b[32m')
str = str.replace(/\|b>/g, '\x1b[34m')
str = str.replace(/\|m>/g, '\x1b[35m')
str = str.replace(/\|c>/g, '\x1b[36m')
str = str.replace(/\|B>/g, String.raw`\033[1m`)
str = str.replace(/\|N>/g, String.raw`\033[0m`)
return str
}
function getTSConfig() {
const path = ts.findConfigFile('./', ts.sys.fileExists, 'tsconfig.json')
const readResult = ts.readConfigFile(path, ts.sys.readFile)
if (readResult.error) throw new Error(ts.formatDiagnostic(readResult.error, FMT_HOST))
const jsonConfig = readResult.config
const convertResult = ts.convertCompilerOptionsFromJson(jsonConfig.compilerOptions, './')
if (convertResult.error) throw new Error(ts.formatDiagnostic(convertResult.error, FMT_HOST))
return convertResult.options
}
function log(msg) {
console.log(`${getTime()} ${msg}`)
}
function logOk(msg) {
console.log(`${getTime()} ${`\x1b[32m${msg}\x1b[0m`}`)
}
function logErr(msg) {
console.log(`${getTime()} ${`\x1b[31m${msg}\x1b[0m`}`)
}
module.exports = {
IS_DEV,
ADDON_PATH,
VUE_DIST,
FMT_HOST,
getTime,
treeToList,
watch,
colorize,
getTSConfig,
log,
logOk,
logErr,
}