From 7d5136883f09de9899bfec396c31af7058065ce5 Mon Sep 17 00:00:00 2001 From: Courey Elliott Date: Wed, 12 Jun 2024 15:45:24 -0400 Subject: [PATCH] handle symlinked and npm installed directory name --- run.ts | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/run.ts b/run.ts index eb1c69a..8d4a281 100644 --- a/run.ts +++ b/run.ts @@ -16,6 +16,8 @@ import { spawn, untilTerminated } from './processes.js' import type { SandboxEngine } from './engines.js' import { UnexpectedResolveError, neverResolve } from './promises.js' import { fork } from 'child_process' +import path from 'path' +import { fileURLToPath } from 'url' type SearchEngineLauncherFunction = ( props: T & { @@ -56,6 +58,13 @@ const launchBinary: SearchEngineLauncherFunction<{ bin: string }> = async ({ } } +function directoryName() { + const __filename = fileURLToPath(import.meta.url) + const __dirname = path.dirname(__filename) + return __dirname.includes('node_modules') + ? __dirname + : './node_modules/@nasa-gcn/architect-plugin-search' +} const launchDocker: SearchEngineLauncherFunction = async ({ dataDir, logsDir, @@ -70,17 +79,9 @@ const launchDocker: SearchEngineLauncherFunction = async ({ port, options, } - const subprocess = fork( - './node_modules/@nasa-gcn/architect-plugin-search/launchSearch.js', - [JSON.stringify(argv)] - ) - - const waitUntilStopped = new Promise((resolve) => { - subprocess.on('exit', () => { - console.log('Docker container exited') - resolve() - }) - }) + const subprocess = fork(`${directoryName()}/launchSearch.js`, [ + JSON.stringify(argv), + ]) return { async kill() { @@ -88,7 +89,12 @@ const launchDocker: SearchEngineLauncherFunction = async ({ subprocess.send({ action: 'kill' }) }, async waitUntilStopped() { - await waitUntilStopped + return new Promise((resolve) => { + subprocess.on('exit', () => { + console.log('Docker container exited') + resolve() + }) + }) }, } }