/
index.js
executable file
·42 lines (35 loc) · 1.1 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
'use strict'
const exec = require('child_process').exec
const fs = require('fs')
const port = process.argv[3] || 8080
var target = process.argv[2]
var retryCounter = 0
var budo, reset
if (!target) {
var examples = fs.readdirSync('.')
if (examples) {
examples = examples.filter((val) => !/\..{0,10}$/.test(val))
target = examples[Math.round(Math.random() * (examples.length - 1))]
console.log(`no target passed choosing random example "${target}"`)
}
}
const command = `budo ./${target}/index.js -p ${port} --css bundle.css --live -- -r ./package.json:package.json`
console.log(`running on port "${port}" running example "${target}"`)
function initBudo () {
if (reset) { clearTimeout(reset) }
budo = exec(command, { cwd: './' })
budo.stdout.pipe(process.stdout)
budo.stdout.once('data', () => {
reset = setTimeout(() => {
retryCounter = 0
}, 5e3)
})
budo.stderr.once('data', () => {
if (reset) { clearTimeout(reset) }
retryCounter++
setTimeout(initBudo, Math.min(500 * retryCounter, 5e3))
})
budo.stderr.pipe(process.stderr)
}
module.exports = initBudo
initBudo()