From f772c0e5516d7b6a6be502d3682a49d67f907687 Mon Sep 17 00:00:00 2001 From: Ayushman Chhabra <14110965+ayushmanchhabra@users.noreply.github.com> Date: Sun, 19 May 2024 21:49:11 +0530 Subject: [PATCH] feat(run): add `unref` option (#159) Fixes: #70 Refs: https://github.com/nwjs/nw.js/issues/7852 --- README.md | 4 ++++ src/parse.js | 4 +++- src/run.js | 6 ++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 25aed69..d76ed9b 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,10 @@ Set `nwjs_native_addon` in `.npmrc` or `NWJS_NATIVE_ADDON` environment variable Set `nwjs_urlbase` in `.npmrc`or `NWJS_URLBASE` environment variable. Defaults to `https://dl.nwjs.io`. The file system (`file://`) is also supported (for example, `file:///home/localghost/local_mirror`). +### Specify unref flag + +Set `nwjs_unref` in `.npmrc` or `NWJS_UNREF` environment variable. Default to `false`. This is useful if you're using `nw` package to call the executable and want to prevent zombie processes eating up memory. + ## Usage Add a script in your `package.json`: diff --git a/src/parse.js b/src/parse.js index 3c5263c..6359c43 100644 --- a/src/parse.js +++ b/src/parse.js @@ -18,7 +18,8 @@ import util from '../src/util.js'; * @property {string} srcDir Source directory * @property {boolean} cache If false, remove cache and redownload. * @property {boolean} ffmpeg If true, ffmpeg is not downloaded. - * @property {false | "gyp"} nativeAddon Rebuild native modules + * @property {false | "gyp"} nativeAddon Rebuild native modules + * @property {boolean} unref Unref the child process and unblock the caller */ /** @@ -73,6 +74,7 @@ export default async function parse(options) { options.cache = options.cache || process.env.npm_config_nwjs_cache || process.env.NWJS_CACHE || true; options.ffmpeg = options.ffmpeg || process.env.npm_config_nwjs_ffmpeg || process.env.NWJS_FFMPEG || false; options.nativeAddon = options.nativeAddon || process.env.npm_config_nwjs_native_addon || process.env.NWJS_NATIVE_ADDON || false; + options.unref = options.unref || process.env.npm_config_nwjs_unref || process.env.NWJS_UNREF || false; return { ...options }; } diff --git a/src/run.js b/src/run.js index c292d3a..2f202ad 100644 --- a/src/run.js +++ b/src/run.js @@ -13,6 +13,7 @@ import util from "./util.js"; * @property {"ia32" | "x64" | "arm64"} [arch] Target arch * @property {string} [srcDir = "./src"] Source directory * @property {string} [cacheDir = "./cache"] Cache directory + * @property {boolean} [unref = false] Unref the child process and unblock the caller * @property {string[]} [args = []] Command line arguments */ @@ -32,6 +33,7 @@ async function run({ arch = util.ARCH_KV[process.arch], srcDir = ".", cacheDir = "./cache", + unref = false, args = [], }) { @@ -64,6 +66,10 @@ async function run({ console.error(error); rej(error); }); + + if (unref) { + nwProcess.unref(); + } }); } catch (error) { console.error(error);