Skip to content

Commit

Permalink
Fix bugs with file config
Browse files Browse the repository at this point in the history
  • Loading branch information
mmgoodnow committed Dec 28, 2021
1 parent 4b7996f commit 485fbe1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 33 deletions.
4 changes: 1 addition & 3 deletions src/config.template.js → src/config.template.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// If you find yourself always using the same command-line flag, you can set
// it here as a default.

export default {
configVersion: 1,

module.exports = {
jackettServerUrl: "http://localhost:9117/jackett",
jackettApiKey: "YOUR_JACKETT_API_KEY_HERE",

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// All of the configuration can be overridden with command-line flags.
// If you find yourself always using the same command-line flag, you can set
// it here as a default.

export default {
module.exports = {
jackettServerUrl: "http://jackett:9117/jackett",
jackettApiKey: "YOUR_JACKETT_API_KEY_HERE",

Expand Down
35 changes: 8 additions & 27 deletions src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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";
import path from "path";
import { inspect } from "util";
import { Action } from "./constants.js";

const require = createRequire(import.meta.url);
const packageDotJson = require("../package.json");

Expand Down Expand Up @@ -51,43 +52,23 @@ export function generateConfig({
createAppDir();
const dest = path.join(appDir(), "config.js");
const templatePath = path.join(
__dirname,
`config.template${docker ? ".docker" : ""}.js`
`./config.template${docker ? ".docker" : ""}.cjs`
);
if (!force && existsSync(dest)) {
console.log("Configuration file already exists.");
return;
}
copyFileSync(templatePath, dest);
copyFileSync(new URL(templatePath, import.meta.url), 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;
return (await import(configPath)).default;
} catch (e) {
if (e.code !== "MODULE_NOT_FOUND") throw e;
if (e.code !== "ERR_MODULE_NOT_FOUND") throw e;
return {};
}
}
2 changes: 1 addition & 1 deletion src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function dropDatabase(): void {
db.data = emptyDatabase;
db.write();
unlinkSync(path.join(appDir(), "cache.json"));
rimraf.rimrafSync(path.join(appDir(), "torrent_cache"));
rimraf.sync(path.join(appDir(), "torrent_cache"));
}

export default db;

0 comments on commit 485fbe1

Please sign in to comment.