Skip to content

Commit

Permalink
Brought back an auto eject option
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed Oct 3, 2023
1 parent 6e17ca5 commit 5f84ebb
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/mounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ export class Mounter {
*
* @param file Path to disk image.
* @param options Options object.
* @param ejectOnExit Eject on exit options, or null.
* @returns Info object.
*/
public async attach(
file: string,
options: Readonly<IMounterAttachOptions> | null = null
options: Readonly<IMounterAttachOptions> | null = null,
ejectOnExit: Readonly<IMounterEjectOptions> | null = null
): Promise<IMounterAttachInfo> {
const devices = await this._runAttach(this._argsAttach(file, options));
const {eject, ejectSync} = this._createEjects(devices);
Expand All @@ -128,11 +130,13 @@ export class Mounter {
*
* @param file Path to disk image.
* @param options Options object.
* @param ejectOnExit Eject on exit options, or null.
* @returns Info object.
*/
public attachSync(
file: string,
options: Readonly<IMounterAttachOptions> | null = null
options: Readonly<IMounterAttachOptions> | null = null,
ejectOnExit: Readonly<IMounterEjectOptions> | null = null
): IMounterAttachInfo {
// eslint-disable-next-line no-sync
const devices = this._runAttachSync(this._argsAttach(file, options));
Expand Down Expand Up @@ -365,12 +369,18 @@ export class Mounter {
* Create ejects callback from a list of devices.
*
* @param devices Device list.
* @param ejectOnExit Eject on exit options, or null.
* @returns Callback function.
*/
protected _createEjects(devices: Readonly<Readonly<IMounterDevice>[]>) {
protected _createEjects(
devices: Readonly<Readonly<IMounterDevice>[]>,
ejectOnExit = null
) {
// Find the root device, to use to eject (none possible in theory).
let devEntry = this._findRootDevice(devices)?.devEntry;
return {

let shutdown: (() => void) | null = null;
const info = {
/**
* The eject callback function.
*
Expand All @@ -380,6 +390,9 @@ export class Mounter {
if (devEntry) {
await this.eject(devEntry, options);
devEntry = '';
if (shutdown) {
process.off('exit', shutdown);
}
}
},

Expand All @@ -393,8 +406,26 @@ export class Mounter {
// eslint-disable-next-line no-sync
this.ejectSync(devEntry, options);
devEntry = '';
if (shutdown) {
process.off('exit', shutdown);
}
}
}
};

if (ejectOnExit) {
/**
* Attempt to auto-eject on normal shutdown.
* Does not catch signals (no clean way in a library).
* Users can explicitly call process.exit() on signals to use this.
*/
shutdown = () => {
// eslint-disable-next-line no-sync
info.ejectSync(ejectOnExit);
};
process.once('exit', shutdown);
}

return info;
}
}

0 comments on commit 5f84ebb

Please sign in to comment.