4
4
// This script runs a TypeScript file using Node.js by first bundling it with
5
5
// esbuild.
6
6
import { spawn } from "cross-spawn" ;
7
+ import { build } from "esbuild" ;
7
8
import { existsSync , mkdirSync , rmdirSync } from "node:fs" ;
8
9
import { fileURLToPath } from "node:url" ;
9
10
import { dirname , join } from "node:path" ;
@@ -12,7 +13,7 @@ import { dirname, join } from "node:path";
12
13
* Run a command with arguments and return a child process
13
14
* @param {string } command
14
15
* @param {string[] } args
15
- * @param {NodeJS.ProcessEnv? } extraEnv
16
+ * @param {Partial< NodeJS.ProcessEnv> ? } extraEnv
16
17
*/
17
18
function runCommand ( command , args , extraEnv = { } ) {
18
19
return spawn ( command , args , {
@@ -47,7 +48,7 @@ function cleanupTempDirectory(tempDir) {
47
48
}
48
49
49
50
// Main function to execute the script
50
- function main ( ) {
51
+ async function main ( ) {
51
52
const args = process . argv . slice ( 2 ) ;
52
53
53
54
// Check if the input file is specified
@@ -70,38 +71,30 @@ function main() {
70
71
process . on ( "SIGTERM" , ( ) => cleanupTempDirectory ( tempDir ) ) ;
71
72
72
73
// 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 ,
104
83
} ) ;
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 ) ) ;
105
98
}
106
99
107
100
main ( ) ;
0 commit comments