@@ -4,7 +4,7 @@ import { spawn } from "node:child_process"
44import { access , mkdtemp , readFile , readdir , rm } from "node:fs/promises"
55import os from "node:os"
66import path from "node:path"
7- import { fileURLToPath } from "node:url"
7+ import { fileURLToPath , pathToFileURL } from "node:url"
88
99import { getManifestBinEntries , getNativeSmokeWrapperFile , getWrapperDefinitions , normalizeTargetPlatform , resolveReleasePath } from "./wrappers.mjs"
1010
@@ -14,10 +14,12 @@ const packageRoot = path.resolve(__dirname, "..")
1414const root = path . resolve ( packageRoot , "../.." )
1515const downloadedDir = path . resolve ( root , process . env . ARTIFACTS_DOWNLOAD_DIR || path . join ( "downloaded" , "omniroute" ) )
1616
17- main ( ) . catch ( ( error ) => {
18- console . error ( error instanceof Error ? error . stack || error . message : String ( error ) )
19- process . exitCode = 1
20- } )
17+ if ( isMainModule ( ) ) {
18+ main ( ) . catch ( ( error ) => {
19+ console . error ( error instanceof Error ? error . stack || error . message : String ( error ) )
20+ process . exitCode = 1
21+ } )
22+ }
2123
2224async function main ( ) {
2325 const metadataPath = await findFile ( downloadedDir , ( entryPath ) => path . basename ( entryPath ) === "metadata.json" )
@@ -139,17 +141,6 @@ async function runNativeWrapperVersion(releaseRoot, binEntries, targetPlatform,
139141 const wrapperFile = getNativeSmokeWrapperFile ( binEntries , targetPlatform )
140142 const wrapperPath = resolveReleasePath ( releaseRoot , wrapperFile )
141143
142- if ( targetPlatform === "windows" ) {
143- return runAndCapture (
144- "cmd.exe" ,
145- [ "/d" , "/s" , "/c" , `"${ wrapperPath } " --version` ] ,
146- {
147- cwd : releaseRoot ,
148- env,
149- } ,
150- )
151- }
152-
153144 return runAndCapture ( wrapperPath , [ "--version" ] , {
154145 cwd : releaseRoot ,
155146 env,
@@ -192,9 +183,22 @@ async function exists(targetPath) {
192183 }
193184}
194185
186+ export function resolveSpawnInvocation ( command , args , hostPlatform = process . platform ) {
187+ if ( hostPlatform === "win32" && / \. ( c m d | b a t ) $ / i. test ( command ) ) {
188+ return {
189+ command : process . env . ComSpec || "C:\\Windows\\System32\\cmd.exe" ,
190+ args : [ "/d" , "/s" , "/c" , command , ...args ] ,
191+ }
192+ }
193+
194+ return { command, args }
195+ }
196+
195197function run ( command , args , options = { } ) {
198+ const invocation = resolveSpawnInvocation ( command , args )
199+
196200 return new Promise ( ( resolve , reject ) => {
197- const child = spawn ( command , args , {
201+ const child = spawn ( invocation . command , invocation . args , {
198202 cwd : options . cwd || root ,
199203 env : options . env || process . env ,
200204 stdio : "inherit" ,
@@ -206,14 +210,16 @@ function run(command, args, options = {}) {
206210 resolve ( )
207211 return
208212 }
209- reject ( new Error ( `${ command } ${ args . join ( " " ) } exited with code ${ code } ` ) )
213+ reject ( new Error ( `${ invocation . command } ${ invocation . args . join ( " " ) } exited with code ${ code } ` ) )
210214 } )
211215 } )
212216}
213217
214218function runAndCapture ( command , args , options = { } ) {
219+ const invocation = resolveSpawnInvocation ( command , args )
220+
215221 return new Promise ( ( resolve , reject ) => {
216- const child = spawn ( command , args , {
222+ const child = spawn ( invocation . command , invocation . args , {
217223 cwd : options . cwd || root ,
218224 env : options . env || process . env ,
219225 stdio : [ "ignore" , "pipe" , "inherit" ] ,
@@ -230,11 +236,15 @@ function runAndCapture(command, args, options = {}) {
230236 resolve ( output )
231237 return
232238 }
233- reject ( new Error ( `${ command } ${ args . join ( " " ) } exited with code ${ code } ` ) )
239+ reject ( new Error ( `${ invocation . command } ${ invocation . args . join ( " " ) } exited with code ${ code } ` ) )
234240 } )
235241 } )
236242}
237243
238244function escapePowerShell ( value ) {
239245 return value . replaceAll ( "'" , "''" )
240246}
247+
248+ function isMainModule ( ) {
249+ return process . argv [ 1 ] != null && import . meta. url === pathToFileURL ( process . argv [ 1 ] ) . href
250+ }
0 commit comments