Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(run): return NW.js process reference #1304

Merged
merged 20 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,15 @@ nwbuild({
> Deprecation warning: From v4.6.0 onward, run mode is deprecated. This logic has been ported over to `nwjs/npm-installer` repo and will be removed in the next major release.

```javascript
nwbuild({
const nwProcess = await nwbuild({
mode: "run",
srcDir: "./app",
glob: false,
});
```

Note: The `nwProcess` is a [Node.js process](https://nodejs.org/api/process.html#process)

### Build Mode

Build with defaults:
Expand Down
12 changes: 9 additions & 3 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import util from './util.js';
* @async
* @function
* @param {RunOptions} options Run mode options
* @returns {Promise<void>}
* @returns {Promise<child_process.ChildProcess | null>} - A Node.js process object
*/
async function run({
version = 'latest',
Expand All @@ -35,6 +35,11 @@ async function run({
glob = false,
argv = [],
}) {
/**
* @type {child_process.ChildProcess | null}
*/
let nwProcess = null;

try {
if (util.EXE_NAME[platform] === undefined) {
throw new Error('Unsupported platform.');
Expand All @@ -49,8 +54,8 @@ async function run({
);

return new Promise((res, rej) => {
// It is assumed that the package.json is located at srcDir/package.json
const nwProcess = child_process.spawn(
/* It is assumed that the package.json is located at `${options.srcDir}/package.json` */
nwProcess = child_process.spawn(
path.resolve(nwDir, util.EXE_NAME[platform]),
[...[srcDir], ...argv],
{ stdio: 'inherit' },
Expand All @@ -68,6 +73,7 @@ async function run({
} catch (error) {
console.error(error);
}
return nwProcess;
}

export default run;
61 changes: 61 additions & 0 deletions tests/specs/bld.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import path from 'node:path';
import process from 'node:process';

import { By } from 'selenium-webdriver';
import chrome from 'selenium-webdriver/chrome.js';
import { beforeAll, describe, expect, it } from 'vitest';

import build from '../../src/bld.js';
import get from '../../src/get/index.js';
import util from '../../src/util.js';

const { Driver, ServiceBuilder, Options } = chrome;

describe.skip('bld test suite', async () => {
let driver = undefined;

const nwOptions = {
srcDir: 'tests/fixtures/app',
mode: 'build',
version: '0.93.0',
flavor: 'sdk',
platform: util.PLATFORM_KV[process.platform],
arch: util.ARCH_KV[process.arch],
downloadUrl: 'https://dl.nwjs.io',
manifestUrl: 'https://nwjs.io/versions',
outDir: 'tests/fixtures/out/app',
cacheDir: './node_modules/nw',
cache: true,
ffmpeg: false,
glob: false,
managedManifest: false,
nativeAddon: false,
zip: false
};

beforeAll(async () => {
await get(nwOptions);
}, Infinity);

it('builds without errors', async () => {
await build(nwOptions);
});

it('runs after build', async () => {
const options = new Options();
const args = [
`--nwapp=${path.resolve('test', 'fixture', 'app')}`,
'--headless=new',
];
options.addArguments(args);

const chromedriverPath = util.getPath('chromedriver', nwOptions);

const service = new ServiceBuilder(chromedriverPath).build();

driver = Driver.createSession(options, service);
const text = await driver.findElement(By.id('test')).getText();
expect(text).toBe('Hello, World!');
}, { timeout: Infinity });

});
58 changes: 29 additions & 29 deletions tests/specs/osx.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import nodeManifest from '../../package.json';
describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function () {

const outDir = './tests/fixtures/macos';
const appPath = path.join(outDir, 'nwapp.app');
const appPath = path.join(outDir, 'Demo.app');
const releaseInfo = await util.getReleaseInfo(
nodeManifest.devDependencies.nw.split('^')[1],
util.PLATFORM_KV['darwin'],
Expand All @@ -30,39 +30,39 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function
'Versions',
chromiumVersion,
'Helpers',
'nwapp Helper (Alerts).app');
'Demo Helper (Alerts).app');
const helperGPUPath = path.join(appPath,
'Contents',
'Frameworks',
'nwjs Framework.framework',
'Versions',
chromiumVersion,
'Helpers',
'nwapp Helper (GPU).app');
'Demo Helper (GPU).app');
const helperPluginPath = path.join(appPath,
'Contents',
'Frameworks',
'nwjs Framework.framework',
'Versions',
chromiumVersion,
'Helpers',
'nwapp Helper (Plugin).app');
'Demo Helper (Plugin).app');
const helperRendererPath = path.join(appPath,
'Contents',
'Frameworks',
'nwjs Framework.framework',
'Versions',
chromiumVersion,
'Helpers',
'nwapp Helper (Renderer).app');
'Demo Helper (Renderer).app');
const helperPath = path.join(appPath,
'Contents',
'Frameworks',
'nwjs Framework.framework',
'Versions',
chromiumVersion,
'Helpers',
'nwapp Helper.app');
'Demo Helper.app');

beforeAll(async function () {
/* Copy the cached NW.js into a specific `outDir`. */
Expand All @@ -72,7 +72,7 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function
/* Rename relevant bundles' plists and executables. */
await setOsxConfig({
app: {
name: 'nwapp',
name: 'Demo',
LSApplicationCategoryType: 'public.app-category.utilities',
CFBundleIdentifier: 'io.nwutils.demo',
CFBundleName: 'Demo',
Expand Down Expand Up @@ -113,27 +113,27 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function
});

it('renames the executables correctly', async function () {
const appExePath = path.join(appPath, 'Contents', 'MacOS', 'nwapp');
const appExePath = path.join(appPath, 'Contents', 'MacOS', 'Demo');
const appExePathExists = await util.fileExists(appExePath);
expect(appExePathExists).toEqual(true);

const helperAlertsExePath = path.join(helperAlertsPath, 'Contents', 'MacOS', 'nwapp Helper (Alerts)');
const helperAlertsExePath = path.join(helperAlertsPath, 'Contents', 'MacOS', 'Demo Helper (Alerts)');
const helperAlertsExePathExists = await util.fileExists(helperAlertsExePath);
expect(helperAlertsExePathExists).toEqual(true);

const helperGPUExePath = path.join(helperGPUPath, 'Contents', 'MacOS', 'nwapp Helper (GPU)');
const helperGPUExePath = path.join(helperGPUPath, 'Contents', 'MacOS', 'Demo Helper (GPU)');
const helperGPUExePathExists = await util.fileExists(helperGPUExePath);
expect(helperGPUExePathExists).toEqual(true);

const helperPluginExePath = path.join(helperPluginPath, 'Contents', 'MacOS', 'nwapp Helper (Plugin)');
const helperPluginExePath = path.join(helperPluginPath, 'Contents', 'MacOS', 'Demo Helper (Plugin)');
const helperPluginExePathExists = await util.fileExists(helperPluginExePath);
expect(helperPluginExePathExists).toEqual(true);

const helperRendererExePath = path.join(helperRendererPath, 'Contents', 'MacOS', 'nwapp Helper (Renderer)');
const helperRendererExePath = path.join(helperRendererPath, 'Contents', 'MacOS', 'Demo Helper (Renderer)');
const helperRendererExePathExists = await util.fileExists(helperRendererExePath);
expect(helperRendererExePathExists).toEqual(true);

const helperExePath = path.join(helperPath, 'Contents', 'MacOS', 'nwapp Helper');
const helperExePath = path.join(helperPath, 'Contents', 'MacOS', 'Demo Helper');
const helperExePathExists = await util.fileExists(helperExePath);
expect(helperExePathExists).toEqual(true);
});
Expand All @@ -157,7 +157,7 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function
expect(ContentsInfoPlistJson.CFBundleSpokenName).toEqual('Demo');
expect(ContentsInfoPlistJson.CFBundleVersion).toEqual('0.0.0');
expect(ContentsInfoPlistJson.CFBundleShortVersionString).toEqual('0.0.0');
expect(ContentsInfoPlistJson.CFBundleExecutable).toEqual('nwapp');
expect(ContentsInfoPlistJson.CFBundleExecutable).toEqual('Demo');
expect(ContentsInfoPlistJson.NSLocalNetworkUsageDescription).toEqual('This test application needs to access the local network for testing purposes.');

const HelperAlertsAppJson = plist.parse(
Expand All @@ -171,10 +171,10 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function
)
);

expect(HelperAlertsAppJson.CFBundleDisplayName).toEqual('nwapp Helper (Alerts)');
expect(HelperAlertsAppJson.CFBundleName).toEqual('nwapp Helper (Alerts)');
expect(HelperAlertsAppJson.CFBundleDisplayName).toEqual('Demo Helper (Alerts)');
expect(HelperAlertsAppJson.CFBundleName).toEqual('Demo Helper (Alerts)');
expect(HelperAlertsAppJson.CFBundleIdentifier).toEqual('io.nwutils.demo.helper.alert');
expect(HelperAlertsAppJson.CFBundleExecutable).toEqual('nwapp Helper (Alerts)');
expect(HelperAlertsAppJson.CFBundleExecutable).toEqual('Demo Helper (Alerts)');

const HelperGpuAppJson = plist.parse(
await fs.promises.readFile(
Expand All @@ -187,10 +187,10 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function
)
);

expect(HelperGpuAppJson.CFBundleDisplayName).toEqual('nwapp Helper (GPU)');
expect(HelperGpuAppJson.CFBundleName).toEqual('nwapp Helper (GPU)');
expect(HelperGpuAppJson.CFBundleDisplayName).toEqual('Demo Helper (GPU)');
expect(HelperGpuAppJson.CFBundleName).toEqual('Demo Helper (GPU)');
expect(HelperGpuAppJson.CFBundleIdentifier).toEqual('io.nwutils.demo.helper.gpu');
expect(HelperGpuAppJson.CFBundleExecutable).toEqual('nwapp Helper (GPU)');
expect(HelperGpuAppJson.CFBundleExecutable).toEqual('Demo Helper (GPU)');

const HelperPluginAppJson = plist.parse(
await fs.promises.readFile(
Expand All @@ -203,10 +203,10 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function
)
);

expect(HelperPluginAppJson.CFBundleDisplayName).toEqual('nwapp Helper (Plugin)');
expect(HelperPluginAppJson.CFBundleName).toEqual('nwapp Helper (Plugin)');
expect(HelperPluginAppJson.CFBundleDisplayName).toEqual('Demo Helper (Plugin)');
expect(HelperPluginAppJson.CFBundleName).toEqual('Demo Helper (Plugin)');
expect(HelperPluginAppJson.CFBundleIdentifier).toEqual('io.nwutils.demo.helper.plugin');
expect(HelperPluginAppJson.CFBundleExecutable).toEqual('nwapp Helper (Plugin)');
expect(HelperPluginAppJson.CFBundleExecutable).toEqual('Demo Helper (Plugin)');

const HelperRendererAppJson = plist.parse(
await fs.promises.readFile(
Expand All @@ -219,10 +219,10 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function
)
);

expect(HelperRendererAppJson.CFBundleDisplayName).toEqual('nwapp Helper (Renderer)');
expect(HelperRendererAppJson.CFBundleName).toEqual('nwapp Helper (Renderer)');
expect(HelperRendererAppJson.CFBundleDisplayName).toEqual('Demo Helper (Renderer)');
expect(HelperRendererAppJson.CFBundleName).toEqual('Demo Helper (Renderer)');
expect(HelperRendererAppJson.CFBundleIdentifier).toEqual('io.nwutils.demo.helper.renderer');
expect(HelperRendererAppJson.CFBundleExecutable).toEqual('nwapp Helper (Renderer)');
expect(HelperRendererAppJson.CFBundleExecutable).toEqual('Demo Helper (Renderer)');

const HelperAppJson = plist.parse(
await fs.promises.readFile(
Expand All @@ -235,10 +235,10 @@ describe.runIf(process.platform === 'darwin')('bld/setOsxConfig', async function
)
);

expect(HelperAppJson.CFBundleDisplayName).toEqual('nwapp Helper');
expect(HelperAppJson.CFBundleName).toEqual('nwapp Helper');
expect(HelperAppJson.CFBundleDisplayName).toEqual('Demo Helper');
expect(HelperAppJson.CFBundleName).toEqual('Demo Helper');
expect(HelperAppJson.CFBundleIdentifier).toEqual('io.nwutils.demo.helper');
expect(HelperAppJson.CFBundleExecutable).toEqual('nwapp Helper');
expect(HelperAppJson.CFBundleExecutable).toEqual('Demo Helper');

afterAll(async function () {
await fs.promises.rm('./tests/fixtures/macos', { recursive: true, force: true });
Expand Down
44 changes: 11 additions & 33 deletions tests/specs/run.test.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,41 @@
import path from 'node:path';
import process from 'node:process';

import { By } from 'selenium-webdriver';
import chrome from 'selenium-webdriver/chrome.js';
import { beforeAll, describe, expect, it } from 'vitest';

import build from '../../src/bld.js';
import get from '../../src/get/index.js';
import run from '../../src/run.js';
import util from '../../src/util.js';

const { Driver, ServiceBuilder, Options } = chrome;

describe.skip('build test suite', async () => {
let driver = undefined;
describe('run test suite', async () => {

const nwOptions = {
srcDir: 'tests/fixtures/app',
mode: 'build',
version: '0.83.0',
version: '0.93.0',
flavor: 'sdk',
platform: util.PLATFORM_KV[process.platform],
arch: util.ARCH_KV[process.arch],
downloadUrl: 'https://dl.nwjs.io',
manifestUrl: 'https://nwjs.io/versions',
outDir: 'tests/fixtures/out/app',
cacheDir: 'tests/fixtures/cache',
cacheDir: './node_modules/nw',
cache: true,
ffmpeg: false,
glob: false,
managedManifest: false,
nativeAddon: false,
zip: false,
app: {
name: 'demo'
}
zip: false
};

beforeAll(async () => {
await get(nwOptions);
}, Infinity);

it('builds without errors', async () => {
await build(nwOptions);
it.skipIf(process.platform === 'win32')('runs and is killed via code', async () => {
const nwProcess = await run(nwOptions);
if (nwProcess) {
nwProcess.kill();
expect(nwProcess.killed).toEqual(true);
}
});

it('runs after build', async () => {
const options = new Options();
const args = [
`--nwapp=${path.resolve('test', 'fixture', 'app')}`,
'--headless=new',
];
options.addArguments(args);

const chromedriverPath = util.getPath('chromedriver', nwOptions);

const service = new ServiceBuilder(chromedriverPath).build();

driver = Driver.createSession(options, service);
const text = await driver.findElement(By.id('test')).getText();
expect(text).toBe('Hello, World!');
}, { timeout: Infinity });
});
Loading