Skip to content

Commit

Permalink
feat(run): add unref option (#159)
Browse files Browse the repository at this point in the history
Fixes: #70
Refs: nwjs/nw.js#7852
  • Loading branch information
ayushmanchhabra authored May 19, 2024
1 parent 0179935 commit f772c0e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
4 changes: 3 additions & 1 deletion src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

/**
Expand Down Expand Up @@ -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 };
}
6 changes: 6 additions & 0 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

Expand All @@ -32,6 +33,7 @@ async function run({
arch = util.ARCH_KV[process.arch],
srcDir = ".",
cacheDir = "./cache",
unref = false,
args = [],
}) {

Expand Down Expand Up @@ -64,6 +66,10 @@ async function run({
console.error(error);
rej(error);
});

if (unref) {
nwProcess.unref();
}
});
} catch (error) {
console.error(error);
Expand Down

0 comments on commit f772c0e

Please sign in to comment.