diff --git a/src/mounter.ts b/src/mounter.ts index 65aa96f..00f4959 100644 --- a/src/mounter.ts +++ b/src/mounter.ts @@ -1,3 +1,5 @@ +import {spawn} from 'node:child_process'; + import { Plist, ValueDict, @@ -6,7 +8,7 @@ import { ValueBoolean } from '@shockpkg/plist-dom'; -import {shutdownHook, shutdownUnhook, spawn} from './util'; +import {shutdownHook, shutdownUnhook} from './util'; export interface IMounterOptions { // @@ -172,21 +174,19 @@ export class Mounter { * @returns Devices list. */ protected async _runAttach(args: Readonly) { - const {proc, done} = spawn(this.hdiutil, args); - const stdoutData: Buffer[] = []; - if (proc.stdout) { - proc.stdout.on('data', (data: Buffer) => { - stdoutData.push(data); - }); - } - - const code = await done; + const stdouts: Buffer[] = []; + const proc = spawn(this.hdiutil, args); + proc.stdout.on('data', (data: Buffer) => { + stdouts.push(data); + }); + const code = await new Promise((resolve, reject) => { + proc.once('exit', resolve); + proc.once('error', reject); + }); if (code) { throw new Error(`Attach failed: hdiutil exit code: ${code}`); } - const stdout = Buffer.concat(stdoutData).toString(); - - return this._parseDevices(stdout); + return this._parseDevices(Buffer.concat(stdouts).toString()); } /** @@ -195,9 +195,11 @@ export class Mounter { * @param args CLI args. */ protected async _runEject(args: Readonly) { - const {done} = spawn(this.hdiutil, args); - - const code = await done; + const proc = spawn(this.hdiutil, args); + const code = await new Promise((resolve, reject) => { + proc.once('exit', resolve); + proc.once('error', reject); + }); if (code) { throw new Error(`Eject failed: hdiutil exit code: ${code}`); } diff --git a/src/util.ts b/src/util.ts index fc6cdc7..6c9213e 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,35 +1,5 @@ -import { - spawn as childProcessSpawn, - SpawnOptions, - SpawnOptionsWithoutStdio -} from 'node:child_process'; - import asyncExitHook from 'async-exit-hook'; -/** - * Spawn a subprocess with a promise for completion. - * - * @param command Command path. - * @param args Argument list. - * @param options Options object. - * @returns Info object. - */ -export function spawn( - command: string, - args: Readonly | null = null, - options: Readonly | null = null -) { - const proc = childProcessSpawn(command, args || [], options || {}); - const done = new Promise((resolve, reject) => { - proc.on('exit', resolve); - proc.on('error', reject); - }); - return { - proc, - done - }; -} - const exitHooks = new Set<() => Promise>(); /**