Skip to content

Commit e0871ac

Browse files
committed
refactor: split local path importer
1 parent ff2f08e commit e0871ac

File tree

3 files changed

+79
-27
lines changed

3 files changed

+79
-27
lines changed

src/index.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { MergedCustomFormatResource } from "./__generated__/mergedTypes";
55
import { configureApi, configureRadarrApi, configureSonarrApi, getArrApi, unsetApi } from "./api";
66
import { getConfig, validateConfig } from "./config";
77
import { calculateCFsToManage, loadCFFromConfig, loadLocalCfs, loadServerCustomFormats, manageCf, mergeCfSources } from "./custom-formats";
8+
import { loadLocalRecyclarrTemplate } from "./local-importer";
89
import { logHeading, logger } from "./logger";
910
import { calculateQualityDefinitionDiff, loadQualityDefinitionFromServer } from "./quality-definitions";
1011
import { calculateQualityProfilesDiff, filterInvalidQualityProfiles, loadQualityProfilesFromServer } from "./quality-profiles";
@@ -33,13 +34,19 @@ const mergeConfigsAndTemplates = async (
3334
arrType: ArrType,
3435
): Promise<{ mergedCFs: CFProcessing; config: MergedConfigInstance }> => {
3536
const recyclarrTemplateMap = loadRecyclarrTemplates(arrType);
37+
const localTemplateMap = loadLocalRecyclarrTemplate(arrType);
3638
const trashTemplates = await loadQPFromTrash(arrType);
3739

40+
logger.debug(
41+
`Loaded ${recyclarrTemplateMap.size} Recyclarr templates, ${localTemplateMap.size} local templates and ${trashTemplates.size} trash templates.`,
42+
);
43+
3844
const recylarrMergedTemplates: MappedMergedTemplates = {
3945
custom_formats: [],
4046
quality_profiles: [],
4147
};
4248

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

6269
logger.info(
63-
`Found ${value.include.length} templates to include: [recyclarr]=${mappedIncludes.recyclarr.length}, [trash]=${mappedIncludes.trash.length} ...`,
70+
`Found ${value.include.length} templates to include. Mapped to [recyclarr]=${mappedIncludes.recyclarr.length}, [trash]=${mappedIncludes.trash.length} ...`,
6471
);
6572

6673
mappedIncludes.recyclarr.forEach((e) => {
67-
const template = recyclarrTemplateMap.get(e.template);
74+
const template = recyclarrTemplateMap.get(e.template) ?? localTemplateMap.get(e.template);
6875

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

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

src/local-importer.ts

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { default as fs } from "node:fs";
2+
import path from "node:path";
3+
import yaml from "yaml";
4+
import { getConfig } from "./config";
5+
import { logger } from "./logger";
6+
import { ArrType, MappedTemplates } from "./types/common.types";
7+
import { RecyclarrTemplates } from "./types/recyclarr.types";
8+
9+
export const getLocalTemplatePath = () => {
10+
const config = getConfig();
11+
12+
if (config.localConfigTemplatesPath == null) {
13+
logger.debug(`No local templates specified. Skipping.`);
14+
return null;
15+
}
16+
17+
const customPath = path.resolve(config.localConfigTemplatesPath);
18+
19+
if (!fs.existsSync(customPath)) {
20+
logger.info(`Provided local templates path '${config.localCustomFormatsPath}' does not exist.`);
21+
return null;
22+
}
23+
24+
return customPath;
25+
};
26+
27+
export const loadLocalRecyclarrTemplate = (arrType: ArrType): Map<string, MappedTemplates> => {
28+
const map = new Map<string, RecyclarrTemplates>();
29+
30+
const fillMap = (path: string) => {
31+
const files = fs.readdirSync(`${path}`).filter((fn) => fn.endsWith("yaml") || fn.endsWith("yml"));
32+
33+
files.forEach((f) => map.set(f.substring(0, f.lastIndexOf(".")), yaml.parse(fs.readFileSync(`${path}/${f}`, "utf8"))));
34+
};
35+
36+
const localPath = getLocalTemplatePath();
37+
38+
if (localPath) {
39+
fillMap(localPath);
40+
}
41+
42+
logger.debug(`Found ${map.size} local templates.`);
43+
44+
return new Map(
45+
Array.from(map, ([k, v]) => {
46+
const customFormats = v.custom_formats?.map((cf) => {
47+
// Changes from Recyclarr 7.2.0: https://github.com/recyclarr/recyclarr/releases/tag/v7.2.0
48+
if (cf.assign_scores_to == null && cf.quality_profiles == null) {
49+
logger.warn(`Local Template "${k}" does not provide correct profile for custom format. Ignoring.`);
50+
}
51+
52+
if (cf.quality_profiles) {
53+
logger.warn(
54+
`Deprecated: (Local Template '${k}') For custom_formats please rename 'quality_profiles' to 'assign_scores_to'. See recyclarr v7.2.0`,
55+
);
56+
}
57+
return { ...cf, assign_scores_to: cf.assign_scores_to ?? cf.quality_profiles ?? [] };
58+
});
59+
60+
return [
61+
k,
62+
{
63+
...v,
64+
custom_formats: customFormats,
65+
},
66+
];
67+
}),
68+
);
69+
};

src/recyclarr-importer.ts

-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { default as fs } from "node:fs";
2-
import path from "node:path";
32
import yaml from "yaml";
43
import { getConfig } from "./config";
54
import { logger } from "./logger";
@@ -21,24 +20,6 @@ export const cloneRecyclarrTemplateRepo = async () => {
2120
logger.info(`Recyclarr repo: ref[${cloneResult.ref}], hash[${cloneResult.hash}], path[${cloneResult.localPath}]`);
2221
};
2322

24-
export const getLocalTemplatePath = () => {
25-
const config = getConfig();
26-
27-
if (config.localConfigTemplatesPath == null) {
28-
logger.debug(`No local templates specified. Skipping.`);
29-
return null;
30-
}
31-
32-
const customPath = path.resolve(config.localConfigTemplatesPath);
33-
34-
if (!fs.existsSync(customPath)) {
35-
logger.info(`Provided local templates path '${config.localCustomFormatsPath}' does not exist.`);
36-
return null;
37-
}
38-
39-
return customPath;
40-
};
41-
4223
export const loadRecyclarrTemplates = (arrType: ArrType): Map<string, MappedTemplates> => {
4324
const map = new Map<string, RecyclarrTemplates>();
4425

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

61-
const localPath = getLocalTemplatePath();
62-
63-
if (localPath) {
64-
fillMap(localPath);
65-
}
66-
6742
logger.debug(`Found ${map.size} Recyclarr templates.`);
6843

6944
return new Map(

0 commit comments

Comments
 (0)