-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
65 lines (57 loc) · 1.54 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
61
62
63
64
65
import ora from "ora"
import logSymbols from "log-symbols"
import process from "child_process"
import fs from "fs"
import path from "path"
const useExec = (command) => {
return new Promise((resolve, reject) => {
process.exec(command, (err, stdout, stderr) => {
if (err) {
reject({
err,
stdout,
stderr,
})
}
resolve({
stdout,
stderr,
})
})
})
}
const removeDir = (dir) => {
const files = fs.readdirSync(dir)
files.forEach((file) => {
const newPath = path.join(dir, file)
const stat = fs.statSync(newPath)
if (stat.isDirectory()) {
removeDir(newPath)
} else {
fs.unlinkSync(newPath)
}
})
fs.rmdirSync(dir)
}
const main = async () => {
const spinner = ora("编译中").start()
if (fs.existsSync("./dist")) {
removeDir("./dist")
}
try {
const { stdout: tscStdout, stderr: tscStderr } = await useExec(`tsc`)
if (tscStdout) console.info(tscStdout)
if (tscStderr) console.error(logSymbols.error, tscStderr)
const { stdout: babelStdout, stderr: babelStderr } = await useExec(`babel src -d dist -x ".ts"`)
spinner.stop()
console.log(logSymbols.success, "编译完成")
if (babelStdout) console.info(babelStdout)
if (babelStderr) console.error(logSymbols.error, babelStderr)
} catch (err) {
spinner.stop()
if (err.err) console.error(logSymbols.error, err.err.message)
if (err.stderr) console.error(logSymbols.error, err.stderr)
if (err.stdout) console.info(err.stdout)
}
}
main()