forked from fgnass/node-dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
61 lines (47 loc) · 1.68 KB
/
Copy pathutils.js
File metadata and controls
61 lines (47 loc) · 1.68 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
const { spawn } = require('node:child_process')
const { join } = require('node:path')
const touch = require('touch')
const { control } = require('../lib/clear.cjs')
const bin = join(__dirname, '..', 'lib', 'entrypoint')
const dir = join(__dirname, 'fixture')
const reClear = new RegExp(control)
const noop = () => {/**/ }
exports.spawn = (cmd, cb = noop) => {
const ps = spawn('node', [bin].concat(cmd.split(' ')), { cwd: dir })
let err = ''
function errorHandler(data) {
err += data.toString()
outHandler(data)
}
function exitHandler(code, signal) {
if (err) cb(err, code, signal)
}
function outHandler(data) {
// Don't log clear
console.log(data.toString().replace(reClear, ''))
const ret = cb.call(ps, data.toString())
if (typeof ret === 'function') {
// use the returned function as new callback
cb = ret
} else if (ret && ret.exit) {
// kill the process and invoke the given function
ps.removeListener('exit', exitHandler)
ps.once('exit', code => {
ps.stdout.removeListener('data', outHandler)
ps.stderr.removeListener('data', errorHandler)
ret.exit(code)
})
ps.kill('SIGTERM')
}
}
ps.stderr.on('data', errorHandler)
ps.once('exit', exitHandler)
ps.stdout.on('data', outHandler)
return ps
}
// filewatcher requires a new mtime to trigger a change event
// but most file systems only have second precision, so wait
// one full second before touching.
exports.touchFile = (...filepath) => {
setTimeout(() => touch(join(dir, ...filepath)), 1000)
}