This repository has been archived by the owner on Feb 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.mjs
executable file
·111 lines (95 loc) · 2.95 KB
/
cli.mjs
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
#!/usr/bin/env node
// @ts-check
import fsStreamable from 'node:fs'
import fs from 'node:fs/promises'
import path from 'node:path'
import config from './config.json' assert { type: 'json' }
import { spawn } from 'node:child_process'
import { Transform } from 'readable-stream'
import { fileURLToPath } from 'url'
import kill from '@nichoth/tree-kill'
import esbuild from 'esbuild'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const target = path.resolve(path.join(config.target, 'bundle.js'))
const writeStream = fsStreamable.createWriteStream(target)
let child
const transformer = new Transform({
transform (_chunk, _, cb) {
const chunk = _chunk.toString()
if (chunk.includes('# ok')) {
kill(child.pid)
setTimeout(() => {
process.exit(0)
}, 100)
}
if (chunk.includes('# fail ')) {
kill(child.pid)
setTimeout(() => {
process.exit(1)
}, 100)
}
if (chunk.includes('Exiting Window#0 (code=1)')) {
setTimeout(() => {
process.exit(1)
}, 100)
}
this.push(chunk)
cb()
}
})
const i = process.argv.findIndex(arg => arg.includes('--html'))
const html = process.argv[i]
if (html) {
// need a custom html file, so write the html to target
// parse the file name
let filename
if (html.includes('=')) {
filename = html.split('=')[1]
} else {
filename = process.argv[i + 1]
}
await cp(
path.join(process.cwd(), filename),
path.join(path.join(config.target, 'index.html'))
)
}
//
// build listener for uncaught errors, then add it to the user's test code
//
esbuild.build({
entryPoints: [path.join(__dirname, 'index.mjs')],
bundle: true,
keepNames: true,
external: ['socket:*'],
format: 'esm',
logLevel: 'silent',
// found this via [source code](https://github.com/evanw/esbuild/blob/a7eb7891ec1aeb7f7967ae38d72ab96518913e62/lib/shared/types.ts#L212)
write: false
}).then(res => {
// https://github.com/evanw/esbuild/issues/496#issue-733010073
const code = new TextDecoder('utf-8').decode(res.outputFiles[0].contents)
writeStream.write(code, (err) => {
// done writing `index.mjs`, now write `stdin` to `bundle.js`
if (err) throw err
process.stdin
.pipe(writeStream)
.on('close', () => {
// have written the file, now run the tests
child = spawn('npx', ['ssc', 'run', '--headless'], {
cwd: __dirname
})
child.stdout
.pipe(transformer)
.pipe(process.stdout)
child.stderr.pipe(process.stderr)
})
})
})
function cp (a, b) {
return fs.cp(
path.resolve(a),
path.resolve(b),
{ recursive: true, force: true }
)
}