-
Notifications
You must be signed in to change notification settings - Fork 65
/
configuration.ts
93 lines (85 loc) · 2.57 KB
/
configuration.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import chalk from "chalk";
import { copyFileSync, existsSync, mkdirSync } from "fs";
import path from "path";
import configTemplate from "./config.template.js";
import { Action, CONFIG_TEMPLATE_URL } from "./constants.js";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const packageDotJson = require("../package.json");
interface FileConfig {
action?: Action;
configVersion?: number;
delay?: number;
includeEpisodes?: boolean;
jackettApiKey?: string;
jackettServerUrl?: string;
outputDir?: string;
rtorrentRpcUrl?: string;
searchAll?: boolean;
excludeOlder?: number;
excludeRecentSearch?: number;
torrentDir?: string;
trackers?: string[];
qbittorrentUrl?: string;
notificationWebhookUrl?: string;
}
interface GenerateConfigParams {
force?: boolean;
docker?: boolean;
}
export function appDir(): string {
return (
process.env.CONFIG_DIR ||
(process.platform === "win32"
? path.resolve(process.env.LOCALAPPDATA, packageDotJson.name)
: path.resolve(process.env.HOME, `.${packageDotJson.name}`))
);
}
export function createAppDir(): void {
mkdirSync(path.join(appDir(), "torrent_cache"), { recursive: true });
mkdirSync(path.join(appDir(), "logs"), { recursive: true });
}
export function generateConfig({
force = false,
docker = false,
}: GenerateConfigParams): void {
createAppDir();
const dest = path.join(appDir(), "config.js");
const templatePath = path.join(
__dirname,
`config.template${docker ? ".docker" : ""}.js`
);
if (!force && existsSync(dest)) {
console.log("Configuration file already exists.");
return;
}
copyFileSync(templatePath, dest);
console.log("Configuration file created at", chalk.yellow.bold(dest));
}
function printUpdateInstructions(missingKeys) {
const configPath = path.join(appDir(), "config.js");
console.error(chalk.yellow`
Error: Your configuration file is out of date.
Missing options:\n\t${missingKeys.join("\n\t")}
Please update at ${configPath}.
When you are done, set the configVersion to ${configTemplate.configVersion}.
It may help to read the template, at ${CONFIG_TEMPLATE_URL}
`);
}
export async function getFileConfig(): Promise<FileConfig> {
const configPath = path.join(appDir(), "config.js");
try {
const fileConfig = (await import(configPath)).default;
const { configVersion = 0 } = fileConfig;
if (configVersion < configVersion) {
const missingKeys = Object.keys(configTemplate).filter(
(k) => !(k in fileConfig)
);
printUpdateInstructions(missingKeys);
}
return fileConfig;
} catch (e) {
if (e.code !== "MODULE_NOT_FOUND") throw e;
return {};
}
}