/
helpers.ts
78 lines (63 loc) · 2.85 KB
/
helpers.ts
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
import { downloadBinaries, detectPlatform, getBinaryFilename } from 'ffbinaries';
import { getEnv, rebuildTypes, recursiveUpdate } from '@inrixia/helpers/object';
import { defaultArgs, defaultSettings } from './defaults.js';
import db from '@inrixia/db';
import fs from 'fs';
import ARGV from 'process.argv';
import 'dotenv/config';
import { parse } from 'json5';
import type { Args, PartialArgs, Settings } from './types.js';
import { FileCookieStore } from 'tough-cookie-file-store';
import { CookieJar } from 'tough-cookie';
export const cookieJar = new CookieJar(new FileCookieStore('./db/cookies.json'));
// ESM Hack
import type { ChalkInstance } from 'chalk';
export let chalk: ChalkInstance;
import type { Floatplane } from 'floatplane';
export let fApi: Floatplane;
export const esmOverload = (async () => {
chalk = (await eval("import('chalk')")).default;
fApi = new (await eval("import('floatplane')")).Floatplane(cookieJar);
})();
export const settings = db<Settings>('./db/settings.json', { template: defaultSettings, pretty: true, forceCreate: true, updateOnExternalChanges: true });
recursiveUpdate(settings, defaultSettings);
const argv = ARGV(process.argv.slice(2))<PartialArgs>({});
rebuildTypes<PartialArgs, Settings & Args>(argv, { ...defaultSettings, ...defaultArgs });
recursiveUpdate(settings, argv, { setUndefined: false, setDefined: true });
const env = getEnv();
rebuildTypes<PartialArgs, Settings & Args>(env, { ...defaultSettings, ...defaultArgs });
if (env.__FPDSettings !== undefined) recursiveUpdate(settings, parse(env.__FPDSettings.replaceAll('\\"', '"')), { setUndefined: false, setDefined: true });
recursiveUpdate(settings, env, { setUndefined: false, setDefined: true });
export const args = { ...argv, ...env };
// Override stdout if headless to not include formatting tags
if (args.headless === true) {
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
type StdoutArgs = Parameters<typeof process.stdout.write>;
process.stdout.write = ((...params: StdoutArgs) => {
// eslint-disable-next-line no-control-regex
if (typeof params[0] === 'string') params[0] = params[0].replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
return originalStdoutWrite(...params);
}) as typeof process.stdout.write;
}
export const fetchFFMPEG = (): Promise<void> =>
new Promise((resolve, reject) => {
const platform = detectPlatform();
const path = args.headless === true ? './' : './db/';
if (fs.existsSync(`${path}${getBinaryFilename('ffmpeg', platform)}`) === false) {
process.stdout.write('> Ffmpeg binary missing! Downloading... ');
downloadBinaries(
'ffmpeg',
{
destination: path,
platform,
},
(err) => {
if (err !== null) reject(err);
else {
process.stdout.write('\u001b[36mDone!\u001b[0m\n\n');
resolve();
}
}
);
} else resolve();
});