11#!/usr/bin/env bun
22import { mkdirSync , readdirSync , statSync , readFileSync , writeFileSync } from 'node:fs' ;
3- import { join , dirname , relative } from 'node:path' ;
3+ import { join , dirname } from 'node:path' ;
44import { fileURLToPath } from 'node:url' ;
55import { brotliCompressSync } from 'node:zlib' ;
66import { createHash } from 'node:crypto' ;
@@ -9,7 +9,7 @@ import { homedir, tmpdir } from 'node:os';
99const __dirname = dirname ( fileURLToPath ( import . meta. url ) ) ;
1010const repoRoot = join ( __dirname , '..' ) ;
1111const resourcesDir = join ( repoRoot , 'src' , 'shared' , 'resources' ) ;
12- const targetPath = join ( resourcesDir , 'embedded-resources.ts ' ) ;
12+ const targetJsonPath = join ( resourcesDir , 'embedded-resources.json ' ) ;
1313const packageJsonPath = join ( repoRoot , 'package.json' ) ;
1414const cacheDirName = 'embedded-resources' ;
1515
@@ -51,7 +51,15 @@ function getCacheBase() {
5151
5252export async function generateEmbeddedResources ( options = { } ) {
5353 const include = options . include ?? DEFAULT_INCLUDE ;
54- const shouldWriteStub = options . writeStub ?? true ;
54+ let shouldWriteJson = options . writeJson ;
55+ if ( shouldWriteJson === undefined && options . writeStub === true ) {
56+ // Backwards compatibility for callers that previously passed writeStub
57+ shouldWriteJson = true ;
58+ }
59+ if ( typeof shouldWriteJson !== 'boolean' ) {
60+ shouldWriteJson = true ;
61+ }
62+ const shouldWriteCache = options . writeCache ?? true ;
5563 const packageJson = JSON . parse ( readFileSync ( packageJsonPath , 'utf8' ) ) ;
5664 const files = [ ] ;
5765
@@ -84,20 +92,13 @@ export async function generateEmbeddedResources(options = {}) {
8492 const base64 = compressed . toString ( 'base64' ) ;
8593 const hash = createHash ( 'sha256' ) . update ( compressed ) . digest ( 'hex' ) ;
8694
87- const fileContents = `// AUTO-GENERATED FILE. DO NOT EDIT.
88- // Run scripts/generate-embedded-resources.mjs to regenerate.
89- import { Buffer } from 'node:buffer';
90-
91- export const EMBEDDED_RESOURCES_VERSION = '${ packageJson . version } ';
92- export const EMBEDDED_RESOURCES_HASH = '${ hash } ';
93- export const EMBEDDED_RESOURCES_SIZE = ${ compressed . length } ;
94-
95- const EMBEDDED_BASE64 = ${ JSON . stringify ( base64 ) } ;
96-
97- export function getEmbeddedResourceArchive(): Buffer {
98- return Buffer.from(EMBEDDED_BASE64, 'base64');
99- }
100- ` ;
95+ const archiveRecord = {
96+ version : packageJson . version ,
97+ generatedAt : payload . generatedAt ,
98+ hash,
99+ size : compressed . length ,
100+ base64,
101+ } ;
101102
102103 const writeCache = ( ) => {
103104 let cacheBase = getCacheBase ( ) ;
@@ -106,12 +107,7 @@ export function getEmbeddedResourceArchive(): Buffer {
106107
107108 try {
108109 mkdirSync ( cacheDir , { recursive : true } ) ;
109- writeFileSync ( cacheFile , JSON . stringify ( {
110- version : packageJson . version ,
111- hash,
112- size : compressed . length ,
113- base64,
114- } , null , 2 ) ) ;
110+ writeFileSync ( cacheFile , JSON . stringify ( archiveRecord , null , 2 ) ) ;
115111 return { cacheFile } ;
116112 } catch ( error ) {
117113 // Fall back to a temp cache if the primary location is not writable
@@ -120,42 +116,48 @@ export function getEmbeddedResourceArchive(): Buffer {
120116 cacheDir = join ( cacheBase , cacheDirName ) ;
121117 cacheFile = join ( cacheDir , 'embedded-resources.json' ) ;
122118 mkdirSync ( cacheDir , { recursive : true } ) ;
123- writeFileSync ( cacheFile , JSON . stringify ( {
124- version : packageJson . version ,
125- hash,
126- size : compressed . length ,
127- base64,
128- } , null , 2 ) ) ;
119+ writeFileSync ( cacheFile , JSON . stringify ( archiveRecord , null , 2 ) ) ;
129120 return { cacheFile } ;
130121 }
131122 throw error ;
132123 }
133124 } ;
134125
135- const { cacheFile } = writeCache ( ) ;
126+ const cacheResult = shouldWriteCache ? writeCache ( ) : { } ;
136127
137- if ( shouldWriteStub ) {
128+ if ( shouldWriteJson ) {
138129 mkdirSync ( resourcesDir , { recursive : true } ) ;
139- writeFileSync ( targetPath , fileContents ) ;
130+ writeFileSync ( targetJsonPath , JSON . stringify ( archiveRecord , null , 2 ) ) ;
140131 }
141132
142133 if ( ! options . quiet ) {
143134 console . log ( `[embed] Embedded ${ files . length } files (${ payloadBuffer . length } bytes, compressed to ${ compressed . length } bytes)` ) ;
144135 console . log ( `[embed] Hash: ${ hash } ` ) ;
145- console . log ( `[embed] Cache file written to ${ cacheFile } ` ) ;
146- if ( ! shouldWriteStub ) {
147- console . log ( '[embed] Skipped updating src/shared/resources/embedded-resources.ts (cache-only)' ) ;
136+ if ( cacheResult . cacheFile ) {
137+ console . log ( `[embed] Cache file written to ${ cacheResult . cacheFile } ` ) ;
138+ }
139+ if ( shouldWriteJson ) {
140+ console . log ( `[embed] JSON archive written to ${ targetJsonPath } ` ) ;
141+ } else {
142+ console . log ( '[embed] Skipped writing src/shared/resources/embedded-resources.json' ) ;
148143 }
149144 }
150145
151- return { hash, size : compressed . length , fileCount : files . length , cacheFile } ;
146+ return {
147+ hash,
148+ size : compressed . length ,
149+ fileCount : files . length ,
150+ cacheFile : cacheResult . cacheFile ,
151+ jsonFile : shouldWriteJson ? targetJsonPath : undefined ,
152+ } ;
152153}
153154
154155if ( import . meta. main ) {
155156 const args = new Set ( process . argv . slice ( 2 ) ) ;
156157 const cacheOnly = args . has ( '--cache-only' ) ;
157- const writeStub = args . has ( '--write-stub' ) ? true : ! cacheOnly ;
158- generateEmbeddedResources ( { writeStub } ) . catch ( ( error ) => {
158+ const writeJson = args . has ( '--no-json' ) ? false : args . has ( '--write-json' ) ? true : ! cacheOnly ;
159+ const writeCache = args . has ( '--no-cache' ) ? false : true ;
160+ generateEmbeddedResources ( { writeJson, writeCache } ) . catch ( ( error ) => {
159161 console . error ( '[embed] Failed to generate embedded resources:' , error ) ;
160162 process . exitCode = 1 ;
161163 } ) ;
0 commit comments