-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathpostinstall.js
More file actions
105 lines (91 loc) · 2.59 KB
/
postinstall.js
File metadata and controls
105 lines (91 loc) · 2.59 KB
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
// this is run after npm install
// download pre-made module, if possible & exit 1, or exit 0 to tell system if it needs to build
const { fs } = require("node:fs/promises");
const path = require("node:path");
const fetch = require("cross-fetch");
let targetPath = path.join(
__dirname,
"..",
"build",
"Release",
"node-raylib.node"
);
const { version } = require("../package.json");
function toBuffer(ab) {
const buf = Buffer.alloc(ab.byteLength);
const view = new Uint8Array(ab);
for (let i = 0; i < buf.length; ++i) {
buf[i] = view[i];
}
return buf;
}
async function exists(path) {
try {
await fs.access(path);
return true;
} catch {
return false;
}
}
async function main() {
if (await exists(targetPath)) {
console.log("Found node-raylib.node.");
process.exit(0);
}
let url = `https://github.com/RobLoach/node-raylib/releases/download/v${version}/node-raylib-${process.platform}-${process.arch}.node`;
// we use a single build for both platforms on Mac
if (process.platform === 'darwin') {
url = `https://github.com/RobLoach/node-raylib/releases/download/v${process.env.npm_package_version}/node-raylib-darwin.node`
}
console.log(`Checking for ${url}`)
try {
await fs.mkdir(path.join(__dirname, "..", "build"));
} catch (e) {}
try {
await fs.mkdir(path.join(__dirname, "..", "build", "Release"));
} catch (e) {}
if (
process.platform === "linux" &&
(process.arch === "arm" || process.arch === "arm64")
) {
targetPath = path.join(
__dirname,
"..",
"build",
"Release",
"node-raylib-drm.node"
);
url = `https://github.com/RobLoach/node-raylib/releases/download/v${version}/node-raylib-${process.platform}-${process.arch}-drm.node`;
try {
const data = await fetch(url)
.then((r) => {
if (r.status !== 200) {
throw new Error(`Status: ${r.status}`);
}
return r;
})
.then((r) => r.arrayBuffer());
await fs.writeFile(targetPath, toBuffer(data));
console.log("Found DRM on releases.");
} catch (e) {}
}
try {
const data = await fetch(url)
.then((r) => {
if (r.status !== 200) {
throw new Error(`Status: ${r.status}`);
}
return r;
})
.then((r) => r.arrayBuffer());
await fs.writeFile(targetPath, toBuffer(data));
console.log("Found on releases.");
process.exit(0);
} catch (e) {
console.error(e.message);
}
// couldn't find it, so tell postinstall to compile it
console.log("Not found. Building.");
process.exit(1);
}
main();