22
33// @ts -check
44
5- // TODO: Use `yarn pack --json` instead.
6-
7- // This performs `npm pack` and retrieves the list of artifact files from the output.
5+ // This performs `yarn pack --json` and retrieves the list of artifact files from the output.
86//
97// In local dev, invoke it with `-updateArtifactList` to perform a dry run of `npm pack`
10- // and recreate `packages/artifacts.txt `.
8+ // and recreate `packages/artifacts.jsonl `.
119// The exes for all platforms will then be included in the list, even if not present locally.
1210//
13- // In CI, the scripts is invoked without options. It then performs `npm pack` for real,
11+ // In CI, the scripts is invoked without options. It then performs `yarn pack` for real,
1412// recreates the artifact list and verifies that it has no changes compared to the committed state.
1513
16- /**
17- * @typedef {{
18- * path: string,
19- * size: number,
20- * mode: number,
21- * }} PackOutputFile
22- *
23- * @typedef {{
24- * files: PackOutputFile[],
25- * entryCount: number,
26- * bundled: unknown[],
27- * }} PackOutputEntry
28- *
29- * @typedef {[PackOutputEntry] } PackOutput
30- */
31-
32- import { execSync , spawnSync } from "node:child_process" ;
14+ import { execSync , spawn } from "node:child_process" ;
3315import fs from "node:fs" ;
3416import path from "node:path" ;
3517import { projectDir } from "#dev/paths" ;
3618
19+ /**
20+ * @typedef {{ base: string } } YarnPackBase
21+ * @typedef {{ location: string } } YarnPackEntry
22+ * @typedef {{ output: string } } YarnPackResult
23+ *
24+ * @typedef {(
25+ * | YarnPackBase
26+ * | YarnPackEntry
27+ * | YarnPackResult
28+ * )} YarnPackOutput
29+ */
30+
3731const mode = process . argv . includes ( "-updateArtifactList" )
3832 ? "updateArtifactList"
3933 : "package" ;
4034
4135const fileListPath = path . join ( projectDir , "packages" , "artifacts.txt" ) ;
4236
43- const output = spawnSync (
44- `npm pack --json${ mode === "updateArtifactList" ? " --dry-run" : "" } ` ,
37+ /** @type {Set<string> } */
38+ const fileList = new Set ( ) ;
39+
40+ const child = spawn (
41+ "yarn" ,
42+ [
43+ "pack" ,
44+ "--json" ,
45+ mode === "updateArtifactList" ? "--dry-run" : "" ,
46+ ] . filter ( Boolean ) ,
4547 {
4648 cwd : projectDir ,
47- encoding : "utf8 ",
48- shell : true ,
49+ shell : process . platform === "win32 ",
50+ stdio : [ "inherit" , "pipe" , "pipe" ] ,
4951 } ,
50- ) . stdout ;
52+ ) ;
53+
54+ const exitCode = new Promise ( ( res , rej ) => {
55+ child . once ( "close" , code => res ( code ) ) ;
56+ child . once ( "error" , rej ) ;
57+ } ) ;
58+
59+ for await ( const chunk of child . stdout . setEncoding ( "utf8" ) ) {
60+ const lines = /** @type {string } */ ( chunk ) . trim ( ) . split ( / \s / ) ;
61+ for ( const line of lines ) {
62+ /** @type {YarnPackOutput } */
63+ const json = JSON . parse ( line ) ;
64+ if ( "location" in json ) {
65+ fileList . add ( json . location ) ;
66+ }
67+ }
68+ }
5169
52- /** @type {PackOutput } */
53- const parsedOutput = JSON . parse ( output ) ;
54- let filePaths = parsedOutput [ 0 ] . files . map ( file => file . path ) ;
70+ await exitCode ;
5571
56- if ( mode === "updateArtifactList" ) {
57- filePaths = Array . from ( new Set ( filePaths . concat ( getFilesAddedByCI ( ) ) ) ) ;
72+ for await ( const file of getFilesAddedByCI ( ) ) {
73+ fileList . add ( file ) ;
5874}
5975
60- filePaths . sort ( ) ;
61- fs . writeFileSync ( fileListPath , filePaths . join ( "\n" ) ) ;
76+ fs . unlinkSync ( fileListPath ) ;
77+ for ( const file of [ ...fileList ] . toSorted ( ) ) {
78+ fs . appendFileSync ( fileListPath , file + "\n" ) ;
79+ }
6280
6381if ( mode === "package" ) {
6482 execSync ( `git diff --exit-code ${ fileListPath } ` , { stdio : "inherit" } ) ;
6583}
6684
67- function getFilesAddedByCI ( ) {
68- const platforms = [ "darwin" , "darwinarm64" , "linux" , "linuxarm64" , "win32" ] ;
85+ function * getFilesAddedByCI ( ) {
86+ yield "ninja.COPYING" ;
87+
88+ const platforms = [
89+ "darwin" ,
90+ "darwinarm64" ,
91+ "linux" ,
92+ "linuxarm64" ,
93+ "win32" ,
94+ ] ;
6995 const exes = [
7096 "bsb_helper.exe" ,
7197 "bsc.exe" ,
@@ -76,13 +102,9 @@ function getFilesAddedByCI() {
76102 "rewatch.exe" ,
77103 ] ;
78104
79- const files = [ "ninja.COPYING" ] ;
80-
81105 for ( const platform of platforms ) {
82106 for ( const exe of exes ) {
83- files . push ( `${ platform } /${ exe } ` ) ;
107+ yield `${ platform } /${ exe } ` ;
84108 }
85109 }
86-
87- return files ;
88110}
0 commit comments