Skip to content

Commit

Permalink
refactor: split local path importer
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackDark committed Nov 16, 2024
1 parent 932fbaf commit 37d9efc
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 27 deletions.
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MergedCustomFormatResource } from "./__generated__/mergedTypes";
import { configureApi, configureRadarrApi, configureSonarrApi, getArrApi, unsetApi } from "./api";
import { getConfig, validateConfig } from "./config";
import { calculateCFsToManage, loadCFFromConfig, loadLocalCfs, loadServerCustomFormats, manageCf, mergeCfSources } from "./custom-formats";
import { loadLocalRecyclarrTemplate } from "./local-importer";
import { logHeading, logger } from "./logger";
import { calculateQualityDefinitionDiff, loadQualityDefinitionFromServer } from "./quality-definitions";
import { calculateQualityProfilesDiff, filterInvalidQualityProfiles, loadQualityProfilesFromServer } from "./quality-profiles";
Expand Down Expand Up @@ -33,13 +34,19 @@ const mergeConfigsAndTemplates = async (
arrType: ArrType,
): Promise<{ mergedCFs: CFProcessing; config: MergedConfigInstance }> => {
const recyclarrTemplateMap = loadRecyclarrTemplates(arrType);
const localTemplateMap = loadLocalRecyclarrTemplate(arrType);
const trashTemplates = await loadQPFromTrash(arrType);

logger.debug(
`Loaded ${recyclarrTemplateMap.size} Recyclarr templates, ${localTemplateMap.size} local templates and ${trashTemplates.size} trash templates.`,
);

const recylarrMergedTemplates: MappedMergedTemplates = {
custom_formats: [],
quality_profiles: [],
};

// TODO: customFormatDefinitions not supported in templates yet
if (value.include) {
const mappedIncludes = value.include.reduce<{ recyclarr: InputConfigIncludeItem[]; trash: InputConfigIncludeItem[] }>(
(previous, current) => {
Expand All @@ -60,11 +67,11 @@ const mergeConfigsAndTemplates = async (
);

logger.info(
`Found ${value.include.length} templates to include: [recyclarr]=${mappedIncludes.recyclarr.length}, [trash]=${mappedIncludes.trash.length} ...`,
`Found ${value.include.length} templates to include. Mapped to [recyclarr]=${mappedIncludes.recyclarr.length}, [trash]=${mappedIncludes.trash.length} ...`,
);

mappedIncludes.recyclarr.forEach((e) => {
const template = recyclarrTemplateMap.get(e.template);
const template = recyclarrTemplateMap.get(e.template) ?? localTemplateMap.get(e.template);

if (!template) {
logger.warn(`Unknown recyclarr template requested: ${e.template}`);
Expand All @@ -91,6 +98,7 @@ const mergeConfigsAndTemplates = async (
}
});

// TODO: local trash guide QP templates do not work yet
mappedIncludes.trash.forEach((e) => {
const template = trashTemplates.get(e.template);

Expand Down
69 changes: 69 additions & 0 deletions src/local-importer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { default as fs } from "node:fs";
import path from "node:path";
import yaml from "yaml";
import { getConfig } from "./config";
import { logger } from "./logger";
import { ArrType, MappedTemplates } from "./types/common.types";
import { RecyclarrTemplates } from "./types/recyclarr.types";

export const getLocalTemplatePath = () => {
const config = getConfig();

if (config.localConfigTemplatesPath == null) {
logger.debug(`No local templates specified. Skipping.`);
return null;
}

const customPath = path.resolve(config.localConfigTemplatesPath);

if (!fs.existsSync(customPath)) {
logger.info(`Provided local templates path '${config.localCustomFormatsPath}' does not exist.`);
return null;
}

return customPath;
};

export const loadLocalRecyclarrTemplate = (arrType: ArrType): Map<string, MappedTemplates> => {
const map = new Map<string, RecyclarrTemplates>();

const fillMap = (path: string) => {
const files = fs.readdirSync(`${path}`).filter((fn) => fn.endsWith("yaml") || fn.endsWith("yml"));

files.forEach((f) => map.set(f.substring(0, f.lastIndexOf(".")), yaml.parse(fs.readFileSync(`${path}/${f}`, "utf8"))));
};

const localPath = getLocalTemplatePath();

if (localPath) {
fillMap(localPath);
}

logger.debug(`Found ${map.size} local templates.`);

return new Map(
Array.from(map, ([k, v]) => {
const customFormats = v.custom_formats?.map((cf) => {
// Changes from Recyclarr 7.2.0: https://github.com/recyclarr/recyclarr/releases/tag/v7.2.0
if (cf.assign_scores_to == null && cf.quality_profiles == null) {
logger.warn(`Local Template "${k}" does not provide correct profile for custom format. Ignoring.`);
}

if (cf.quality_profiles) {
logger.warn(
`Deprecated: (Local Template '${k}') For custom_formats please rename 'quality_profiles' to 'assign_scores_to'. See recyclarr v7.2.0`,
);
}
return { ...cf, assign_scores_to: cf.assign_scores_to ?? cf.quality_profiles ?? [] };
});

return [
k,
{
...v,
custom_formats: customFormats,
},
];
}),
);
};
25 changes: 0 additions & 25 deletions src/recyclarr-importer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { default as fs } from "node:fs";
import path from "node:path";
import yaml from "yaml";
import { getConfig } from "./config";
import { logger } from "./logger";
Expand All @@ -21,24 +20,6 @@ export const cloneRecyclarrTemplateRepo = async () => {
logger.info(`Recyclarr repo: ref[${cloneResult.ref}], hash[${cloneResult.hash}], path[${cloneResult.localPath}]`);
};

export const getLocalTemplatePath = () => {
const config = getConfig();

if (config.localConfigTemplatesPath == null) {
logger.debug(`No local templates specified. Skipping.`);
return null;
}

const customPath = path.resolve(config.localConfigTemplatesPath);

if (!fs.existsSync(customPath)) {
logger.info(`Provided local templates path '${config.localCustomFormatsPath}' does not exist.`);
return null;
}

return customPath;
};

export const loadRecyclarrTemplates = (arrType: ArrType): Map<string, MappedTemplates> => {
const map = new Map<string, RecyclarrTemplates>();

Expand All @@ -58,12 +39,6 @@ export const loadRecyclarrTemplates = (arrType: ArrType): Map<string, MappedTemp
fillMap(recyclarrRepoPaths.sonarrQP);
}

const localPath = getLocalTemplatePath();

if (localPath) {
fillMap(localPath);
}

logger.debug(`Found ${map.size} Recyclarr templates.`);

return new Map(
Expand Down

0 comments on commit 37d9efc

Please sign in to comment.