Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc. fixes to make packaging easier #222

Merged
merged 11 commits into from
Dec 24, 2024
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"json5": "^2.2.3",
"mime-types": "^3.0.0",
"multi-progress-bars": "^5.0.3",
"postject": "1.0.0-alpha.6",
"process.argv": "^0.6.1",
"prom-client": "^15.1.3",
"prompts": "^2.4.2",
Expand Down Expand Up @@ -66,4 +67,4 @@
"got": "^14.4.3",
"typescript": "^5.7.0-beta"
}
}
}
353 changes: 189 additions & 164 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sea-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
"assets": {
"./version": "./dist/version"
}
}
}
4 changes: 2 additions & 2 deletions src/lib/Attachment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import db from "@inrixia/db";
import { nPad } from "@inrixia/helpers/math";
import { ValueOfA } from "@inrixia/helpers/ts";
import { settings } from "./helpers/index.js";
import { settings, args } from "./helpers/index.js";
import sanitize from "sanitize-filename";

import { dirname, basename, extname } from "path";
Expand Down Expand Up @@ -31,7 +31,7 @@ enum Extensions {
}

export class Attachment implements AttachmentAttributes {
private static readonly AttachmentsDB: Record<string, AttachmentInfo> = db<Record<string, AttachmentInfo>>(`./db/attachments.json`);
private static readonly AttachmentsDB: Record<string, AttachmentInfo> = db<Record<string, AttachmentInfo>>(`${args.dbPath}/attachments.json`);
Noodlez1232 marked this conversation as resolved.
Show resolved Hide resolved
public static readonly Extensions = Extensions;

public readonly attachmentId: string;
Expand Down
4 changes: 3 additions & 1 deletion src/lib/Video.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { updatePlex } from "./helpers/updatePlex.js";
import { ProgressHeadless } from "./logging/ProgressConsole.js";
import { ProgressBars } from "./logging/ProgressBars.js";

import { ffmpegPath } from "./helpers/fetchFFMPEG.js";

const exec = promisify(execCallback);
const sleep = promisify(setTimeout);

Expand Down Expand Up @@ -363,7 +365,7 @@ export class Video extends Attachment {

await new Promise((resolve, reject) =>
execFile(
"./db/ffmpeg",
ffmpegPath,
[
"-i",
this.partialPath,
Expand Down
3 changes: 3 additions & 0 deletions src/lib/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export const defaultArgs: Args = {
plexUsername: "",
plexPassword: "",
sanityCheck: false,
dbPath: "./db",
ffmpegPath: "",
settingsPath: "",
};

export const defaultSettings: Settings = {
Expand Down
9 changes: 6 additions & 3 deletions src/lib/helpers/fetchFFMPEG.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { downloadBinaries, detectPlatform, getBinaryFilename } from "ffbinaries";
import { args } from "./index.js";
import fs from "fs";
import { dirname } from "path";

export const ffmpegPath = args.ffmpegPath || `${args.dbPath}/${getBinaryFilename("ffmpeg", detectPlatform())}`;

export const fetchFFMPEG = (): Promise<void> =>
new Promise((resolve, reject) => {
const platform = detectPlatform();
const path = "./db/";
if (fs.existsSync(`${path}${getBinaryFilename("ffmpeg", platform)}`) === false) {
Noodlez1232 marked this conversation as resolved.
Show resolved Hide resolved
if (!fs.existsSync(ffmpegPath)) {
process.stdout.write("> Ffmpeg binary missing! Downloading... ");
downloadBinaries(
"ffmpeg",
{
destination: path,
destination: dirname(ffmpegPath),
platform,
},
(err) => {
Expand Down
47 changes: 26 additions & 21 deletions src/lib/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getEnv, rebuildTypes, recursiveUpdate } from "@inrixia/helpers/object";
Noodlez1232 marked this conversation as resolved.
Show resolved Hide resolved
import { getEnv, recursiveUpdate, rebuildTypes } from "@inrixia/helpers/object";
import { defaultArgs, defaultSettings } from "../defaults.js";
import { Histogram } from "prom-client";
import db from "@inrixia/db";
Expand All @@ -22,9 +22,33 @@ import type { 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"));

import { Floatplane } from "floatplane";

const argv = ARGV(process.argv.slice(2))<PartialArgs>({});
const env = getEnv();

export const args = { ...defaultArgs };
recursiveUpdate(args, env, { setUndefined: false, setDefined: true });
recursiveUpdate(args, argv, { setUndefined: false, setDefined: true });
rebuildTypes(args, defaultArgs);

const settingsPath = args.settingsPath || `${args.dbPath}/settings.json`;

export const settings = { ...defaultSettings };
const newSettings = db<Settings>(settingsPath, { template: defaultSettings, pretty: true, forceCreate: true, updateOnExternalChanges: true });
recursiveUpdate(settings, newSettings, { setUndefined: true, setDefined: true });
recursiveUpdate(settings, argv, { setUndefined: false, setDefined: true });
rebuildTypes(settings, defaultSettings);

if (env.__FPDSettings !== undefined) {
if (typeof env.__FPDSettings !== "string") throw new Error("The __FPDSettings environment variable cannot be parsed!");
recursiveUpdate(settings, parse(env.__FPDSettings.replaceAll('\\"', '"')), { setUndefined: false, setDefined: true });
}

recursiveUpdate(settings, env, { setUndefined: false, setDefined: true });

export const cookieJar = new CookieJar(new FileCookieStore(`${args.dbPath}/cookies.json`));
export const fApi = new Floatplane(
cookieJar,
`Floatplane-Downloader/${DownloaderVersion} (Inrix, +https://github.com/Inrixia/Floatplane-Downloader), CFNetwork`,
Expand Down Expand Up @@ -58,25 +82,6 @@ fApi.extend({
},
});

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(argv, { ...defaultSettings, ...defaultArgs });
Noodlez1232 marked this conversation as resolved.
Show resolved Hide resolved
recursiveUpdate(settings, argv, { setUndefined: false, setDefined: true });

const env = getEnv();
rebuildTypes(env, { ...defaultSettings, ...defaultArgs });

if (env.__FPDSettings !== undefined) {
if (typeof env.__FPDSettings !== "string") throw new Error("The __FPDSettings environment variable cannot be parsed!");
recursiveUpdate(settings, parse(env.__FPDSettings.replaceAll('\\"', '"')), { setUndefined: false, setDefined: true });
}

recursiveUpdate(settings, env, { setUndefined: false, setDefined: true });

export const args = { ...argv, ...env };

// eslint-disable-next-line no-control-regex
const headlessStdoutRegex = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
// Override stdout if headless to not include formatting tags
Expand Down
3 changes: 3 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export type Args = {
plexUsername: string;
plexPassword: string;
sanityCheck: boolean;
dbPath: string;
ffmpegPath: string;
settingsPath: string;
};

export type PartialArgs = Partial<Args & Settings>;
Expand Down