[6a3a178] | 1 | const browserify = require('browserify')
|
---|
| 2 | const watchify = require('watchify')
|
---|
| 3 | const { createWriteStream } = require('fs')
|
---|
| 4 | const { readFile } = require('fs').promises
|
---|
| 5 |
|
---|
| 6 | const bundleResourceToFile = (inPath, outPath) => {
|
---|
| 7 | return new Promise((resolve, reject) => {
|
---|
| 8 | browserify(inPath).bundle()
|
---|
| 9 | .once('error', (e) => reject(e))
|
---|
| 10 | .pipe(createWriteStream(outPath))
|
---|
| 11 | .once('finish', () => resolve())
|
---|
| 12 | })
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 | const bundleResource = (inPath) => {
|
---|
| 16 | return new Promise((resolve, reject) => {
|
---|
| 17 | browserify(inPath).bundle((err, buffer) => {
|
---|
| 18 | if (err != null) {
|
---|
| 19 | reject(err)
|
---|
| 20 | return
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | resolve(buffer)
|
---|
| 24 | })
|
---|
| 25 | })
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | const watchResourceToFile = (inPath, outPath) => {
|
---|
| 29 | const b = browserify({
|
---|
| 30 | entries: [inPath],
|
---|
| 31 | cache: {},
|
---|
| 32 | packageCache: {},
|
---|
| 33 | plugin: [watchify]
|
---|
| 34 | })
|
---|
| 35 |
|
---|
| 36 | const bundle = () => {
|
---|
| 37 | b.bundle()
|
---|
| 38 | .once('error', (e) => {
|
---|
| 39 | console.error(`Failed to bundle ${inPath} into ${outPath}.`)
|
---|
| 40 | console.error(e)
|
---|
| 41 | })
|
---|
| 42 | .pipe(createWriteStream(outPath))
|
---|
| 43 | .once('finish', () => console.log(`Bundled ${inPath} into ${outPath}.`))
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | b.on('update', bundle)
|
---|
| 47 | bundle()
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | const main = async () => {
|
---|
| 51 | if (process.argv[2] === 'build') {
|
---|
| 52 | await bundleResourceToFile('client/main.js', 'static/karma.js')
|
---|
| 53 | await bundleResourceToFile('context/main.js', 'static/context.js')
|
---|
| 54 | } else if (process.argv[2] === 'check') {
|
---|
| 55 | const expectedClient = await bundleResource('client/main.js')
|
---|
| 56 | const expectedContext = await bundleResource('context/main.js')
|
---|
| 57 |
|
---|
| 58 | const actualClient = await readFile('static/karma.js')
|
---|
| 59 | const actualContext = await readFile('static/context.js')
|
---|
| 60 |
|
---|
| 61 | if (Buffer.compare(expectedClient, actualClient) !== 0 || Buffer.compare(expectedContext, actualContext) !== 0) {
|
---|
| 62 | // eslint-disable-next-line no-throw-literal
|
---|
| 63 | throw 'Bundled client assets are outdated. Forgot to run "npm run build"?'
|
---|
| 64 | }
|
---|
| 65 | } else if (process.argv[2] === 'watch') {
|
---|
| 66 | watchResourceToFile('client/main.js', 'static/karma.js')
|
---|
| 67 | watchResourceToFile('context/main.js', 'static/context.js')
|
---|
| 68 | } else {
|
---|
| 69 | // eslint-disable-next-line no-throw-literal
|
---|
| 70 | throw `Unknown command: ${process.argv[2]}`
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | main().catch((err) => {
|
---|
| 75 | console.error(err)
|
---|
| 76 | process.exit(1)
|
---|
| 77 | })
|
---|