-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev.js
79 lines (71 loc) · 2.08 KB
/
dev.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
/**
* Concurrently run our various dev tasks.
*
* Usage: node dev
**/
const app = require('.')
, chalk = require('chalk'), {bold} = chalk
, {red, green, blue, cyan, yellow} = bold
, dev = module.exports = () => run({
server: task(app.package.scripts.start, {color: blue}),
build: task(app.package.scripts['build-watch'], {color: green}),
lint: task(app.package.scripts['lint-watch'], {color: cyan}),
test: task(app.package.scripts['test-watch'], {color: yellow})
})
const taskEnvironment = (path = require('path')) => {
const env = {}
for (const key in process.env) {
env[key] = process.env[key]
}
Object.assign(env, {
NODE_ENV: 'development',
PATH: [ path.join(app.root, 'node_modules', '.bin')
, process.env.PATH ].join(path.delimiter)
})
return env
}
function run(tasks) {
Object.keys(tasks)
.map(name => tasks[name](name))
}
function task(command, {
spawn = require('child_process').spawn,
path = require('path'),
color
} = {}) {
return name => {
const stdout = log({name, color}, process.stdout)
, stderr = log({name, color, text: red}, process.stderr)
, proc = spawn(command, {
shell: true,
stdio: 'pipe',
env: taskEnvironment(),
}).on('error', stderr)
.on('exit', (code, signal) => {
stderr(`Exited with code ${code}`)
if (signal) stderr(`Exited with signal ${signal}`)
})
proc.stdout.on('data', stdout)
proc.stderr.on('data', stderr)
}
}
function log({
name,
ts = timestamp,
color = none,
text = none,
}, out = process.stdout) {
return data => data.toString()
// Strip out screen-clearing control sequences, which really
// muck up the output.
.replace('\u001b[2J', '')
.replace('\u001b[1;3H', '')
.split('\n')
.forEach(line => out.write(`${color(`${ts()} ${name} \t⎹ `)}${text(line)}\n`))
}
const dateformat = require('dateformat')
function timestamp() {
return dateformat('yyyy-mm-dd HH:MM:ss (Z)')
}
function none(x) { return x }
if (module === require.main) { dev() }