1 | import fs from 'node:fs';
|
2 | import path from 'node:path';
|
3 | import process from 'node:process';
|
4 | import url from 'node:url';
|
5 |
|
6 | import parse from './parse.js';
|
7 |
|
8 | const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | async 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 |
|
26 | const PLATFORM_KV = {
|
27 | darwin: "osx",
|
28 | linux: "linux",
|
29 | win32: "win",
|
30 | };
|
31 |
|
32 | const ARCH_KV = {
|
33 | x64: "x64",
|
34 | ia32: "ia32",
|
35 | arm64: "arm64",
|
36 | };
|
37 |
|
38 | const EXE_NAME = {
|
39 | win: "nw.exe",
|
40 | osx: "nwjs.app/Contents/MacOS/nwjs",
|
41 | linux: "nw",
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | async 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 |
|
56 |
|
57 |
|
58 |
|
59 | let binPath = '';
|
60 |
|
61 | |
62 |
|
63 |
|
64 | function findNwjs() {
|
65 | binPath = path.resolve(nwDir, EXE_NAME[options.platform]);
|
66 | }
|
67 |
|
68 | |
69 |
|
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 |
|
86 | export default { ARCH_KV, EXE_NAME, PLATFORM_KV, fileExists, findpath }
|