Skip to content

Commit

Permalink
feat(findpath): detect ChromeDriver path (#115)
Browse files Browse the repository at this point in the history
* refactor: `findpath` functionc
* fix: incorrect import
* fix: import
* fix: export `findpath` via package
* fix: typo
* feat: add ChromeDriver to `findpath`
* docs: chromedriver findpath example
* test: ChromeDriver DNE in normal build
* fix: chromedriver assert
* docs: remove `win | osx` from `hostOs` type def
* docs: chromedriver var naming
* refactor: code review changes
  • Loading branch information
ayushmanchhabra committed Oct 24, 2023
1 parent 86b52bc commit fc3c0ef
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 30 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -103,6 +103,16 @@ var nwpath = findpath();
// nwpath will equal the path to the binary depending on your environment
```

## finding the path to the chromedriver binary

If you would like to programmatically retrieve the path to the chromedriver use:

``` js
var findpath = require('nw').findpath;
var chromedriver_path = findpath('chromedriver');
// chromedriver_path will equal the path to the binary depending on your environment
```

Then you can use that path to run NW.js programmatically. For example, to run in the current script's directory:

```js
Expand Down
4 changes: 0 additions & 4 deletions index.js

This file was deleted.

20 changes: 0 additions & 20 deletions lib/findpath.js

This file was deleted.

58 changes: 58 additions & 0 deletions lib/findpath.mjs
@@ -0,0 +1,58 @@
import { dirname } from 'node:path';
import { env, platform } from 'node:process';
import { join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

/**
* Get the platform dependant path of the NW.js or ChromeDriver binary.
*
* @param {'nwjs' | 'chromedriver'} executable Path to NW.js or Chromedriver executable.
* @return {string}
*/
export default function findpath(executable = 'nwjs') {
const __dirname = dirname(fileURLToPath(import.meta.url));
const nwDir = resolve(__dirname, '..', 'nwjs');
/**
* File path to executable.
*
* @type {string}
*/
let binPath = '';

/**
* Host operating system
*
* @type {NodeJS.Platform}
*/
let hostOs = env.npm_config_nwjs_platform || env.NWJS_PLATFORM || platform;

/**
* Get the platform dependant path of the NW.js binary.
*/
function findNwjs() {
if (hostOs === 'darwin') {
binPath = join(nwDir, 'nwjs.app', 'Contents', 'MacOS', 'nwjs');
} else if (hostOs === 'win32') {
binPath = join(nwDir, 'nw.exe');
} else {
binPath = join(nwDir, 'nw');
}
}

/**
* Get the platform dependant path of the ChromeDriver binary.
*/
function findChromeDriver() {
binPath = resolve(nwDir, `chromedriver${platform === "win32" ? ".exe" : ""}`);
}

if (executable === 'nwjs') {
findNwjs();
} else if (executable === 'chromedriver') {
findChromeDriver();
} else {
console.error(`[ ERROR ] Expected nwjs or chromedriver, got ${executable}.`);
}

return binPath;
}
6 changes: 2 additions & 4 deletions package.json
Expand Up @@ -6,7 +6,7 @@
"type": "git",
"url": "git://github.com/nwjs/npm-installer.git"
},
"main": "index.js",
"main": "lib/findpath.mjs",
"bin": {
"nw": "bin/nw"
},
Expand All @@ -16,9 +16,7 @@
},
"files": [
"lib",
"bin",
"scripts",
"index.js"
"bin"
],
"author": {
"name": "Kyle Robinson Young",
Expand Down
8 changes: 6 additions & 2 deletions test/index.mjs
Expand Up @@ -2,8 +2,12 @@ import { strictEqual } from 'node:assert';
import { existsSync } from 'node:fs';
import test from 'node:test';

import findpath from '../lib/findpath.js';
import findpath from '../lib/findpath.mjs';

test('has downloaded and extracted', function() {
test('nwjs has downloaded and been extracted', function() {
strictEqual(existsSync(findpath()), true);
});

test('chromedriver does not exist in normal build', function() {
strictEqual(existsSync(findpath('chromedriver')), false);
});

0 comments on commit fc3c0ef

Please sign in to comment.