-
Notifications
You must be signed in to change notification settings - Fork 0
/
prebuild.js
executable file
·99 lines (76 loc) · 2.48 KB
/
prebuild.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
#!/usr/bin/env node
const fs = require('fs');
const child_process = require('child_process');
const temp = require('temp').track();
const path = require('path');
function usage() {
console.error("usage: neon-prebuild <target> <addon>");
console.error(" <target> name of target key in neon.prebuilds section of package.json");
console.error(" <addon> path to precompiled addon");
}
function die(msg) {
usage();
if (msg) {
console.error();
console.error(msg);
}
process.exit(1);
}
if (process.argv.length !== 4) {
die();
}
const [ _node, _script, target, addon ] = process.argv;
const manifest = JSON.parse(fs.readFileSync('package.json'));
const version = manifest.version;
const prebuilds = manifest.neon.prebuilds;
const name = prebuilds[target];
if (!name) {
die(`Rust target ${target} not found in package.json`);
}
const LLVM = require('./llvm.json');
const NODE = require('./node.json');
function lookup(target) {
const path = LLVM[target];
if (!path) {
die(`Rust target ${target} not supported`);
}
const [platform, arch, abi] = path;
return NODE[platform][arch][abi];
}
const targetInfo = lookup(target);
const description = `Prebuilt binary package for \`${manifest.name}\` on \`${targetInfo.node}\`.`;
let prebuildManifest = {
name,
description,
version,
os: [targetInfo.platform],
cpu: [targetInfo.arch],
main: "index.node",
files: ["README.md", "index.node"]
};
const OPTIONAL_KEYS = [
'author', 'repository', 'keywords', 'bugs', 'homepage', 'license', 'engines'
];
for (const key of OPTIONAL_KEYS) {
if (manifest[key]) {
prebuildManifest[key] = manifest[key];
}
}
const tmpdir = temp.mkdirSync('neon-');
fs.writeFileSync(path.join(tmpdir, "package.json"), JSON.stringify(prebuildManifest, null, 2));
fs.copyFileSync(addon, path.join(tmpdir, "index.node"));
fs.writeFileSync(path.join(tmpdir, "README.md"), `# \`${name}\`\n\n${description}\n`);
const result = child_process.spawnSync("npm", ["pack", "--json"], {
shell: true,
cwd: tmpdir,
stdio: ['pipe', 'pipe', 'inherit']
});
if (result.status !== 0) {
process.exit(result.status);
}
// FIXME: comment linking to the npm issue this fixes
const tarball = JSON.parse(result.stdout)[0].filename.replace('@', '').replace('/', '-');
// Copy instead of move since e.g. GitHub Actions Windows runners host temp directories
// on a different device (which causes fs.renameSync to fail).
fs.copyFileSync(path.join(tmpdir, tarball), path.join(process.cwd(), tarball));
console.log(tarball);