Skip to content

Commit

Permalink
Removed spawn utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Sep 30, 2023
1 parent 074480d commit 5a09d21
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 46 deletions.
34 changes: 18 additions & 16 deletions src/mounter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {spawn} from 'node:child_process';

import {
Plist,
ValueDict,
Expand All @@ -6,7 +8,7 @@ import {
ValueBoolean
} from '@shockpkg/plist-dom';

import {shutdownHook, shutdownUnhook, spawn} from './util';
import {shutdownHook, shutdownUnhook} from './util';

export interface IMounterOptions {
//
Expand Down Expand Up @@ -172,21 +174,19 @@ export class Mounter {
* @returns Devices list.
*/
protected async _runAttach(args: Readonly<string[]>) {
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<number | null>((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());
}

/**
Expand All @@ -195,9 +195,11 @@ export class Mounter {
* @param args CLI args.
*/
protected async _runEject(args: Readonly<string[]>) {
const {done} = spawn(this.hdiutil, args);

const code = await done;
const proc = spawn(this.hdiutil, args);
const code = await new Promise<number | null>((resolve, reject) => {
proc.once('exit', resolve);
proc.once('error', reject);
});
if (code) {
throw new Error(`Eject failed: hdiutil exit code: ${code}`);
}
Expand Down
30 changes: 0 additions & 30 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -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<string[]> | null = null,
options: Readonly<SpawnOptions | SpawnOptionsWithoutStdio> | null = null
) {
const proc = childProcessSpawn(command, args || [], options || {});
const done = new Promise<number | null>((resolve, reject) => {
proc.on('exit', resolve);
proc.on('error', reject);
});
return {
proc,
done
};
}

const exitHooks = new Set<() => Promise<unknown>>();

/**
Expand Down

0 comments on commit 5a09d21

Please sign in to comment.