-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopybinaries.js
100 lines (80 loc) · 2.38 KB
/
copybinaries.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
'use strict';
const {
appendFileSync,
createReadStream,
createWriteStream,
copyFileSync,
mkdirSync,
readdirSync,
readFileSync,
writeFileSync,
} = require('fs');
const { join, resolve } = require('path');
const { createGzip } = require('zlib');
function setBinaryPath(path) {
try {
appendFileSync(process.env.GITHUB_ENV, `binary_path=${path}\n`);
} catch (ex) {
console.error(`Unable to set env variable: ${ex.message}`);
process.exit(1);
}
}
const repoPath = process.env.GITHUB_WORKSPACE;
const [ , , napiVer, platform, libc, binaryType ] = process.argv;
if (typeof napiVer !== 'string' || !napiVer) {
console.error('Missing node API argument');
process.exit(1);
}
if (typeof platform !== 'string' || !platform) {
console.error('Missing platform argument');
process.exit(1);
}
if (typeof libc !== 'string' || !libc) {
console.error('Missing libc argument');
process.exit(1);
}
const baseDir = resolve(join(repoPath, 'build'));
const outDir = join(baseDir, 'prepared');
const addonBaseDir = join(baseDir, 'Release');
let buildConfig = readFileSync(join(baseDir, 'config.gypi'));
buildConfig = buildConfig.slice(buildConfig.indexOf('{'));
buildConfig = JSON.parse(buildConfig).variables;
const {
node_module_version: moduleVer,
target_arch: arch,
} = buildConfig;
const { version } = require(join(repoPath, 'package.json'));
let found = false;
for (const filename of readdirSync(addonBaseDir)) {
if (!/[.]node$/.test(filename))
continue;
found = true;
mkdirSync(outDir, { recursive: true });
const newFilename =
`v${version}-m${moduleVer}-n${napiVer}-${platform}-${libc}-${arch}.node`;
console.log(`${filename} => ${newFilename}`);
const oldFullPath = join(addonBaseDir, filename);
let newFullPath = join(outDir, newFilename);
if (binaryType === 'application/gzip') {
newFullPath = `${newFullPath}.gz`;
const ws = createWriteStream(newFullPath);
ws.on('close', () => {
setBinaryPath(newFullPath);
process.exit(0);
});
createReadStream(oldFullPath).pipe(createGzip({ level: 9 })).pipe(ws);
} else {
try {
copyFileSync(oldFullPath, newFullPath);
} catch (ex) {
console.error(`Unable to copy .node file: ${ex.message}`);
process.exit(1);
}
setBinaryPath(newFullPath);
}
break;
}
if (!found) {
console.error('Did not find any Release .node files');
process.exit(1);
}