Skip to content

Commit 1151459

Browse files
ref[mnt]: use bun and typescript scripts
1 parent 13151c8 commit 1151459

10 files changed

Lines changed: 103 additions & 55 deletions

File tree

bun.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

npm/.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/scripts/

npm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"access": "public"
4040
},
4141
"scripts": {
42-
"prepublishOnly": "bun prepublish.mjs"
42+
"prepublishOnly": "bun scripts/prepublish.ts"
4343
},
4444
"optionalDependencies": {},
4545
"engines": {

npm/platforms.mjs

Lines changed: 0 additions & 12 deletions
This file was deleted.

npm/prepublish.mjs

Lines changed: 0 additions & 18 deletions
This file was deleted.

npm/scripts/platforms.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const platforms = {
2+
"linux-x64": {
3+
target: "x86_64-unknown-linux-gnu",
4+
os: "linux",
5+
cpu: "x64",
6+
libc: "glibc",
7+
},
8+
"linux-arm64": {
9+
target: "aarch64-unknown-linux-gnu",
10+
os: "linux",
11+
cpu: "arm64",
12+
libc: "glibc",
13+
},
14+
"linux-x64-musl": {
15+
target: "x86_64-unknown-linux-musl",
16+
os: "linux",
17+
cpu: "x64",
18+
libc: "musl",
19+
},
20+
"linux-arm64-musl": {
21+
target: "aarch64-unknown-linux-musl",
22+
os: "linux",
23+
cpu: "arm64",
24+
libc: "musl",
25+
},
26+
"darwin-x64": {
27+
target: "x86_64-apple-darwin",
28+
os: "darwin",
29+
cpu: "x64",
30+
},
31+
"darwin-arm64": {
32+
target: "aarch64-apple-darwin",
33+
os: "darwin",
34+
cpu: "arm64",
35+
},
36+
"win32-x64": {
37+
target: "x86_64-pc-windows-msvc",
38+
os: "win32",
39+
cpu: "x64",
40+
},
41+
"win32-arm64": {
42+
target: "aarch64-pc-windows-msvc",
43+
os: "win32",
44+
cpu: "arm64",
45+
},
46+
};
47+
48+
export default platforms;
Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,53 @@
1-
// Usage: bun prepare.mjs <version>
2-
//
3-
// Materializes the platform packages under platforms/ from the GitHub
4-
// release assets of v<version> (verifying each sha256), and stamps
5-
// <version> into package.json. optionalDependencies are pinned by
6-
// prepublish.mjs when publishing.
1+
import { execFile } from "node:child_process";
72
import crypto from "node:crypto";
83
import fs from "node:fs/promises";
94
import path from "node:path";
10-
import { execFile } from "node:child_process";
11-
import { fileURLToPath } from "node:url";
125
import { promisify } from "node:util";
13-
import PLATFORMS from "./platforms.mjs";
146

15-
const version = process.argv[2];
7+
import platforms from "./platforms";
8+
9+
const version = process.argv.at(2);
1610
if (!version) {
17-
console.error("usage: bun prepare.mjs <version>");
11+
// oxlint-disable-next-line no-console
12+
console.error("usage: bun scripts/prepare.ts <version>");
13+
// oxlint-disable-next-line unicorn/no-process-exit
1814
process.exit(2);
1915
}
2016

21-
const root = path.dirname(fileURLToPath(import.meta.url));
17+
const root = import.meta.dirname;
2218
const base = `https://github.com/kekkon-nexus/atypical/releases/download/v${version}`;
2319
const extract = promisify(execFile);
2420

25-
async function download(url) {
21+
async function download(url: string) {
2622
const res = await fetch(url);
2723
if (!res.ok) {
2824
throw new Error(`GET ${url} failed: ${res.status} ${res.statusText}`);
2925
}
3026
return Buffer.from(await res.arrayBuffer());
3127
}
3228

33-
async function materialize(platform, { target, os, cpu, libc }) {
29+
async function materialize(
30+
platform: string,
31+
{
32+
target,
33+
os,
34+
cpu,
35+
libc,
36+
}: {
37+
target: string;
38+
os: string;
39+
cpu: string;
40+
libc?: string;
41+
},
42+
) {
3443
const stem = `atypical-commit-v${version}-${target}`;
3544
const archive = `${stem}.${os === "win32" ? "zip" : "tar.gz"}`;
3645
const [data, checksum] = await Promise.all([
3746
download(`${base}/${archive}`),
3847
download(`${base}/${stem}.sha256`),
3948
]);
4049

41-
const expected = checksum.toString().trim().split(/\s+/)[0];
50+
const [expected] = checksum.toString().trim().split(/\s+/);
4251
const actual = crypto.createHash("sha256").update(data).digest("hex");
4352
if (actual !== expected) {
4453
throw new Error(`${archive}: checksum mismatch (${actual} != ${expected})`);
@@ -51,8 +60,7 @@ async function materialize(platform, { target, os, cpu, libc }) {
5160
const file = path.join(dir, archive);
5261
await fs.writeFile(file, data);
5362
try {
54-
const [cmd, ...args] =
55-
os === "win32" ? ["unzip", "-o", archive] : ["tar", "-xf", archive];
63+
const [cmd, ...args] = os === "win32" ? ["unzip", "-o", archive] : ["tar", "-xf", archive];
5664
await extract(cmd, args, { cwd: dir });
5765
} finally {
5866
await fs.rm(file);
@@ -62,6 +70,7 @@ async function materialize(platform, { target, os, cpu, libc }) {
6270

6371
await fs.writeFile(
6472
path.join(dir, "package.json"),
73+
// oxlint-disable-next-line prefer-template
6574
JSON.stringify(
6675
{
6776
name,
@@ -80,17 +89,15 @@ async function materialize(platform, { target, os, cpu, libc }) {
8089
2,
8190
) + "\n",
8291
);
92+
// oxlint-disable-next-line no-console
8393
console.log(`${name}@${version} <- ${archive}`);
8494
}
8595

8696
const file = path.join(root, "package.json");
87-
const main = JSON.parse(await fs.readFile(file));
97+
const main = JSON.parse(await fs.readFile(file, "utf8"));
8898
main.version = version;
8999

90-
await Promise.all(
91-
Object.entries(PLATFORMS).map(([platform, spec]) =>
92-
materialize(platform, spec),
93-
),
94-
);
100+
await Promise.all(Object.entries(platforms).map(([platform, spec]) => materialize(platform, spec)));
95101

102+
// oxlint-disable-next-line prefer-template
96103
await fs.writeFile(file, JSON.stringify(main, null, 2) + "\n");

npm/scripts/prepublish.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import fs from "node:fs/promises";
2+
import path from "node:path";
3+
4+
import platforms from "./platforms";
5+
6+
const file = path.join(import.meta.dirname, "package.json");
7+
const main = JSON.parse(await fs.readFile(file, "utf8"));
8+
9+
main.optionalDependencies = Object.fromEntries(
10+
Object.keys(platforms).map((platform) => [`${main.name}-${platform}`, main.version]),
11+
);
12+
13+
// oxlint-disable-next-line prefer-template
14+
await fs.writeFile(file, JSON.stringify(main, null, 2) + "\n");

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
],
88
"devDependencies": {
99
"@kekkon-nexus/config": "2.260712.0",
10+
"@types/bun": "latest",
1011
"oxfmt": "^0.58.0",
1112
"oxlint": "^1.73.0"
1213
}

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"compilerOptions": {
55
"allowJs": true,
66
"checkJs": true,
7-
"skipLibCheck": true
7+
"skipLibCheck": true,
8+
9+
"noEmit": true
810
}
911
}

0 commit comments

Comments
 (0)