@@ -21,26 +21,18 @@ import {
21
21
transformTrashQPCFs ,
22
22
transformTrashQPToTemplate ,
23
23
} from "./trash-guide" ;
24
- import { ArrType , CFProcessing , MappedMergedTemplates } from "./types/common.types" ;
25
- import {
26
- ConfigQualityProfile ,
27
- CustomFormatDefinitions ,
28
- InputConfigArrInstance ,
29
- InputConfigIncludeItem ,
30
- MergedConfigInstance ,
31
- } from "./types/config.types" ;
24
+ import { ArrType , MappedMergedTemplates } from "./types/common.types" ;
25
+ import { ConfigQualityProfile , InputConfigArrInstance , InputConfigIncludeItem , MergedConfigInstance } from "./types/config.types" ;
32
26
import { TrashQualityDefintion } from "./types/trashguide.types" ;
33
27
34
28
/**
35
29
* Load data from trash, recyclarr, custom configs and merge.
36
30
* Afterwards do sanitize and check against required configuration.
31
+ * TODO: probably move to config.ts and write tests for it for different merge scenarios
37
32
* @param value
38
33
* @param arrType
39
34
*/
40
- const mergeConfigsAndTemplates = async (
41
- value : InputConfigArrInstance ,
42
- arrType : ArrType ,
43
- ) : Promise < { mergedCFs : CFProcessing ; config : MergedConfigInstance } > => {
35
+ const mergeConfigsAndTemplates = async ( value : InputConfigArrInstance , arrType : ArrType ) : Promise < { config : MergedConfigInstance } > => {
44
36
const recyclarrTemplateMap = loadRecyclarrTemplates ( arrType ) ;
45
37
const localTemplateMap = loadLocalRecyclarrTemplate ( arrType ) ;
46
38
const trashTemplates = await loadQPFromTrash ( arrType ) ;
@@ -54,7 +46,7 @@ const mergeConfigsAndTemplates = async (
54
46
quality_profiles : [ ] ,
55
47
} ;
56
48
57
- // TODO: customFormatDefinitions not supported in templates yet
49
+ // HINT: we assume customFormatDefinitions only exist in RECYCLARR
58
50
if ( value . include ) {
59
51
const mappedIncludes = value . include . reduce < { recyclarr : InputConfigIncludeItem [ ] ; trash : InputConfigIncludeItem [ ] } > (
60
52
( previous , current ) => {
@@ -108,6 +100,17 @@ const mergeConfigsAndTemplates = async (
108
100
recyclarrMergedTemplates . media_naming = { ...recyclarrMergedTemplates . media_naming , ...template . media_naming } ;
109
101
}
110
102
103
+ if ( template . customFormatDefinitions ) {
104
+ if ( Array . isArray ( template . customFormatDefinitions ) ) {
105
+ recyclarrMergedTemplates . customFormatDefinitions = [
106
+ ...( recyclarrMergedTemplates . customFormatDefinitions || [ ] ) ,
107
+ ...template . customFormatDefinitions ,
108
+ ] ;
109
+ } else {
110
+ logger . warn ( `CustomFormatDefinitions in template must be an array. Ignoring.` ) ;
111
+ }
112
+ }
113
+
111
114
// TODO Ignore recursive include for now
112
115
if ( template . include ) {
113
116
logger . warn ( `Recursive includes not supported at the moment. Ignoring.` ) ;
@@ -128,6 +131,7 @@ const mergeConfigsAndTemplates = async (
128
131
} ) ;
129
132
}
130
133
134
+ // Config values overwrite template values
131
135
if ( value . custom_formats ) {
132
136
recyclarrMergedTemplates . custom_formats . push ( ...value . custom_formats ) ;
133
137
}
@@ -148,6 +152,17 @@ const mergeConfigsAndTemplates = async (
148
152
recyclarrMergedTemplates . quality_definition = { ...recyclarrMergedTemplates . quality_definition , ...value . quality_definition } ;
149
153
}
150
154
155
+ if ( value . customFormatDefinitions ) {
156
+ if ( Array . isArray ( value . customFormatDefinitions ) ) {
157
+ recyclarrMergedTemplates . customFormatDefinitions = [
158
+ ...( recyclarrMergedTemplates . customFormatDefinitions || [ ] ) ,
159
+ ...value . customFormatDefinitions ,
160
+ ] ;
161
+ } else {
162
+ logger . warn ( `CustomFormatDefinitions in config file must be an array. Ignoring.` ) ;
163
+ }
164
+ }
165
+
151
166
const recyclarrProfilesMerged = recyclarrMergedTemplates . quality_profiles . reduce < Map < string , ConfigQualityProfile > > ( ( p , c ) => {
152
167
const profile = p . get ( c . name ) ;
153
168
@@ -175,21 +190,6 @@ const mergeConfigsAndTemplates = async (
175
190
176
191
recyclarrMergedTemplates . quality_profiles = filterInvalidQualityProfiles ( recyclarrMergedTemplates . quality_profiles ) ;
177
192
178
- /*
179
- TODO: do we want to load all available local templates or only the included ones in the instance?
180
- Example: we have a local template folder which we can always traverse. So we could load every CF defined there.
181
- But then we could also have in theory conflicted CF IDs if user want to define same CF in different templates.
182
- How to handle overwrite? Maybe also support overriding CFs defined in Trash or something?
183
- */
184
- const localTemplateCFDs = Array . from ( localTemplateMap . values ( ) ) . reduce ( ( p , c ) => {
185
- if ( c . customFormatDefinitions ) {
186
- p . push ( ...c . customFormatDefinitions ) ;
187
- }
188
- return p ;
189
- } , [ ] as CustomFormatDefinitions ) ;
190
-
191
- const mergedCFs = await loadCustomFormatDefinitions ( arrType , localTemplateCFDs ) ;
192
-
193
193
// merge profiles from recyclarr templates into one
194
194
const qualityProfilesMerged = recyclarrMergedTemplates . quality_profiles . reduce ( ( p , c ) => {
195
195
let existingQp = p . get ( c . name ) ;
@@ -218,18 +218,33 @@ const mergeConfigsAndTemplates = async (
218
218
219
219
const validatedConfig = validateConfig ( recyclarrMergedTemplates ) ;
220
220
logger . debug ( `Merged config: '${ JSON . stringify ( validatedConfig ) } '` ) ;
221
- return { mergedCFs : mergedCFs , config : validatedConfig } ;
221
+
222
+ /*
223
+ TODO: do we want to load all available local templates or only the included ones in the instance?
224
+ Example: we have a local template folder which we can always traverse. So we could load every CF defined there.
225
+ But then we could also have in theory conflicted CF IDs if user want to define same CF in different templates.
226
+ How to handle overwrite? Maybe also support overriding CFs defined in Trash or something?
227
+ */
228
+ // const localTemplateCFDs = Array.from(localTemplateMap.values()).reduce((p, c) => {
229
+ // if (c.customFormatDefinitions) {
230
+ // p.push(...c.customFormatDefinitions);
231
+ // }
232
+ // return p;
233
+ // }, [] as CustomFormatDefinitions);
234
+
235
+ return { config : validatedConfig } ;
222
236
} ;
223
237
224
238
const pipeline = async ( value : InputConfigArrInstance , arrType : ArrType ) => {
225
239
const api = getUnifiedClient ( ) ;
226
240
227
- const { config, mergedCFs } = await mergeConfigsAndTemplates ( value , arrType ) ;
241
+ const { config } = await mergeConfigsAndTemplates ( value , arrType ) ;
228
242
229
243
const idsToManage = calculateCFsToManage ( config ) ;
230
-
231
244
logger . debug ( Array . from ( idsToManage ) , `CustomFormats to manage` ) ;
232
245
246
+ const mergedCFs = await loadCustomFormatDefinitions ( idsToManage , arrType , config . customFormatDefinitions || [ ] ) ;
247
+
233
248
let serverCFs = await loadServerCustomFormats ( ) ;
234
249
logger . info ( `CustomFormats on server: ${ serverCFs . length } ` ) ;
235
250
0 commit comments