44// This script runs a TypeScript file using Node.js by first bundling it with
55// esbuild.
66import { spawn } from "cross-spawn" ;
7+ import { build } from "esbuild" ;
78import { existsSync , mkdirSync , rmdirSync } from "node:fs" ;
89import { fileURLToPath } from "node:url" ;
910import { dirname , join } from "node:path" ;
@@ -12,7 +13,7 @@ import { dirname, join } from "node:path";
1213 * Run a command with arguments and return a child process
1314 * @param {string } command
1415 * @param {string[] } args
15- * @param {NodeJS.ProcessEnv? } extraEnv
16+ * @param {Partial< NodeJS.ProcessEnv> ? } extraEnv
1617 */
1718function runCommand ( command , args , extraEnv = { } ) {
1819 return spawn ( command , args , {
@@ -47,7 +48,7 @@ function cleanupTempDirectory(tempDir) {
4748}
4849
4950// Main function to execute the script
50- function main ( ) {
51+ async function main ( ) {
5152 const args = process . argv . slice ( 2 ) ;
5253
5354 // Check if the input file is specified
@@ -70,38 +71,30 @@ function main() {
7071 process . on ( "SIGTERM" , ( ) => cleanupTempDirectory ( tempDir ) ) ;
7172
7273 // Run esbuild to bundle the TypeScript file
73- const esbuildProcess = runCommand ( "esbuild" , [
74- "--sourcemap" ,
75- "--log-level=warning" ,
76- "--conditions=cursorless:bundler" ,
77- "--bundle" ,
78- "--format=cjs" ,
79- "--platform=node" ,
80- fileToRun ,
81- "--outfile=" + outFile ,
82- ] ) ;
83-
84- esbuildProcess . on ( "close" , ( code ) => {
85- if ( code === 0 ) {
86- // Execute the bundled file with Node, passing any additional arguments
87- const nodeProcess = runCommand (
88- process . execPath ,
89- [ "--enable-source-maps" , outFile , ...childArgs ] ,
90- {
91- [ "CURSORLESS_REPO_ROOT" ] : join (
92- dirname ( fileURLToPath ( import . meta. url ) ) ,
93- ".." ,
94- ".." ,
95- ".." ,
96- ) ,
97- } ,
98- ) ;
99- nodeProcess . on ( "close" , ( code ) => process . exit ( code ?? undefined ) ) ;
100- } else {
101- console . error ( `esbuild failed with code ${ code } ` ) ;
102- process . exit ( code ?? undefined ) ;
103- }
74+ await build ( {
75+ entryPoints : [ fileToRun ] ,
76+ sourcemap : true ,
77+ conditions : [ "cursorless:bundler" ] ,
78+ logLevel : "warning" ,
79+ platform : "node" ,
80+ bundle : true ,
81+ format : "cjs" ,
82+ outfile : outFile ,
10483 } ) ;
84+
85+ const nodeProcess = runCommand (
86+ process . execPath ,
87+ [ "--enable-source-maps" , outFile , ...childArgs ] ,
88+ {
89+ [ "CURSORLESS_REPO_ROOT" ] : join (
90+ dirname ( fileURLToPath ( import . meta. url ) ) ,
91+ ".." ,
92+ ".." ,
93+ ".." ,
94+ ) ,
95+ } ,
96+ ) ;
97+ nodeProcess . on ( "close" , ( code ) => process . exit ( code ?? undefined ) ) ;
10598}
10699
107100main ( ) ;
0 commit comments