-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·155 lines (135 loc) · 4.21 KB
/
index.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#! /usr/bin/env node
let { homedir } = require('os')
let { existsSync, readFileSync } = require('fs')
let { join } = require('path')
let { spawn } = require('child_process')
let watcher = require('node-watch')
let minimist = require('minimist')
let c = require('picocolors')
let update = require('update-notifier-cjs')
let pkg = require(join(__dirname, 'package.json'))
let prefsPath = join(homedir(), '.trr.json')
let prefs = {}
if (existsSync(prefsPath)) prefs = JSON.parse(readFileSync(prefsPath))
// Update
let boxenOpts = { padding: 1, margin: 1, align: 'center', borderColor: 'green', borderStyle: 'round', dimBorder: true }
update({ pkg, shouldNotifyInNpmScript: true }).notify({ boxenOpts })
// Args
let alias = {
ignore: [ 'i' ],
queue: [ 'q' ],
runOnStart: [ 'r' ],
timeout: [ 't' ],
watch: [ 'w' ],
verbose: [ 'v' ],
}
let boolean = [ 'queue', 'runOnStart', 'verbose' ]
let opts = minimist(process.argv.slice(2), { alias, boolean })
let { _: run } = opts
let ignore = opts.ignore || prefs.ignore || []
let queue = opts.queue || prefs.queue
let timeout = opts.timeout || prefs.timeout || 5
let watch = opts.watch || prefs.watch || process.cwd()
let verbose = opts.verbose || prefs.verbose || false
if (!run.length) {
console.error(c.red('Error:'), 'Please supply a command to run')
process.exit(1)
}
// Set ignore list
ignore = typeof ignore === 'string' ? [ ignore ] : ignore
ignore.unshift('node_modules', '.git')
// Use specified shortcuts in ~/.trr.json
let input
if (prefs.shortcuts && prefs.shortcuts[run[0]]) {
input = prefs.shortcuts[run[0]]
if (run.length > 1) run.slice(1).forEach((arg, i) => {
input = input.replace(`$${i}`, arg)
})
}
else input = run.join(' ')
// Announce
console.log(c.bold(`Watching:`), watch)
console.log(c.bold(`Will run:`), input)
if (verbose) console.log(c.dim(`Ignoring: ${ignore.join(', ')}`))
// Current status
let running = false
let queued = false
// Start
if (prefs.runOnStart) go()
watcher(watch, { recursive: true }, function (event, filename) {
filename = filename.replace(watch, '').substr(1)
if (ignore.some(i => filename.includes(i))) {
if (!filename.includes('node_modules') && verbose) console.log(c.dim(`Ignored: ${filename}`))
return
}
if (verbose) console.log(`${event.charAt(0).toUpperCase() + event.substr(1)}d: ${filename}`)
if (!running) go(filename)
else if (running && queue) queued = true
})
function go (filename) {
console.log() // Break up the runs a bit
let start = Date.now()
let to = timeout * 1000
let lastPrinted
running = true
console.log(c.bold(`Running:`), input)
let cmd, args
// The inclusion of pipe shell syntax necessitates running the command as a bash abstraction
if (input.match(/|/) && !process.platform.startsWith('win')) {
cmd = '/bin/sh'
args = [ '-c', `'${input}'` ]
}
else {
let bits = input.split(' ')
cmd = bits[0]
args = bits.slice(1)
args = args.map(a => {
if (a === '{last}') a = filename
return a
})
}
let options = {
cwd: process.cwd(),
shell: true,
env: {
FORCE_COLOR: true,
...process.env,
},
}
let result = spawn(cmd, args, options)
result.stdout.on('data', data => {
lastPrinted = Date.now()
process.stdout.write(data.toString())
})
result.stderr.on('data', data => {
lastPrinted = Date.now()
process.stderr.write(data.toString())
})
result.on('error', err => {
console.log('Spawn error:', err)
process.exit(1)
})
// Begin inactivity checks
let interval = setInterval(() => {
let timedOut = (Date.now() - lastPrinted) >= to
if (running && timedOut) {
running = false
clearInterval(interval)
console.log(c.dim(`Terminated run due to inactivity for ${to}ms`))
result.kill('SIGINT')
}
}, 100)
result.on('close', code => {
running = false
clearInterval(interval)
let good = code === 0
let status = good ? c.green(c.bold('Success!')) : c.red(c.bold('Failed :('))
console.log(status)
console.log(c.dim(` | '${input}' exited ${good ? '' : 'un'}successfully with code ${code} in ${Date.now() - start}ms`))
if (queue && queued) {
console.log(c.dim(` | Starting queued run`))
queued = false
go()
}
})
}