Skip to content

Commit

Permalink
refactor: move json file reading to own util function
Browse files Browse the repository at this point in the history
* resolves node experimental warning
  • Loading branch information
BlackDark committed Oct 9, 2024
1 parent a89a7bc commit 1611b87
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 46 deletions.
16 changes: 5 additions & 11 deletions src/custom-formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { CustomFormatResource } from "./__generated__/generated-sonarr-api";
import { getArrApi } from "./api";
import { getConfig } from "./config";
import { logger } from "./logger";
import { CFProcessing, ConfigCustomFormatList, ConfigarrCF, DynamicImportType, TrashCF } from "./types";
import { IS_DRY_RUN, IS_LOCAL_SAMPLE_MODE, compareObjectsCarr, mapImportCfToRequestCf, toCarrCF } from "./util";
import { CFProcessing, ConfigCustomFormatList, ConfigarrCF, TrashCF } from "./types";
import { IS_DRY_RUN, IS_LOCAL_SAMPLE_MODE, compareObjectsCarr, loadJsonFile, mapImportCfToRequestCf, toCarrCF } from "./util";

export const deleteAllCustomFormats = async () => {
const api = getArrApi();
Expand All @@ -19,11 +19,7 @@ export const deleteAllCustomFormats = async () => {

export const loadServerCustomFormats = async (): Promise<CustomFormatResource[]> => {
if (IS_LOCAL_SAMPLE_MODE) {
return (
await import(path.resolve("./tests/samples/cfs.json"), {
with: { type: "json" },
})
).default as unknown as Promise<CustomFormatResource[]>;
return loadJsonFile<CustomFormatResource[]>(path.resolve(__dirname, "../tests/samples/cfs.json"));
}
const api = getArrApi();
const cfOnServer = await api.v3CustomformatList();
Expand Down Expand Up @@ -122,11 +118,9 @@ export const loadLocalCfs = async (): Promise<CFProcessing | null> => {

for (const file of files) {
const name = `${cfPath}/${file}`;
const cf: DynamicImportType<TrashCF | ConfigarrCF> = await import(`${name}`, {
with: { type: "json" },
});
const cf = loadJsonFile<TrashCF | ConfigarrCF>(path.resolve(name));

const cfD = toCarrCF(cf.default);
const cfD = toCarrCF(cf);

carrIdToObject.set(cfD.configarr_id, {
carrConfig: cfD,
Expand Down
8 changes: 2 additions & 6 deletions src/quality-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ import { QualityDefinitionResource } from "./__generated__/generated-sonarr-api"
import { getArrApi } from "./api";
import { logger } from "./logger";
import { TrashQualityDefintion, TrashQualityDefintionQuality } from "./types";
import { IS_LOCAL_SAMPLE_MODE } from "./util";
import { IS_LOCAL_SAMPLE_MODE, loadJsonFile } from "./util";

export const loadQualityDefinitionFromServer = async (): Promise<QualityDefinitionResource[]> => {
if (IS_LOCAL_SAMPLE_MODE) {
return (
await import(path.resolve("./tests/samples/qualityDefinition.json"), {
with: { type: "json" },
})
).default;
return loadJsonFile(path.resolve(__dirname, "../tests/samples/qualityDefinition.json"));
}
return (await getArrApi().v3QualitydefinitionList()).data as QualityDefinitionResource[];
};
Expand Down
8 changes: 2 additions & 6 deletions src/quality-profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { loadServerCustomFormats } from "./custom-formats";
import { logger } from "./logger";
import { loadQualityDefinitionFromServer } from "./quality-definitions";
import { CFProcessing, ConfigCustomFormat, ConfigQualityProfile, ConfigQualityProfileItem, RecyclarrMergedTemplates } from "./types";
import { IS_LOCAL_SAMPLE_MODE, cloneWithJSON, notEmpty } from "./util";
import { IS_LOCAL_SAMPLE_MODE, cloneWithJSON, loadJsonFile, notEmpty } from "./util";

export const mapQualityProfiles = (
{ carrIdMapping }: CFProcessing,
Expand Down Expand Up @@ -74,11 +74,7 @@ export const mapQualityProfiles = (

export const loadQualityProfilesFromServer = async (): Promise<QualityProfileResource[]> => {
if (IS_LOCAL_SAMPLE_MODE) {
return (
await import(path.resolve(`./tests/samples/quality_profiles.json`), {
with: { type: "json" },
})
).default;
return loadJsonFile(path.resolve(__dirname, `../tests/samples/quality_profiles.json`));
}
const api = getArrApi();

Expand Down
27 changes: 6 additions & 21 deletions src/trash-guide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import {
ArrType,
CFProcessing,
ConfigarrCF,
DynamicImportType,
QualityDefintionsRadarr,
QualityDefintionsSonarr,
TrashCF,
TrashQualityDefintion,
} from "./types";
import { mapImportCfToRequestCf, toCarrCF, trashRepoPaths } from "./util";
import { loadJsonFile, mapImportCfToRequestCf, toCarrCF, trashRepoPaths } from "./util";

const DEFAULT_TRASH_GIT_URL = "https://github.com/TRaSH-Guides/Guides";

Expand Down Expand Up @@ -73,11 +72,9 @@ export const loadSonarrTrashCFs = async (arrType: ArrType): Promise<CFProcessing
for (const file of files) {
const name = `${pathForFiles}/${file}`;

const cf: DynamicImportType<TrashCF> = await import(path.resolve(name), {
with: { type: "json" },
});
const cf = loadJsonFile<TrashCF>(path.resolve(name));

const carrConfig = toCarrCF(cf.default);
const carrConfig = toCarrCF(cf);

carrIdToObject.set(carrConfig.configarr_id, {
carrConfig: carrConfig,
Expand Down Expand Up @@ -105,23 +102,11 @@ export const loadQualityDefinitionSonarrFromTrash = async (

switch (qdType) {
case "anime":
return (
await import(path.resolve(`${trashPath}/anime.json`), {
with: { type: "json" },
})
).default;
return loadJsonFile(path.resolve(`${trashPath}/anime.json`));
case "series":
return (
await import(path.resolve(`${trashPath}/series.json`), {
with: { type: "json" },
})
).default;
return loadJsonFile(path.resolve(`${trashPath}/series.json`));
case "movie":
return (
await import(path.resolve(`${trashPath}/movie.json`), {
with: { type: "json" },
})
).default;
return loadJsonFile(path.resolve(`${trashPath}/movie.json`));
case "custom":
throw new Error("Not implemented yet");
default:
Expand Down
6 changes: 4 additions & 2 deletions src/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from "path";
import { describe, expect, test } from "vitest";
import { CustomFormatResource as CustomFormatResourceRadarr, PrivacyLevel } from "./__generated__/generated-radarr-api";
import { CustomFormatResource } from "./__generated__/generated-sonarr-api";
import { TrashCF, TrashCFSpF } from "./types";
import { cloneWithJSON, compareObjectsCarr, mapImportCfToRequestCf, toCarrCF } from "./util";
import { cloneWithJSON, compareObjectsCarr, loadJsonFile, mapImportCfToRequestCf, toCarrCF } from "./util";

const exampleCFImplementations = {
name: "TestSpec",
Expand Down Expand Up @@ -172,7 +173,8 @@ describe("SizeSpecification", async () => {
});

describe("compareObjectsCarr - general", async () => {
const serverResponse = (await import("../tests/samples/20240930_cf_exceptLanguage.json")) as CustomFormatResourceRadarr;
const filePath = path.resolve(__dirname, "../tests/samples/20240930_cf_exceptLanguage.json");
const serverResponse = loadJsonFile<CustomFormatResourceRadarr>(filePath);

const custom: TrashCF = {
trash_id: "test123",
Expand Down
6 changes: 6 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readFileSync } from "node:fs";
import path from "node:path";
import { CustomFormatResource } from "./__generated__/generated-sonarr-api";
import { logger } from "./logger";
Expand Down Expand Up @@ -188,3 +189,8 @@ export const cloneWithJSON = <T>(input: T): T => {
};

export const ROOT_PATH = path.resolve(process.cwd());

export const loadJsonFile = <T = object>(filePath: string) => {
const file = readFileSync(filePath, { encoding: "utf-8" });
return JSON.parse(file) as T;
};

0 comments on commit 1611b87

Please sign in to comment.