-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathrelease.js
163 lines (148 loc) · 4.48 KB
/
release.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
'use strict'
const { exec } = require('child_process')
const { createHash } = require('crypto')
const path = require('path')
const packager = require('electron-packager')
const fs = require('fs-extra')
var glob = require('glob')
var JSZip = require('jszip')
const zlib = require('zlib')
var deterministic = require('deterministic-tar')
var tar = require('tar-fs')
const packageJson = require('../package.json')
let skipPack = false
let binaryPath = null
process.argv.forEach(function (val) {
if (val === '--skip-pack') {
console.log('Skipping packaging')
skipPack = true
}
if (val.startsWith('--binary')) {
binaryPath = val.replace('--binary=', '')
console.log('Using prebuilt binary', binaryPath)
}
})
if (!binaryPath) {
console.error(`\x1b[31mPlease specify a gaia binary for this platform using the "--binary" flag
Example: npm run build:darwin -- --binary=./gaia
\x1b[0m`)
process.exit(1)
}
if (process.env.PLATFORM_TARGET === 'clean') {
require('del').sync(['builds/*', '!.gitkeep'])
console.log('\x1b[33m`builds` directory cleaned.\n\x1b[0m')
} else {
if (skipPack) {
build(process.env.PLATFORM_TARGET)
} else {
pack()
}
}
/**
* Build webpack in production
*/
function pack () {
console.log('\x1b[33mBuilding webpack in production mode...\n\x1b[0m')
let pack = exec('npm run pack')
pack.stdout.on('data', data => console.log(data))
pack.stderr.on('data', data => console.error(data))
pack.on('exit', code => {
if (code === null || code <= 0) {
build(process.env.PLATFORM_TARGET)
}
})
}
/**
* Use electron-packager to build electron app
*/
function build (platform) {
let options = require('../config').building
options.afterCopy = [
copyBinary('gaia', binaryPath)
]
console.log('\x1b[34mBuilding electron app(s)...\n\x1b[0m')
packager(options, async (err, appPaths) => {
if (err) {
console.error('\x1b[31mError from `electron-packager` when building app...\x1b[0m')
console.error(err)
} else {
console.log('Build(s) successful!')
console.log(appPaths)
console.log('\n\x1b[34mZipping files...\n\x1b[0m')
await Promise.all(appPaths.map(async appPath => {
if (platform === 'win32') {
await zipFolder(appPath, options.out, packageJson.version)
} else {
await tarFolder(appPath, options.out, packageJson.version)
.catch(err => console.error(err))
}
}))
console.log('\n\x1b[34mDONE\n\x1b[0m')
}
})
}
function copyBinary (name, binaryLocation) {
return function (buildPath, electronVersion, platform, arch, cb) {
let binPath = path.join(buildPath, 'bin', name)
if (platform === 'win32') {
binPath = binPath + '.exe'
}
fs.copySync(binaryLocation, binPath)
cb()
}
}
function sha256File (path) {
let hash = createHash('sha256')
fs.createReadStream(path).pipe(hash)
return new Promise((resolve, reject) => {
hash.on('data', (hash) =>
resolve(hash.toString('hex')))
})
}
function zipFolder (inDir, outDir, version) {
return new Promise(async (resolve, reject) => {
let name = path.parse(inDir).name
let outFile = path.join(outDir, `${name}_${version}.zip`)
var zip = new JSZip()
await new Promise((resolve) => {
glob(inDir + '/**/*', (err, files) => {
if (err) {
return reject(err)
}
files
.filter(file => !fs.lstatSync(file).isDirectory())
.forEach(file => {
zip.file(path.relative(inDir, file), fs.readFileSync(file), {date: new Date('1987-08-16')}) // make the zip deterministic by changing all file times
})
resolve()
})
})
zip.generateNodeStream({type: 'nodebuffer', streamFiles: true})
.pipe(fs.createWriteStream(outFile))
.on('finish', function () {
sha256File(outFile).then((hash) => {
console.log('Zip successful!', outFile, 'SHA256:', hash)
resolve()
})
})
})
}
function tarFolder (inDir, outDir, version) {
return new Promise(async (resolve, reject) => {
let name = path.parse(inDir).name
let outFile = path.join(outDir, `${name}_${version}.tar.gz`)
// make tar deterministic
tar.pack(inDir)
.pipe(deterministic())
.pipe(zlib.createGzip())
// save tar to disc
.pipe(fs.createWriteStream(outFile))
.on('finish', function () {
console.log('write finished')
sha256File(outFile).then((hash) => {
console.log('Zip successful!', outFile, 'SHA256:', hash)
resolve()
})
})
})
}