forked from karma-runner/karma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
77 lines (67 loc) · 2.21 KB
/
client.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
const browserify = require('browserify')
const watchify = require('watchify')
const { createWriteStream } = require('fs')
const { readFile } = require('fs').promises
const bundleResourceToFile = (inPath, outPath) => {
return new Promise((resolve, reject) => {
browserify(inPath).bundle()
.once('error', (e) => reject(e))
.pipe(createWriteStream(outPath))
.once('finish', () => resolve())
})
}
const bundleResource = (inPath) => {
return new Promise((resolve, reject) => {
browserify(inPath).bundle((err, buffer) => {
if (err != null) {
reject(err)
return
}
resolve(buffer)
})
})
}
const watchResourceToFile = (inPath, outPath) => {
const b = browserify({
entries: [inPath],
cache: {},
packageCache: {},
plugin: [watchify]
})
const bundle = () => {
b.bundle()
.once('error', (e) => {
console.error(`Failed to bundle ${inPath} into ${outPath}.`)
console.error(e)
})
.pipe(createWriteStream(outPath))
.once('finish', () => console.log(`Bundled ${inPath} into ${outPath}.`))
}
b.on('update', bundle)
bundle()
}
const main = async () => {
if (process.argv[2] === 'build') {
await bundleResourceToFile('client/main.js', 'static/karma.js')
await bundleResourceToFile('context/main.js', 'static/context.js')
} else if (process.argv[2] === 'check') {
const expectedClient = await bundleResource('client/main.js')
const expectedContext = await bundleResource('context/main.js')
const actualClient = await readFile('static/karma.js')
const actualContext = await readFile('static/context.js')
if (Buffer.compare(expectedClient, actualClient) !== 0 || Buffer.compare(expectedContext, actualContext) !== 0) {
// eslint-disable-next-line no-throw-literal
throw 'Bundled client assets are outdated. Forgot to run "npm run build"?'
}
} else if (process.argv[2] === 'watch') {
watchResourceToFile('client/main.js', 'static/karma.js')
watchResourceToFile('context/main.js', 'static/context.js')
} else {
// eslint-disable-next-line no-throw-literal
throw `Unknown command: ${process.argv[2]}`
}
}
main().catch((err) => {
console.error(err)
process.exit(1)
})