Skip to content

Commit ef29ea2

Browse files
committed
feat: Improved transformations by providing the original back in from, that way the user has the option to return the original if they are the same. Added a custom refreshMapper to apply logic for generating the refresh keys
1 parent 2607ad1 commit ef29ea2

File tree

4 files changed

+57
-33
lines changed

4 files changed

+57
-33
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codify-plugin-lib",
3-
"version": "1.0.156",
3+
"version": "1.0.161",
44
"description": "Library plugin library",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",

src/resource/resource-controller.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -274,29 +274,7 @@ export class ResourceController<T extends StringIndexedObject> {
274274
await this.applyTransformParameters(parameters);
275275

276276
// Use refresh parameters if specified, otherwise try to refresh as many parameters as possible here
277-
const parametersToRefresh = this.settings.importAndDestroy?.refreshKeys
278-
? {
279-
...Object.fromEntries(
280-
this.settings.importAndDestroy?.refreshKeys.map((k) => [k, null])
281-
),
282-
...this.settings.importAndDestroy?.defaultRefreshValues,
283-
...parameters,
284-
...(Object.fromEntries( // If a default value was used, but it was also declared in the defaultRefreshValues, prefer the defaultRefreshValue instead
285-
Object.entries(parameters).filter(([k, v]) =>
286-
this.parsedSettings.defaultValues[k] !== undefined
287-
&& v === this.parsedSettings.defaultValues[k]
288-
&& context.originalDesiredConfig?.[k] === undefined
289-
&& this.settings.importAndDestroy?.defaultRefreshValues?.[k] !== undefined
290-
).map(([k]) => [k, this.settings.importAndDestroy!.defaultRefreshValues![k]])
291-
))
292-
}
293-
: {
294-
...Object.fromEntries(
295-
this.getAllParameterKeys().map((k) => [k, null])
296-
),
297-
...this.settings.importAndDestroy?.defaultRefreshValues,
298-
...parameters,
299-
};
277+
const parametersToRefresh = this.getParametersToRefreshForImport(parameters, context);
300278

301279
// Parse data from the user supplied config
302280
const parsedConfig = new ConfigParser(parametersToRefresh, null, this.parsedSettings.statefulParameters)
@@ -320,7 +298,7 @@ export class ResourceController<T extends StringIndexedObject> {
320298
?.map((r, idx) => ({ ...r, ...statefulCurrentParameters[idx] }))
321299

322300
for (const result of resultParametersArray) {
323-
await this.applyTransformParameters(result, true);
301+
await this.applyTransformParameters(result, { original: parameters });
324302
this.removeDefaultValues(result, parameters);
325303
}
326304

@@ -403,7 +381,7 @@ ${JSON.stringify(refresh, null, 2)}
403381
}
404382
}
405383

406-
private async applyTransformParameters(config: Partial<T> | null, reverse = false): Promise<void> {
384+
private async applyTransformParameters(config: Partial<T> | null, reverse?: { original: Partial<T> }): Promise<void> {
407385
if (!config) {
408386
return;
409387
}
@@ -414,13 +392,13 @@ ${JSON.stringify(refresh, null, 2)}
414392
}
415393

416394
(config as Record<string, unknown>)[key] = reverse
417-
? await inputTransformation.from(config[key])
395+
? await inputTransformation.from(config[key], reverse.original?.[key])
418396
: await inputTransformation.to(config[key]);
419397
}
420398

