@@ -3,6 +3,7 @@ import { spawnSync } from "node:child_process";
33import fs from "node:fs/promises" ;
44import path from "node:path" ;
55import { resolvePnpmRunner } from "../pnpm-runner.mjs" ;
6+ import { stageCodexAppServerProtocolArtifacts } from "./codex-app-server-protocol-artifacts.js" ;
67
78const PROTOCOL_SCHEMA_RELATIVE_PATH = "codex-rs/app-server-protocol/schema" ;
89const DEFAULT_PROTOCOL_GENERATION_MIN_FREE_BYTES = 10 * 1024 * 1024 * 1024 ;
@@ -171,8 +172,7 @@ export async function generateExperimentalCodexAppServerProtocolSource(
171172 try {
172173 await assertCodexProtocolGenerationHeadroom ( { codexRepo, repoRoot } ) ;
173174 runCargoProtocolGenerator ( codexRepo , buildCodexProtocolExportArgs ( manifestPath , generatedRoot ) ) ;
174- await splitGeneratedProtocolOutput ( generatedRoot , { jsonRoot, typescriptRoot } ) ;
175- await rewriteTypeScriptImports ( typescriptRoot ) ;
175+ await stageCodexAppServerProtocolArtifacts ( generatedRoot , { jsonRoot, typescriptRoot } ) ;
176176 formatGeneratedTypeScript ( repoRoot , typescriptRoot ) ;
177177 } catch ( error ) {
178178 await cleanup ( ) ;
@@ -316,47 +316,6 @@ async function resolveExistingStatfsPath(targetPath: string): Promise<string> {
316316 }
317317}
318318
319- async function splitGeneratedProtocolOutput (
320- sourceRoot : string ,
321- roots : { jsonRoot : string ; typescriptRoot : string } ,
322- ) : Promise < void > {
323- await copyGeneratedProtocolFiles ( sourceRoot , sourceRoot , roots ) ;
324- }
325-
326- async function copyGeneratedProtocolFiles (
327- sourceRoot : string ,
328- currentRoot : string ,
329- roots : { jsonRoot : string ; typescriptRoot : string } ,
330- ) : Promise < void > {
331- const entries = await fs . readdir ( currentRoot , { withFileTypes : true } ) ;
332- await Promise . all (
333- entries . map ( async ( entry ) => {
334- const sourcePath = path . join ( currentRoot , entry . name ) ;
335- if ( entry . isDirectory ( ) ) {
336- await copyGeneratedProtocolFiles ( sourceRoot , sourcePath , roots ) ;
337- return ;
338- }
339- if ( ! entry . isFile ( ) ) {
340- return ;
341- }
342-
343- const relativePath = path . relative ( sourceRoot , sourcePath ) ;
344- const targetRoot = entry . name . endsWith ( ".ts" )
345- ? roots . typescriptRoot
346- : entry . name . endsWith ( ".json" )
347- ? roots . jsonRoot
348- : null ;
349- if ( targetRoot === null ) {
350- return ;
351- }
352-
353- const targetPath = path . join ( targetRoot , relativePath ) ;
354- await fs . mkdir ( path . dirname ( targetPath ) , { recursive : true } ) ;
355- await fs . copyFile ( sourcePath , targetPath ) ;
356- } ) ,
357- ) ;
358- }
359-
360319function runCargoProtocolGenerator ( codexRepo : string , args : string [ ] ) : void {
361320 const result = spawnSync ( "cargo" , args , {
362321 cwd : codexRepo ,
@@ -399,31 +358,6 @@ function formatGeneratedTypeScript(repoRoot: string, root: string): void {
399358 }
400359}
401360
402- async function rewriteTypeScriptImports ( root : string ) : Promise < void > {
403- const entries = await fs . readdir ( root , { withFileTypes : true } ) ;
404- await Promise . all (
405- entries . map ( async ( entry ) => {
406- const fullPath = path . join ( root , entry . name ) ;
407- if ( entry . isDirectory ( ) ) {
408- await rewriteTypeScriptImports ( fullPath ) ;
409- return ;
410- }
411- if ( ! entry . isFile ( ) || ! entry . name . endsWith ( ".ts" ) ) {
412- return ;
413- }
414- const text = await fs . readFile ( fullPath , "utf8" ) ;
415- await fs . writeFile ( fullPath , normalizeGeneratedTypeScript ( text ) ) ;
416- } ) ,
417- ) ;
418- }
419-
420- function normalizeGeneratedTypeScript ( text : string ) : string {
421- return text
422- . replace ( / ( f r o m \s + [ " ' ] ) ( \. { 1 , 2 } \/ [ ^ " ' ] + ?) ( \. j s ) ? ( [ " ' ] ) / g, "$1$2.js$4" )
423- . replace ( 'export * as v2 from "./v2.js";' , 'export * as v2 from "./v2/index.js";' )
424- . replaceAll ( "| null | null" , "| null" ) ;
425- }
426-
427361// Sort typed-object arrays for schema keywords whose item order does not affect
428362// payload validity; preserve order everywhere else, especially prefixItems.
429363const typeSortedSchemaArrayKeys = new Set ( [ "anyOf" , "enum" , "oneOf" , "required" ] ) ;
0 commit comments