-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuild.js
110 lines (87 loc) · 3.42 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
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
const { exec } = require('pkg');
const Downloader = require('./downloader.js');
const path = require('path');
const copy = require('recursive-copy');
const rimraf = require('rimraf');
const mkdirp = require('mkdirp');
const moveFile = require('move-file');
const fs = require('fs');
const downloadPath = path.join(__dirname, './build/server/chromium');
const buildPath = path.join(__dirname, './build/server');
const binDir = path.join(__dirname, './bin');
async function build() {
console.log('Start build');
let downloader = new Downloader(downloadPath);
let revision = Downloader.defaultRevision();
let platforms = downloader.supportedPlatforms();
await mkDir(downloadPath);
for (let i = 0; i < platforms.length; i++) {
let canDownload = await downloader.canDownloadRevision(platforms[i], revision);
if (canDownload) {
let targetBinDir = path.join(binDir, platforms[i].includes('win') ? 'win' : platforms[i]);
console.log('Download puppeteer for ' + platforms[i]);
await downloader.downloadRevision(platforms[i], revision);
console.log('Download puppeteer for ' + platforms[i] + ' completed');
await mkDir(path.join(targetBinDir, '/chromium'));
let chromiumTarget = path.join(targetBinDir, 'chromium', platforms[i] + '-' + revision);
await rmDir(chromiumTarget);
console.log('Move puppeteer executable to os target');
await moveFile(path.join(downloadPath, platforms[i] + '-' + revision), chromiumTarget);
await rmDir(path.join(targetBinDir, 'cert'));
console.log('Copy https certificates to os target destination');
await copy(path.join(__dirname, 'src', 'cert'), path.join(targetBinDir, 'cert'))
.catch(function(error) {
console.error('Copy failed: ' + error);
});
}
}
console.log('Clean up download path');
await rmDir(downloadPath);
await createExecutables();
await new Promise((resolve, reject) => {
fs.readdir(buildPath, function(err, files) {
if (err) {
reject(err);
}
else {
resolve(files);
}
});
}).then(files => {
return Promise.all(files.map(file => {
let target = '';
if (file.includes('linux')) {
target = 'linux';
}
if (file.includes('macos')) {
target = 'mac';
}
if (file.includes('win')) {
target = 'win';
}
if (target) {
console.log('Move server executable to os target destination (' + target + ')');
return moveFile(path.join(buildPath, file), path.join(binDir, target + '/server' + (target === 'win' ? '.exe' : '')));
}
}));
});
await rmDir(path.join(buildPath, '..'));
}
async function createExecutables() {
console.log('Create server executables');
await exec(['./src/server.js', '--out-path', buildPath]);
}
function mkDir(dir) {
return new Promise((resolve, reject) => {
mkdirp(dir, function(err) {
if (err) reject(err);
else resolve();
});
});
}
function rmDir(dir) {
return new Promise((resolve, reject) => {
rimraf(dir, resolve);
});
}
build().then(() => console.log('Build finished successfully!'));