421399
if (this.settings.transformation) {
422400
const transformed = reverse
423-
? await this.settings.transformation.from({ ...config })
401+
? await this.settings.transformation.from({ ...config }, reverse.original)
424402
: await this.settings.transformation.to({ ...config })
425403

426404
Object.keys(config).forEach((k) => delete config[k])
@@ -523,5 +501,35 @@ ${JSON.stringify(refresh, null, 2)}
523501
? Object.keys((this.settings.schema as any)?.properties)
524502
: Object.keys(this.parsedSettings.parameterSettings);
525503
}
504+
505+
private getParametersToRefreshForImport(parameters: Partial<T>, context: RefreshContext<T>): Partial<T> {
506+
if (this.settings.importAndDestroy?.refreshMapper) {
507+
return this.settings.importAndDestroy?.refreshMapper(parameters, context);
508+
}
509+
510+
return this.settings.importAndDestroy?.refreshKeys
511+
? {
512+
...Object.fromEntries(
513+
this.settings.importAndDestroy?.refreshKeys.map((k) => [k, null])
514+
),
515+
...this.settings.importAndDestroy?.defaultRefreshValues,
516+
...parameters,
517+
...(Object.fromEntries( // If a default value was used, but it was also declared in the defaultRefreshValues, prefer the defaultRefreshValue instead
518+
Object.entries(parameters).filter(([k, v]) =>
519+
this.parsedSettings.defaultValues[k] !== undefined
520+
&& v === this.parsedSettings.defaultValues[k]
521+
&& context.originalDesiredConfig?.[k] === undefined
522+
&& this.settings.importAndDestroy?.defaultRefreshValues?.[k] !== undefined
523+
).map(([k]) => [k, this.settings.importAndDestroy!.defaultRefreshValues![k]])
524+
))
525+
}
526+
: {
527+
...Object.fromEntries(
528+
this.getAllParameterKeys().map((k) => [k, null])
529+
),
530+
...this.settings.importAndDestroy?.defaultRefreshValues,
531+
...parameters,
532+
};
533+
}
526534
}
527535

src/resource/resource-settings.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import path from 'node:path';
55

66
import { ArrayStatefulParameter, StatefulParameter } from '../stateful-parameter/stateful-parameter.js';
77
import { addVariablesToPath, areArraysEqual, resolvePathWithVariables, tildify, untildify } from '../utils/utils.js';
8+
import { RefreshContext } from './resource.js';
89

910
export interface InputTransformation {
1011
to: (input: any) => Promise<any> | any;
11-
from: (current: any) => Promise<any> | any;
12+
from: (current: any, original: any) => Promise<any> | any;
1213
}
1314

1415
/**
@@ -141,6 +142,15 @@ export interface ResourceSettings<T extends StringIndexedObject> {
141142
* See {@link importAndDestroy} for more information on how importing works.
142143
*/
143144
defaultRefreshValues?: Partial<T>;
145+
146+
/**
147+
* A custom function that maps the input to what gets passed to refresh for imports. If this is set, then refreshKeys and
148+
* defaultRefreshValues are ignored.
149+
*
150+
* @param input
151+
* @param context
152+
*/
153+
refreshMapper?: (input: Partial<T>, context: RefreshContext<T>) => Partial<T>
144154
}
145155
}
146156

@@ -369,7 +379,13 @@ export function resolveFnFromEqualsFnOrString(
369379
const ParameterTransformationDefaults: Partial<Record<ParameterSettingType, InputTransformation>> = {
370380
'directory': {
371381
to: (a: unknown) => path.resolve(resolvePathWithVariables((untildify(String(a))))),
372-
from: (a: unknown) => addVariablesToPath(tildify(String(a))),
382+
from: (a: unknown, original) => {
383+
if (ParameterEqualsDefaults.directory!(a, original)) {
384+
return original;
385+
}
386+
387+
return tildify(addVariablesToPath(String(a)))
388+
},
373389
},
374390
'string': {
375391
to: String,
@@ -406,8 +422,8 @@ export function resolveParameterTransformFn(
406422
to(input: unknown[]) {
407423
return input.map((i) => itemTransformation.to(i))
408424
},
409-
from(input: unknown[]) {
410-
return input.map((i) => itemTransformation.from(i))
425+
from(input: unknown[], original) {
426+
return input.map((i) => itemTransformation.from(i, original))
411427
}
412428
}
413429
}

src/utils/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function resolvePathWithVariables(pathWithVariables: string) {
4646
export function addVariablesToPath(pathWithoutVariables: string) {
4747
let result = pathWithoutVariables;
4848
for (const [key, value] of Object.entries(process.env)) {
49-
if (!value || !path.isAbsolute(value) || value === '/') {
49+
if (!value || !path.isAbsolute(value) || value === '/' || key === 'HOME' || key === 'PATH' || key === 'SHELL' || key === 'PWD') {
5050
continue;
5151
}
5252

0 commit comments

Comments
 (0)