-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.js
68 lines (59 loc) · 2.1 KB
/
tasks.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
//------------------------------------------------------
// task helper scripts:
//------------------------------------------------------
const shell = (command) => new Promise((resolve, reject) => {
const { spawn } = require('child_process')
const windows = /^win/.test(process.platform)
console.log(`\x1b[32m${command}\x1b[0m` )
const ls = spawn(windows ? 'cmd' : 'sh', [windows ? '/c' : '-c', command] )
ls.stdout.pipe(process.stdout)
ls.stderr.pipe(process.stderr)
ls.on('close', (code) => resolve(code))
})
const watch = (directory, func) => new Promise((resolve, reject) => {
const fs = require("fs")
const path = require("path")
fs.watch(directory, func)
const paths = fs.readdirSync(directory).map(n => path.join(directory, n))
const stats = paths.map(n => ({path: n, stat: fs.statSync(n)}))
const dirs = stats.filter(stat => stat.stat.isDirectory())
return Promise.all([dirs.map(dir => watch(dir.path, func))])
})
const cli = async (args, tasks) => {
const task = (args.length === 3) ? args[2] : "none"
const func = (tasks[task]) ? tasks[task] : () => {
console.log("tasks:")
Object.keys(tasks).forEach(task => console.log(` - ${task}`))
}; await func()
}
//------------------------------------------------------
// constants:
//------------------------------------------------------
const TYPESCRIPT_SRC = "tsc-bundle --project ./src/tsconfig.json"
//------------------------------------------------------
// tasks:
//------------------------------------------------------
const clean = async () => {
await shell("shx rm -rf ./dist/index.js")
await shell("shx rm -rf ./node_modules")
}
const build = async () => {
await shell("npm install")
await shell(`${TYPESCRIPT_SRC}`)
}
const start = async () => {
await shell("npm install")
await shell(`${TYPESCRIPT_SRC}`)
await Promise.all([
shell(`${TYPESCRIPT_SRC} --watch > /dev/null`),
shell("fsweb ./dist 5000")
])
}
//------------------------------------------------------
// cli:
//------------------------------------------------------
cli(process.argv, {
clean,
build,
start
}).catch(console.log)