Skip to content

Commit

Permalink
Added spawn and spawnSync
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzjuniel committed Oct 22, 2023
1 parent 6203133 commit 1f625b6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
64 changes: 63 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from 'path';
import child_process from "child_process";
import os from "os";

const platform = require("os").platform();
const platform = os.platform();
const useWine = platform != "win32";

/**
Expand Down Expand Up @@ -42,6 +43,67 @@ export function checkWindowsOrWine() {
return isWindowsOrWine;
}


/**
* Calls spawn function from child_process module. Wine is automatically used when using Linux or macOS.
*
* Returns the `child_process.ChildProcess` if successful or `null` if using Linux or macOS with no Wine installed
*
* For more information, please refer to child_process.spawn [documentation](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options).
*
* @param command The command to run.
* @param args List of string arguments
* @param options Additional options for the spawn function.
*
* @returns child_process.ChildProcess | null
*/
export function spawn(command: string, args: string[], options:child_process.SpawnOptions)
: child_process.ChildProcess | null {

if (!isWindowsOrWine) return null;

if (!useWine) return child_process.spawn(command, args, options);

let cmd = command;

if (typeof options.cwd === 'string') {
cmd = path.join(options.cwd, cmd);
}

return child_process.spawn(`wine`, [cmd, ...args], options);
}


/**
* Calls spawnSync function from child_process module. Wine is automatically used when using Linux or macOS.
*
* Returns the `child_process.SpawnSyncReturns<Buffer>` if successful or `null` if using Linux or macOS with no Wine installed
*
* For more information, please refer to child_process.spawnSync [documentation](https://nodejs.org/api/child_process.html#child_processspawnsynccommand-args-options).
*
* @param command The command to run.
* @param args List of string arguments
* @param options Additional options for the spawn function.
*
* @returns child_process.ChildProcess | null
*/
export function spawnSync(command: string, args: string[], options:child_process.SpawnOptions)
: child_process.SpawnSyncReturns<Buffer> | null {

if (!isWindowsOrWine) return null;

if (!useWine) return child_process.spawnSync(command, args, options);

let cmd = command;

if (typeof options.cwd === 'string') {
cmd = path.join(options.cwd, cmd);
}

return child_process.spawnSync(`wine`, [cmd, ...args], options);
}


/**
* Calls exec function from child_process module. Wine is automatically used when using Linux or macOS.
*
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wine-child-process",
"version": "0.1.1",
"version": "0.1.2",
"description": "A child process implementation that automatically utilizes wine when running Windows applications in Linux and macOS",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down

0 comments on commit 1f625b6

Please sign in to comment.