-
Notifications
You must be signed in to change notification settings - Fork 14
/
build.js
52 lines (33 loc) · 1.46 KB
/
build.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
const args = require('yargs')
.option('target')
.demandOption('target')
.parse(process.argv.slice(2))
try {
const { name } = require('./package.json')
const OUTPUT_MAP = {
linux: `./dist/${name}-linux`,
darwin: `./dist/${name}-macos`,
win32: `./dist/${name}-windows.exe`
}
const packageData = require('./package.json')
const { target } = args
const targetKey = `pkg-${target}`
const pkgConfig = packageData[targetKey]
if (!pkgConfig) throw new Error(`Invalid target, must supply ${targetKey} in the package.json`)
packageData.pkg = pkgConfig
console.log('Preparing package.json')
require('fs').writeFileSync('./package.json', JSON.stringify(packageData, null, ' '))
console.log('Rebuilding dependencies for', target)
require('child_process').execSync(`npm i --target_arch=x64 --target_platform=${target}`, { stdio: ['pipe', 'pipe', 'pipe'] })
const outputName = OUTPUT_MAP[target] || pkgConfig.output
if (!outputName) throw new Error('Must specify appropriate output name in config')
console.log('Compiling, output to', outputName)
require('child_process').execSync(`pkg ./ --output ${outputName} --compress Brotli`, { stdio: ['pipe', 'pipe', 'pipe'] })
console.log('Cleaning package.json')
delete packageData.pkg
require('fs').writeFileSync('./package.json', JSON.stringify(packageData, null, ' '))
console.log('Done!')
} catch (e) {
if (e.stdout) console.error(e.stdout.toString('utf8'))
else throw e
}