@nova-lang/cli 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/nova.js +38 -15
- package/install.js +147 -30
- package/package.json +1 -1
package/bin/nova.js
CHANGED
|
@@ -5,26 +5,49 @@ const { spawnSync } = require("child_process");
|
|
|
5
5
|
const path = require("path");
|
|
6
6
|
const fs = require("fs");
|
|
7
7
|
|
|
8
|
+
const ROOT = path.resolve(__dirname, "..");
|
|
9
|
+
|
|
10
|
+
function platformArch() {
|
|
11
|
+
const os = process.platform;
|
|
12
|
+
const a = process.arch;
|
|
13
|
+
const p = os === "darwin" ? "darwin" : os === "linux" ? "linux" : os === "win32" ? "win32" : null;
|
|
14
|
+
const ar = a === "x64" ? "x64" : a === "arm64" ? "arm64" : null;
|
|
15
|
+
if (!p || !ar) return null;
|
|
16
|
+
return p + "-" + ar;
|
|
17
|
+
}
|
|
18
|
+
|
|
8
19
|
function findBinary() {
|
|
9
|
-
const
|
|
20
|
+
const pa = platformArch();
|
|
21
|
+
const names = [];
|
|
10
22
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
23
|
+
if (pa) {
|
|
24
|
+
const base = "nova-" + pa;
|
|
25
|
+
names.push(process.platform === "win32" ? base + ".exe" : base);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
names.push(process.platform === "win32" ? "nova.exe" : "nova");
|
|
29
|
+
|
|
30
|
+
const dirs = [
|
|
31
|
+
path.join(ROOT, "bin"),
|
|
32
|
+
path.join(ROOT, "zig-out", "bin"),
|
|
14
33
|
];
|
|
15
34
|
|
|
16
35
|
if (process.env.NOVA_BINARY) {
|
|
17
|
-
|
|
36
|
+
dirs.unshift(path.dirname(process.env.NOVA_BINARY));
|
|
37
|
+
names.unshift(path.basename(process.env.NOVA_BINARY));
|
|
18
38
|
}
|
|
19
39
|
|
|
20
|
-
for (const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
40
|
+
for (const dir of dirs) {
|
|
41
|
+
for (const name of names) {
|
|
42
|
+
const p = path.join(dir, name);
|
|
43
|
+
try {
|
|
44
|
+
if (fs.statSync(p).isFile()) {
|
|
45
|
+
const mode = fs.statSync(p).mode;
|
|
46
|
+
if (mode & 0o111 || process.platform === "win32") return p;
|
|
47
|
+
}
|
|
48
|
+
} catch {}
|
|
49
|
+
}
|
|
26
50
|
}
|
|
27
|
-
|
|
28
51
|
return null;
|
|
29
52
|
}
|
|
30
53
|
|
|
@@ -32,9 +55,9 @@ function main() {
|
|
|
32
55
|
const binary = findBinary();
|
|
33
56
|
if (!binary) {
|
|
34
57
|
console.error(
|
|
35
|
-
"Nova
|
|
36
|
-
|
|
37
|
-
|
|
58
|
+
"Nova binary not found.\n" +
|
|
59
|
+
" Reinstall: npm install @nova-lang/cli\n" +
|
|
60
|
+
" Or set: NOVA_BINARY=/path/to/nova\n"
|
|
38
61
|
);
|
|
39
62
|
process.exit(1);
|
|
40
63
|
}
|
package/install.js
CHANGED
|
@@ -2,66 +2,183 @@
|
|
|
2
2
|
"use strict";
|
|
3
3
|
|
|
4
4
|
const { execSync, spawnSync } = require("child_process");
|
|
5
|
-
const path = require("path");
|
|
6
5
|
const fs = require("fs");
|
|
6
|
+
const path = require("path");
|
|
7
|
+
const https = require("https");
|
|
8
|
+
const { createHash } = require("crypto");
|
|
7
9
|
|
|
8
10
|
const ROOT = path.resolve(__dirname);
|
|
9
|
-
const BIN_DIR = path.join(ROOT, "
|
|
10
|
-
const
|
|
11
|
-
const
|
|
11
|
+
const BIN_DIR = path.join(ROOT, "bin");
|
|
12
|
+
const PKG = JSON.parse(fs.readFileSync(path.join(ROOT, "package.json"), "utf-8"));
|
|
13
|
+
const VERSION = PKG.version;
|
|
14
|
+
|
|
15
|
+
function platform() {
|
|
16
|
+
const os = process.platform;
|
|
17
|
+
if (os === "darwin") return "darwin";
|
|
18
|
+
if (os === "linux") return "linux";
|
|
19
|
+
if (os === "win32") return "win32";
|
|
20
|
+
throw new Error("unsupported platform: " + os);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function arch() {
|
|
24
|
+
const a = process.arch;
|
|
25
|
+
if (a === "x64") return "x64";
|
|
26
|
+
if (a === "arm64") return "arm64";
|
|
27
|
+
throw new Error("unsupported arch: " + a);
|
|
28
|
+
}
|
|
12
29
|
|
|
13
|
-
function
|
|
30
|
+
function binaryName() {
|
|
31
|
+
const base = ["nova", platform(), arch()].join("-");
|
|
32
|
+
return process.platform === "win32" ? base + ".exe" : base;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function binaryPath() {
|
|
36
|
+
return path.join(BIN_DIR, binaryName());
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function binaryExists() {
|
|
14
40
|
try {
|
|
15
|
-
return fs.statSync(
|
|
41
|
+
return fs.statSync(binaryPath()).isFile();
|
|
16
42
|
} catch {
|
|
17
43
|
return false;
|
|
18
44
|
}
|
|
19
45
|
}
|
|
20
46
|
|
|
47
|
+
function downloadUrl() {
|
|
48
|
+
return [
|
|
49
|
+
"https://github.com/nova-lang/nova/releases/download",
|
|
50
|
+
"v" + VERSION,
|
|
51
|
+
binaryName(),
|
|
52
|
+
].join("/");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function sha256Url() {
|
|
56
|
+
return downloadUrl() + ".sha256";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function download(url, dest) {
|
|
60
|
+
return new Promise((resolve, reject) => {
|
|
61
|
+
const file = fs.createWriteStream(dest);
|
|
62
|
+
https
|
|
63
|
+
.get(url, { timeout: 30000 }, (res) => {
|
|
64
|
+
if (res.statusCode === 302 || res.statusCode === 301) {
|
|
65
|
+
file.close();
|
|
66
|
+
fs.unlinkSync(dest);
|
|
67
|
+
return download(res.headers.location, dest).then(resolve).catch(reject);
|
|
68
|
+
}
|
|
69
|
+
if (res.statusCode !== 200) {
|
|
70
|
+
file.close();
|
|
71
|
+
fs.unlinkSync(dest);
|
|
72
|
+
return reject(new Error("HTTP " + res.statusCode + " for " + url));
|
|
73
|
+
}
|
|
74
|
+
res.pipe(file);
|
|
75
|
+
file.on("finish", () => {
|
|
76
|
+
file.close();
|
|
77
|
+
resolve();
|
|
78
|
+
});
|
|
79
|
+
})
|
|
80
|
+
.on("error", (err) => {
|
|
81
|
+
file.close();
|
|
82
|
+
try { fs.unlinkSync(dest); } catch {}
|
|
83
|
+
reject(err);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function verifyChecksum(filePath, expectedSha256) {
|
|
89
|
+
const hash = createHash("sha256");
|
|
90
|
+
const data = fs.readFileSync(filePath);
|
|
91
|
+
hash.update(data);
|
|
92
|
+
return hash.digest("hex") === expectedSha256.trim();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function downloadBinary() {
|
|
96
|
+
const url = downloadUrl();
|
|
97
|
+
const dest = binaryPath();
|
|
98
|
+
const shaUrl = sha256Url();
|
|
99
|
+
const shaDest = dest + ".sha256";
|
|
100
|
+
|
|
101
|
+
console.log("nova: downloading " + url);
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
await download(shaUrl, shaDest);
|
|
105
|
+
const expected = fs.readFileSync(shaDest, "utf-8").split(/\s+/)[0];
|
|
106
|
+
fs.unlinkSync(shaDest);
|
|
107
|
+
|
|
108
|
+
await download(url, dest);
|
|
109
|
+
fs.chmodSync(dest, 0o755);
|
|
110
|
+
|
|
111
|
+
if (!verifyChecksum(dest, expected)) {
|
|
112
|
+
fs.unlinkSync(dest);
|
|
113
|
+
throw new Error("sha256 mismatch");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
console.log("nova: downloaded -> " + dest);
|
|
117
|
+
return true;
|
|
118
|
+
} catch (err) {
|
|
119
|
+
try { fs.unlinkSync(dest); } catch {}
|
|
120
|
+
try { fs.unlinkSync(shaDest); } catch {}
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
21
125
|
function findZig() {
|
|
22
126
|
try {
|
|
23
127
|
const r = spawnSync("which", ["zig"], { stdio: "pipe" });
|
|
24
|
-
if (r.status === 0)
|
|
25
|
-
return r.stdout.toString().trim();
|
|
26
|
-
}
|
|
128
|
+
if (r.status === 0) return r.stdout.toString().trim();
|
|
27
129
|
} catch {}
|
|
28
130
|
try {
|
|
29
131
|
const r = spawnSync("where", ["zig"], { stdio: "pipe", shell: true });
|
|
30
|
-
if (r.status === 0)
|
|
31
|
-
return r.stdout.toString().trim().split("\n")[0];
|
|
32
|
-
}
|
|
132
|
+
if (r.status === 0) return r.stdout.toString().trim().split("\n")[0];
|
|
33
133
|
} catch {}
|
|
34
134
|
return null;
|
|
35
135
|
}
|
|
36
136
|
|
|
37
|
-
function
|
|
38
|
-
if (alreadyBuilt()) {
|
|
39
|
-
console.log("nova: binary already built at " + BIN_PATH);
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
137
|
+
function buildFromSource() {
|
|
43
138
|
const zig = findZig();
|
|
44
139
|
if (!zig) {
|
|
45
140
|
console.log(
|
|
46
|
-
"nova:
|
|
47
|
-
|
|
48
|
-
|
|
141
|
+
"nova: no pre-built binary for " + platform() + "-" + arch() + ".\n" +
|
|
142
|
+
" Build from source: npm run build (requires Zig compiler)\n" +
|
|
143
|
+
" Or download: https://github.com/nova-lang/nova/releases"
|
|
49
144
|
);
|
|
50
|
-
return;
|
|
145
|
+
return false;
|
|
51
146
|
}
|
|
52
147
|
|
|
53
|
-
console.log("nova: building with " + zig + " ...");
|
|
54
|
-
const result = spawnSync(zig, ["build"], {
|
|
148
|
+
console.log("nova: building from source with " + zig + " ...");
|
|
149
|
+
const result = spawnSync(zig, ["build", "-Doptimize=ReleaseSafe"], {
|
|
55
150
|
cwd: ROOT,
|
|
56
151
|
stdio: "inherit",
|
|
57
152
|
});
|
|
153
|
+
if (result.status !== 0) {
|
|
154
|
+
console.error("nova: build failed");
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
58
157
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
158
|
+
const built = path.join(ROOT, "zig-out", "bin", process.platform === "win32" ? "nova.exe" : "nova");
|
|
159
|
+
const dest = binaryPath();
|
|
160
|
+
if (fs.existsSync(built)) {
|
|
161
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true });
|
|
162
|
+
fs.copyFileSync(built, dest);
|
|
163
|
+
fs.chmodSync(dest, 0o755);
|
|
164
|
+
console.log("nova: built -> " + dest);
|
|
165
|
+
return true;
|
|
64
166
|
}
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async function main() {
|
|
171
|
+
if (binaryExists()) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
fs.mkdirSync(BIN_DIR, { recursive: true });
|
|
176
|
+
|
|
177
|
+
if (await downloadBinary()) return;
|
|
178
|
+
buildFromSource();
|
|
65
179
|
}
|
|
66
180
|
|
|
67
|
-
|
|
181
|
+
main().catch((err) => {
|
|
182
|
+
console.error("nova: install error:", err.message);
|
|
183
|
+
process.exit(1);
|
|
184
|
+
});
|