UNPKG

2.16 kBJavaScriptView Raw
1import fs from 'node:fs';
2import path from 'node:path';
3import process from 'node:process';
4import url from 'node:url';
5
6import parse from './parse.js';
7
8const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
9
10/**
11 * Check if file exists at specified path.
12 *
13 * @param {fs.PathLike} filePath - File path to check existence of
14 * @return {Promise<boolean>} `true` if exists, otherwise `false`
15 */
16async function fileExists(filePath) {
17 let exists = true;
18 try {
19 await fs.promises.stat(filePath);
20 } catch {
21 exists = false;
22 }
23 return exists;
24}
25
26const PLATFORM_KV = {
27 darwin: "osx",
28 linux: "linux",
29 win32: "win",
30};
31
32const ARCH_KV = {
33 x64: "x64",
34 ia32: "ia32",
35 arm64: "arm64",
36};
37
38const EXE_NAME = {
39 win: "nw.exe",
40 osx: "nwjs.app/Contents/MacOS/nwjs",
41 linux: "nw",
42};
43
44/**
45 * Get the platform dependant path of the NW.js or ChromeDriver binary.
46 *
47 * @param {'nwjs' | 'chromedriver'} executable Path to NW.js or Chromedriver executable.
48 * @return {Promise<string>}
49 */
50async function findpath(executable = 'nwjs', options = {}) {
51 options = await parse(options);
52 const nwDir = path.resolve(__dirname, '..', `nwjs${options.flavor === "sdk" ? "-sdk" : ""}-v${options.version}-${options.platform}-${options.arch}`);
53
54 /**
55 * File path to executable.
56 *
57 * @type {string}
58 */
59 let binPath = '';
60
61 /**
62 * Get the platform dependant path of the NW.js binary.
63 */
64 function findNwjs() {
65 binPath = path.resolve(nwDir, EXE_NAME[options.platform]);
66 }
67
68 /**
69 * Get the platform dependant path of the ChromeDriver binary.
70 */
71 function findChromeDriver() {
72 binPath = path.resolve(nwDir, `chromedriver${process.platform === "win32" ? ".exe" : ""}`);
73 }
74
75 if (executable === 'nwjs') {
76 findNwjs();
77 } else if (executable === 'chromedriver') {
78 findChromeDriver();
79 } else {
80 console.error(`[ ERROR ] Expected nwjs or chromedriver, got ${executable}.`);
81 }
82
83 return binPath;
84}
85
86export default { ARCH_KV, EXE_NAME, PLATFORM_KV, fileExists, findpath }