@@ -15,6 +15,7 @@ const protoc = path.join(require.resolve("grpc-tools"), "../bin/protoc")
1515const __filename = fileURLToPath ( import . meta. url )
1616const SCRIPT_DIR = path . dirname ( __filename )
1717const ROOT_DIR = path . resolve ( SCRIPT_DIR , ".." )
18+ const TS_OUT_DIR = path . join ( ROOT_DIR , "src" , "shared" , "proto" )
1819
1920const isWindows = process . platform === "win32"
2021const tsProtoPlugin = isWindows
@@ -68,18 +69,9 @@ async function main() {
6869 // Check for Apple Silicon compatibility before proceeding
6970 checkAppleSiliconCompatibility ( )
7071
71- // Define output directories
72- const TS_OUT_DIR = path . join ( ROOT_DIR , "src" , "shared" , "proto" )
73-
7472 // Create output directories if they don't exist
7573 await fs . mkdir ( TS_OUT_DIR , { recursive : true } )
76-
77- // Clean up existing generated files
78- console . log ( chalk . cyan ( "Cleaning up existing generated TypeScript files..." ) )
79- const existingFiles = await globby ( "**/*.ts" , { cwd : TS_OUT_DIR } )
80- for ( const file of existingFiles ) {
81- await fs . unlink ( path . join ( TS_OUT_DIR , file ) )
82- }
74+ await cleanup ( )
8375
8476 // Check for missing proto files for services in serviceNameMap
8577 await ensureProtoFilesExist ( )
@@ -654,6 +646,35 @@ export {
654646 console . log ( chalk . green ( `Generated host gRPC client at ${ configPath } ` ) )
655647}
656648
649+ async function cleanup ( ) {
650+ // Clean up existing generated files
651+ console . log ( chalk . cyan ( "Cleaning up existing generated TypeScript files..." ) )
652+ const existingFiles = await globby ( "**/*.ts" , { cwd : TS_OUT_DIR } )
653+ for ( const file of existingFiles ) {
654+ await fs . unlink ( path . join ( TS_OUT_DIR , file ) )
655+ }
656+
657+ // Clean up generated files that were moved.
658+ await fs . rm ( path . join ( ROOT_DIR , "src" , "standalone" , "services" , "host-grpc-client.ts" ) , { force : true } )
659+ await rmdir ( path . join ( ROOT_DIR , "src" , "standalone" , "services" ) )
660+ await fs . rm ( path . join ( ROOT_DIR , "hosts" , "vscode" ) , { force : true , recursive : true } )
661+ await rmdir ( path . join ( ROOT_DIR , "hosts" ) )
662+ }
663+
664+ /**
665+ * Remove an empty dir, do nothing if the directory doesn't exist or is not empty.
666+ */
667+ async function rmdir ( path ) {
668+ try {
669+ await fs . rmdir ( path )
670+ } catch ( error ) {
671+ if ( error . code !== "ENOTEMPTY" && error . code !== "ENOENT" ) {
672+ // Only re-throw if it's not "not empty" or "doesn't exist"
673+ throw error
674+ }
675+ }
676+ }
677+
657678// Check for Apple Silicon compatibility
658679function checkAppleSiliconCompatibility ( ) {
659680 // Only run check on macOS
0 commit comments