Skip to content

Commit

Permalink
fix(run): update run mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushmanchhabra committed Mar 30, 2024
1 parent c752795 commit a52b2d6
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 109 deletions.
60 changes: 0 additions & 60 deletions bin/nw.js

This file was deleted.

32 changes: 0 additions & 32 deletions lib/app_assets.js

This file was deleted.

11 changes: 10 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type": "module",
"main": "src/index.js",
"bin": {
"nw": "bin/nw.js"
"nw": "src/cli.js"
},
"scripts": {
"postinstall": "node src/postinstall.js",
Expand All @@ -34,6 +34,7 @@
"license": "MIT",
"dependencies": {
"axios": "^1.6.8",
"commander": "^12.0.0",
"semver": "^7.5.4",
"tar": "^6.2.1",
"yargs": "^17.7.2",
Expand Down
35 changes: 35 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env node

import process from 'node:process';

import { program } from 'commander';

import parse from '../src/parse.js';
import run from '../src/run.js';

await cli();

async function cli() {

program
.option('--version <string>')
.option('--flavor <flavor>')
.option('--platform <platform>')
.option('--arch <arch>')
.option('--cacheDir <cacheDir>')
.argument('<app>')
.parse(process.argv);

let options = program.opts();
options = await parse(options);

await run({
version: options.version,
flavor: options.flavor,
platform: options.platform,
arch: options.arch,
cacheDir: options.cacheDir,
srcDir: program.args[0],
});

}
65 changes: 65 additions & 0 deletions src/parse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import process from 'node:process';

import semver from 'semver';

import nodeManifest from '../package.json' assert { type: 'json' };
import util from '../src/util.js';

/**
* @typedef {object} ParseOptions
* @property {string | "latest" | "stable" | "lts"} version Runtime version
* @property {"normal" | "sdk"} flavor Build flavor
* @property {"linux" | "osx" | "win"} platform Target platform
* @property {"ia32" | "x64" | "arm64"} arch Target arch
* @property {string} downloadUrl Download server.
* @property {string} cacheDir Cache 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
*/

/**
* Parse options.
*
* @param {ParseOptions} options
* @return {Promise<ParseOptions>}
*/
export default async function parse(options) {
options.version = options.version ?? nodeManifest.version;
const parsedVersion = semver.parse(options.version);
options.version = [
parsedVersion.major,
parsedVersion.minor,
parsedVersion.patch
].join('.');

options.flavor = options.flavor || process.env.npm_config_nwjs_build_type || process.env.NWJS_BUILD_TYPE || 'normal';

/* Check if version is a prelease. */
if (typeof parsedVersion?.prerelease?.[0] === 'string') {
let prerelease = parsedVersion.prerelease[0].split('-');
if (prerelease.length > 1) {
prerelease = prerelease.slice(0, -1);
}
options.version = [version, ...prerelease].join('-');
}

/* Check build flavor and slice that off the `version`. */
if (options.version.endsWith('-sdk')) {
options.version = options.version.slice(0, -4);
options.flavor = 'sdk';
} else if (options.version.endsWith('sdk')) {
options.version = version.slice(0, -3);
options.flavor = 'sdk';
}

options.platform = options.platform || util.PLATFORM_KV[process.env.npm_config_nwjs_platform || process.env.NWJS_PLATFORM || process.platform];
options.arch = options.arch || util.ARCH_KV[process.env.npm_config_nwjs_process_arch || process.env.NWJS_ARCH || process.arch];
options.downloadUrl = options.downloadUrl || process.env.npm_config_nwjs_urlbase || process.env.NWJS_URLBASE || 'https://dl.nwjs.io';
options.cacheDir = options.npm_config_nwjs_urlbase || process.env.npm_config_nwjs_cache_dir || process.env.NWJS_CACHE_DIR || '.';
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;

return { ...options };
}
17 changes: 3 additions & 14 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ 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} [glob = false] If true, throw error
* @property {string[]} [argv = []] CLI arguments
*/

/**
Expand All @@ -31,33 +29,24 @@ async function run({
flavor = "normal",
platform = util.PLATFORM_KV[process.platform],
arch = util.ARCH_KV[process.arch],
srcDir = "./src",
srcDir = ".",
cacheDir = "./cache",
glob = false,
argv = [],
}) {

try {
if (util.EXE_NAME[platform] === undefined) {
throw new Error("Unsupported platform.");
}
if (glob === true) {
throw new Error("Globbing is not supported with run mode.");
}

const nwDir = path.resolve(
cacheDir,
`nwjs${flavor === "sdk" ? "-sdk" : ""}-v${version}-${platform}-${arch}`,
);

return new Promise((res, rej) => {
// It is assumed that the package.json is located at srcDir/package.json
const nwProcess = child_process.spawn(
path.resolve(nwDir, util.EXE_NAME[platform]),
[...[srcDir], ...argv],
{
detached: true,
windowsHide: true,
},
[srcDir]
);

nwProcess.on("close", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
/**
* Check if file exists at specified path.
*
* @param {NodeJS.fs.} filePath - File path to check existence of
* @param {fs.PathLike} filePath - File path to check existence of
* @return {Promise<boolean>} `true` if exists, otherwise `false`
*/
async function fileExists(filePath) {
Expand Down

0 comments on commit a52b2d6

Please sign in to comment.