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" ;
72import crypto from "node:crypto" ;
83import fs from "node:fs/promises" ;
94import path from "node:path" ;
10- import { execFile } from "node:child_process" ;
11- import { fileURLToPath } from "node:url" ;
125import { 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 ) ;
1610if ( ! 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 ;
2218const base = `https://github.com/kekkon-nexus/atypical/releases/download/v${ version } ` ;
2319const 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
8696const 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" ) ) ;
8898main . 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
96103await fs . writeFile ( file , JSON . stringify ( main , null , 2 ) + "\n" ) ;
0 commit comments