|
| 1 | +const browserify = require('browserify') |
| 2 | +const fs = require('fs') |
| 3 | +const { readFile } = require('fs').promises |
| 4 | + |
| 5 | +const bundleResourceToFile = (inPath, outPath) => { |
| 6 | + return new Promise((resolve, reject) => { |
| 7 | + browserify(inPath).bundle() |
| 8 | + .pipe(fs.createWriteStream(outPath)) |
| 9 | + .once('finish', () => resolve()) |
| 10 | + .once('error', (e) => reject(e)) |
| 11 | + }) |
| 12 | +} |
| 13 | + |
| 14 | +const bundleResource = (inPath) => { |
| 15 | + return new Promise((resolve, reject) => { |
| 16 | + browserify(inPath).bundle((err, buffer) => { |
| 17 | + if (err != null) { |
| 18 | + reject(err) |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + resolve(buffer) |
| 23 | + }) |
| 24 | + }) |
| 25 | +} |
| 26 | + |
| 27 | +const main = async () => { |
| 28 | + if (process.argv[2] === 'build') { |
| 29 | + await bundleResourceToFile('client/main.js', 'static/karma.js') |
| 30 | + await bundleResourceToFile('context/main.js', 'static/context.js') |
| 31 | + } else if (process.argv[2] === 'check') { |
| 32 | + const expectedClient = await bundleResource('client/main.js') |
| 33 | + const expectedContext = await bundleResource('context/main.js') |
| 34 | + |
| 35 | + const actualClient = await readFile('static/karma.js') |
| 36 | + const actualContext = await readFile('static/context.js') |
| 37 | + |
| 38 | + if (Buffer.compare(expectedClient, actualClient) !== 0 || Buffer.compare(expectedContext, actualContext) !== 0) { |
| 39 | + // eslint-disable-next-line no-throw-literal |
| 40 | + throw 'Bundled client assets are outdated. Forgot to run "npm run build"?' |
| 41 | + } |
| 42 | + } else { |
| 43 | + // eslint-disable-next-line no-throw-literal |
| 44 | + throw `Unknown command: ${process.argv[2]}` |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +main().catch((err) => { |
| 49 | + console.error(err) |
| 50 | + process.exit(1) |
| 51 | +}) |
0 commit comments