Skip to content

Commit

Permalink
fix(run): pass cli args to nw process (#156)
Browse files Browse the repository at this point in the history
Fixes: #150
  • Loading branch information
ayushmanchhabra authored Apr 27, 2024
1 parent a95ec3a commit e5d6fb1
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 20 deletions.
16 changes: 13 additions & 3 deletions .github/sdk.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { writeFileSync } from 'node:fs';
import fs from 'node:fs';
import path from 'node:path';
import url from 'node:url';

import nodeManifest from '../package.json' assert { type: 'json' };
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
/**
* @type {fs.PathLike}
*/
const nodeManifestPath = path.resolve(__dirname, '..' ,'package.json');
/**
* @type {object}
*/
const nodeManifest = JSON.parse(fs.readFileSync(nodeManifestPath), { encoding: 'utf-8' });

nodeManifest.version = nodeManifest.version + '-sdk';

writeFileSync('./package.json', JSON.stringify(nodeManifest, null, 2));
fs.writeFileSync('./package.json', JSON.stringify(nodeManifest, null, 2));
15 changes: 8 additions & 7 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ await cli();
async function cli() {

program
.option('--version <string>')
.option('--flavor <flavor>')
.option('--platform <platform>')
.option('--arch <arch>')
.option('--cacheDir <cacheDir>')
.argument('[app]')
.allowUnknownOption()
.argument('[app]', 'File path to project', '.')
.option('--version <string>', 'NW.js version')
.option('--flavor <flavor>', 'NW.js flavor')
.option('--platform <platform>', 'Host platform')
.option('--arch <arch>', 'Host architecture')
.option('--cacheDir <cacheDir>', 'File path to cache directory')
.allowUnknownOption() // this allows chromium and node options to be passed through to the nwjs binary
.parse(process.argv);

let options = program.opts();
Expand All @@ -31,6 +31,7 @@ async function cli() {
arch: options.arch,
cacheDir: options.cacheDir,
srcDir: program.args[0],
args: program.args.slice(1),
});

}
12 changes: 11 additions & 1 deletion src/parse.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import url from 'node:url';

import semver from 'semver';

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

/**
Expand All @@ -26,6 +27,15 @@ import util from '../src/util.js';
* @return {Promise<ParseOptions>}
*/
export default async function parse(options) {
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
/**
* @type {fs.PathLike}
*/
const nodeManifestPath = path.resolve(__dirname, '..', 'package.json');
/**
* @type {object}
*/
const nodeManifest = JSON.parse(await fs.promises.readFile(nodeManifestPath, { encoding: 'utf-8' }));
options.version = options.version ?? nodeManifest.version;
const parsedVersion = semver.parse(options.version);
options.version = [
Expand Down
16 changes: 14 additions & 2 deletions src/postinstall.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import fs from 'node:fs';
import process from 'node:process';
import path from 'node:path';
import url from 'node:url';

import semver from 'semver';

import get from './get.js';
import util from './util.js';

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

await postinstall()
.catch((error) => {
if (error.code === 'EPERM') {
console.error('Unable to create symlink since user did not run as Administrator.');
} else {
console.error(error)
}
});

async function postinstall() {
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
/**
* @type {fs.PathLike}
*/
const nodeManifestPath = path.resolve(__dirname, '..' ,'package.json');
/**
* @type {object}
*/
const nodeManifest = JSON.parse(await fs.promises.readFile(nodeManifestPath));
const parsedVersion = semver.parse(nodeManifest.version);
let version = [
parsedVersion.major,
Expand Down
9 changes: 8 additions & 1 deletion 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 {string[]} [args = []] Command line arguments
*/

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

try {
Expand All @@ -46,13 +48,18 @@ async function run({
return new Promise((res, rej) => {
const nwProcess = child_process.spawn(
path.resolve(nwDir, util.EXE_NAME[platform]),
[srcDir]
[srcDir, ...args],
{ detached: true, stdio: "ignore" },
);

nwProcess.on("close", () => {
res();
});

nwProcess.on("message", (message) => {
console.log(message);
});

nwProcess.on("error", (error) => {
console.error(error);
rej(error);
Expand Down
8 changes: 4 additions & 4 deletions test/findpath.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { expect, test } from 'vitest';
import util from '../src/util.js';

test('nwjs has downloaded and been extracted', function () {
util.findpath('nwjs').then(function (path) {
util.findpath('nwjs', { flavor: 'sdk' }).then(function (path) {
expect(fs.existsSync(path)).toEqual(true);
})
.catch((error) => {
console.log(error);
});
.catch((error) => {
console.log(error);
});
});
7 changes: 5 additions & 2 deletions test/selenium.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { afterAll, beforeAll, describe, expect, it } from "vitest";
import util from "../src/util.js";

describe("run", async function () {
/**
* @type {chrome.Driver | undefined}
*/
let driver = undefined;

beforeAll(async function () {
Expand All @@ -32,8 +35,8 @@ describe("run", async function () {
expect(text).toEqual('Hello, World! Is anyone out there?');
}, { timeout: Infinity });

afterAll(function () {
driver.quit();
afterAll(async function () {
await driver.quit();
});

});

0 comments on commit e5d6fb1

Please sign in to comment.