/
runner-prepare.mjs
107 lines (89 loc) · 3.03 KB
/
runner-prepare.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
import { exec } from 'child_process'
import { promises } from 'fs'
import { resolve } from 'path'
import { fileURLToPath } from 'url'
import util from '../../lib/ours/util.js'
const { copyFile, mkdir, rmdir } = promises
function highlightFile(file) {
return `\x1b[33m${file.replace(process.cwd() + '/', '')}\x1b[0m`
}
function info(message) {
console.log(`\x1b[34m[INFO]\x1b[0m ${message}`)
}
function error(message) {
console.log(`\x1b[31m[INFO]\x1b[0m ${message}`)
}
async function run(command) {
info(`Executing \x1b[33m${command}\x1b[0m ...`)
const { promise, reject, resolve } = util.createDeferredPromise()
let hasOutput = false
function logOutput(chunk) {
if (!hasOutput) {
hasOutput = true
console.log('')
}
console.log(chunk.toString('utf-8').trim().replace(/^/gm, ' '))
}
try {
const process = exec(command, { stdio: 'pipe' }, (error) => {
if (error) {
return reject(error)
}
resolve(error)
})
process.stdout.on('data', logOutput)
process.stderr.on('data', logOutput)
await promise
if (hasOutput) {
console.log('')
}
} catch (e) {
if (hasOutput) {
console.log('')
}
error(`Command failed with status code ${e.code}.`)
process.exit(1)
}
}
async function main() {
const validBundlers = ['browserify', 'esbuild', 'rollup', 'webpack']
const bundler = process.argv[2] || process.env.BUNDLER
if (!validBundlers.includes(bundler)) {
error(`Usage: node await runner-prepare.mjs [${validBundlers.join('|')}]`)
error('You can also use the BUNDLER environment variable.')
process.exit(1)
}
const rootDir = resolve(fileURLToPath(new URL('.', import.meta.url)), `../../tmp/${bundler}`)
const sourceIndex = resolve(fileURLToPath(new URL('.', import.meta.url)), '../../test/browser/fixtures/index.html')
const targetIndex = resolve(rootDir, 'index.html')
info(`Emptying directory ${highlightFile(rootDir)} ...`)
try {
await rmdir(rootDir, { recursive: true })
} catch (e) {
// No-op
}
await mkdir(rootDir, { recursive: true })
info(`Copying file ${highlightFile(sourceIndex)} to ${highlightFile(targetIndex)} ...`)
await copyFile(sourceIndex, targetIndex)
switch (bundler) {
case 'browserify':
await run('browserify test/browser/test-browser.js -o tmp/browserify/suite.browser.js')
await run('browserify test/browser/test-browser.js --node -o tmp/browserify/suite.node.js')
break
case 'esbuild':
await run('node src/test/browser/fixtures/esbuild.browser.config.mjs')
await run('node src/test/browser/fixtures/esbuild.node.config.mjs')
break
case 'rollup':
await run('rollup -c test/browser/fixtures/rollup.browser.config.mjs')
await run('rollup -c test/browser/fixtures/rollup.node.config.mjs')
break
case 'webpack':
await run('webpack -c test/browser/fixtures/webpack.browser.config.mjs')
await run('webpack -c test/browser/fixtures/webpack.node.config.mjs')
}
}
main().catch((e) => {
error(e)
process.exit(1)
})