forked from TheBrenny/sans-video-ripper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
135 lines (125 loc) · 4.89 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const fs = require("fs");
const pj = require("path").join;
const colors = {
"black": 30,
"red": 31,
"green": 32,
"yellow": 33,
"blue": 34,
"magenta": 35,
"cyan": 36,
"white": 37,
"default": 39,
"reset": 0,
"brightBlack": 90,
"brightRed": 91,
"brightGreen": 92,
"brightYellow": 93,
"brightBlue": 94,
"brightMagenta": 95,
"brightCyan": 96,
"brightWhite": 97,
}
/** @param {keyof colors} color */
function color(color) {
return `\x1b[${colors[color]}m`
}
function nullOrUndefined(v) {
return v === null || v === undefined;
}
async function filterAsync(array, predicate) {
const results = await Promise.all(array.map(predicate));
return array.filter((_v, i) => results[i]);
}
Object.defineProperty(Array.prototype, "filterAsync", {
value(predicate) {return filterAsync(this, predicate);},
enumerable: false,
});
function cookieSplit(cookie) {
return [cookie.substring(0, cookie.indexOf("=")), cookie.substring(cookie.indexOf("=") + 1)];
}
function tryRequire(...packages) {
for(let package of packages) {
let p;
try {
p = require(package);
} catch(e) {
if(e.code == "MODULE_NOT_FOUND") continue;
else throw e;
}
return p;
}
}
// Functions so we can use process.env across all systems but ONLY when we need to (we don't want to `path.join(undefined, "blah")` )
/** @type {Object<"win32"|"darwin"|"linux", Function<Array<String>>> } */
const browserLocs = {
win32: () => [
// Chrome
pj(process.env["ProgramFiles"], "Google", "Chrome", "Application", "chrome.exe"),
pj(process.env["ProgramFiles(x86)"], "Google", "Chrome", "Application", "chrome.exe"),
pj(process.env["LocalAppData"], "Google", "Chrome", "Application", "chrome.exe"),
pj(process.env["ProgramFiles(x86)"], "Google", "Application", "chrome.exe"),
// Edge
pj(process.env["SystemRoot"], "SystemApps", "Microsoft.MicrosoftEdge_8wekyb3d8bbwe", "MicrosoftEdge.exe"),
pj(process.env["ProgramFiles"], "Microsoft", "Edge", "Application", "msedge.exe"),
pj(process.env["ProgramFiles(x86)"], "Microsoft", "Edge", "Application", "msedge.exe"),
pj(process.env["LocalAppData"], "Microsoft", "Edge", "Application", "msedge.exe"),
pj(process.env["LocalAppData"], "MicrosoftEdge", "Application", "msedge.exe"),
// Firefox
pj(process.env["ProgramFiles"], "Mozilla Firefox", "firefox.exe"),
pj(process.env["ProgramFiles(x86)"], "Mozilla Firefox", "firefox.exe"),
pj(process.env["LocalAppData"], "Mozilla Firefox", "firefox.exe"),
],
darwin: () => [
// Chrome
pj("Applications", "Google Chrome.app", "Contents", "MacOS", "Google Chrome"),
// Edge
pj("Applications", "Microsoft Edge.app", "Contents", "MacOS", "Microsoft Edge"),
// Firefox
pj("Applications", "Firefox.app", "Contents", "MacOS", "firefox"),
pj("Applications", "Firefox.app", "Contents", "MacOS", "firefox-bin"),
],
linux: () => [
// Chrome
pj("snap", "bin", "chromium"),
pj("usr", "bin", "chromium"),
pj("usr", "share", "applications", "chromium"),
pj("usr", "bin", "google-chrome-stable"),
pj("usr", "share", "applications", "google-chrome-stable"),
pj("usr", "bin", "google-chrome"),
pj("usr", "share", "applications", "google-chrome"),
pj("opt", "google", "chrome", "google-chrome"),
// Edge
pj("snap", "bin", "msedge"),
pj("usr", "bin", "msedge"),
pj("usr", "share", "applications", "msedge"),
pj("usr", "bin", "microsoft-edge-stable"),
pj("usr", "share", "applications", "microsoft-edge-stable"),
pj("usr", "bin", "microsoft-edge"),
pj("usr", "share", "applications", "microsoft-edge"),
pj("opt", "microsoft", "edge", "microsoft-edge"),
// Firefox
pj("snap", "bin", "firefox"),
pj("usr", "lib", "firefox", "firefox"),
pj("usr", "bin", "firefox"),
pj("usr", "share", "applications", "firefox"),
pj("opt", "firefox", "firefox"),
],
}
function getBrowserExecutable(path) {
if(fs.existsSync(path)) return path;
let paths = browserLocs[process.platform]?.() ?? browserLocs.linux();
for(let p of paths) if(fs.existsSync(p)) return p;
throw new Error("No Chromium/Firefox executable was found. Please specify using the --browser argument.");
}
module.exports = {
log: (s) => console.log(`${s}...`),
color,
write: (value) => process.stdout.write(value),
writeAt: (x, value) => process.stdout.write(`\x1b[${x}G${value}`),
moveTo: (x) => process.stdout.write(`\x1b[${x}G`),
nullOrUndefined,
cookieSplit,
tryRequire,
getBrowserExecutable,
}