diff --git a/lib/index.ts b/lib/index.ts index 48eb6e3..7b5a6c8 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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"; /** @@ -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` 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 | 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. * diff --git a/package-lock.json b/package-lock.json index 7055374..2ec02f5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "wine-child-process", - "version": "0.1.0", + "version": "0.1.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "wine-child-process", - "version": "0.1.0", + "version": "0.1.2", "license": "ISC", "devDependencies": { "@types/node": "^18.7.3", diff --git a/package.json b/package.json index bb4b070..b5ab77f 100644 --- a/package.json +++ b/package.json @@ -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",