Skip to content

Commit 8a7fabe

Browse files
committed
feat: Bug fixes and improvements
1 parent 724818d commit 8a7fabe

File tree

7 files changed

+107
-54
lines changed

7 files changed

+107
-54
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.145",
3+
"version": "1.0.147",
44
"description": "Library plugin library",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",

src/plan/plan.ts

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -229,56 +229,6 @@ export class Plan<T extends StringIndexedObject> {
229229
return this.coreParameters.type
230230
}
231231

232-
/**
233-
* When multiples of the same resource are allowed, this matching function will match a given config with one of the
234-
* existing configs on the system. For example if there are multiple versions of Android Studios installed, we can use
235-
* the application name and location to match it to our desired configs name and location.
236-
*
237-
* @param params
238-
* @private
239-
*/
240-
private static matchCurrentParameters<T extends StringIndexedObject>(params: {
241-
desired: Partial<T> | null,
242-
currentArray: Partial<T>[] | null,
243-
state: Partial<T> | null,
244-
settings: ParsedResourceSettings<T>,
245-
isStateful: boolean,
246-
}): Partial<T> | null {
247-
const {
248-
desired,
249-
currentArray,
250-
state,
251-
settings,
252-
isStateful
253-
} = params;
254-
255-
if (!settings.allowMultiple) {
256-
return currentArray?.[0] ?? null;
257-
}
258-
259-
if (!currentArray) {
260-
return null;
261-
}
262-
263-
const { matcher: parameterMatcher, id } = settings;
264-
const matcher = (desired: Partial<T>, currentArray: Partial<T>[]): Partial<T> | undefined => {
265-
const matched = currentArray.filter((c) => parameterMatcher(desired, c))
266-
if (matched.length > 0) {
267-
console.log(`Resource: ${id} did not uniquely match resources when allow multiple is set to true`)
268-
}
269-
270-
return matched[0];
271-
}
272-
273-
if (isStateful) {
274-
return state
275-
? matcher(state, currentArray) ?? null
276-
: null
277-
}
278-
279-
return matcher(desired!, currentArray) ?? null;
280-
}
281-
282232
/**
283233
* Only keep relevant params for the plan. We don't want to change settings that were not already
284234
* defined.

src/plugin/plugin.test.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Resource } from '../resource/resource.js';
55
import { Plan } from '../plan/plan.js';
66
import { spy } from 'sinon';
77
import { ResourceSettings } from '../resource/resource-settings.js';
8-
import { TestConfig } from '../utils/test-utils.test.js';
8+
import { TestConfig, TestStatefulParameter } from '../utils/test-utils.test.js';
99
import { getPty } from '../pty/index.js';
1010

1111
interface TestConfig extends StringIndexedObject {
@@ -389,5 +389,63 @@ describe('Plugin tests', () => {
389389
parameters: { path: '/my/path', propA: 'hig' },
390390
})
391391

392+
const match2 = await testPlugin.match({
393+
resource: {
394+
core: { type: 'testResource' },
395+
parameters: { path: '/my/path', propA: 'abc' },
396+
},
397+
array: []
398+
})
399+
400+
expect(match2).toMatchObject({
401+
match: undefined,
402+
})
403+
})
404+
405+
it('Can match resources together 2', { timeout: 3000000 }, async () => {
406+
const resource = spy(new class extends TestResource {
407+
getSettings(): ResourceSettings<TestConfig> {
408+
return {
409+
id: 'ssh-config',
410+
parameterSettings: {
411+
hosts: { type: 'stateful', definition: new TestStatefulParameter() }
412+
},
413+
importAndDestroy: {
414+
refreshKeys: ['hosts'],
415+
defaultRefreshValues: { hosts: [] },
416+
requiredParameters: []
417+
},
418+
dependencies: ['ssh-key'],
419+
allowMultiple: {
420+
matcher: (a, b) => a.hosts === b.hosts
421+
}
422+
}
423+
}
424+
})
425+
426+
const testPlugin = Plugin.create('testPlugin', [resource as any]);
427+
428+
const { match } = await testPlugin.match({
429+
resource: {
430+
core: { type: 'ssh-config' },
431+
parameters: { hosts: 'a' },
432+
},
433+
array: [
434+
{
435+
core: { type: 'ssh-config' },
436+
parameters: { hosts: 'b' },
437+
},
438+
{
439+
core: { type: 'ssh-config' },
440+
parameters: { hosts: 'a' },
441+
},
442+
{
443+
core: { type: 'ssh-config' },
444+
parameters: { hosts: 'c' },
445+
},
446+
]
447+
})
448+
449+
console.log(match)
392450
})
393451
});

src/plugin/plugin.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ export class Plugin {
101101
throw new Error(`Resource of type ${resourceConfig.core.type} could not be found for match`);
102102
}
103103

104+
if (!resource.settings.allowMultiple) {
105+
return { match: array.find((r) => r.core.type === resourceConfig.core.type) }
106+
}
107+
104108
const parameterMatcher = resource?.parsedSettings.matcher;
105109
const match = array.find((r) => {
106110
if (resourceConfig.core.type !== r.core.type) {

src/resource/parsed-resource-settings.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ export class ParsedResourceSettings<T extends StringIndexedObject> implements Re
9292
...v,
9393
controller: spController,
9494
nestedSettings: spController?.parsedSettings,
95-
definition: undefined,
9695
};
9796

9897
return [k, parsed as ParsedStatefulParameterSetting];

src/resource/resource-controller.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ export class ResourceController<T extends StringIndexedObject> {
252252
|| this.settings.allowMultiple // Stateful parameters are not supported currently if allowMultiple is true
253253
|| currentParametersArray.filter(Boolean).length === 0
254254
) {
255+
for (const result of currentParametersArray ?? []) {
256+
await this.applyTransformParameters(result, true);
257+
this.removeDefaultValues(result, parameters)
258+
}
259+
255260
return currentParametersArray
256261
?.map((r) => ({ core, parameters: r }))
257262
?? null;
@@ -456,5 +461,39 @@ ${JSON.stringify(refresh, null, 2)}
456461
? Object.keys((this.settings.schema as any)?.properties)
457462
: Object.keys(this.parsedSettings.parameterSettings);
458463
}
464+
465+
/**
466+
* When multiples of the same resource are allowed, this matching function will match a given config with one of the
467+
* existing configs on the system. For example if there are multiple versions of Android Studios installed, we can use
468+
* the application name and location to match it to our desired configs name and location.
469+
*
470+
* @param params
471+
* @private
472+
*/
473+
private matchParameters(
474+
desired: Partial<T> | null,
475+
currentArray: Partial<T>[] | null
476+
): Partial<T> | null {
477+
if (!this.parsedSettings.allowMultiple) {
478+
return currentArray?.[0] ?? null;
479+
}
480+
481+
if (!currentArray) {
482+
return null;
483+
}
484+
485+
const { matcher: parameterMatcher, id } = this.parsedSettings;
486+
const matcher = (desired: Partial<T>, currentArray: Partial<T>[]): Partial<T> | undefined => {
487+
const matched = currentArray.filter((c) => parameterMatcher(desired, c))
488+
if (matched.length > 0) {
489+
console.log(`Resource: ${id} did not uniquely match resources when allow multiple is set to true`)
490+
}
491+
492+
return matched[0];
493+
}
494+
495+
return matcher(desired!, currentArray) ?? null;
496+
}
497+
459498
}
460499

src/resource/resource-settings.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,16 @@ export function resolveParameterTransformFn(
412412
export function resolveMatcher<T extends StringIndexedObject>(
413413
settings: ResourceSettings<T>
414414
): (desired: Partial<T>, current: Partial<T>) => boolean {
415-
416415
return typeof settings.allowMultiple === 'boolean' || !settings.allowMultiple?.matcher
417416
? ((desired: Partial<T>, current: Partial<T>) => {
418417
if (!desired || !current) {
419418
return false;
420419
}
421420

421+
if (!settings.allowMultiple) {
422+
throw new Error(`Matching only works when allow multiple is enabled. Type: ${settings.id}`)
423+
}
424+
422425
const requiredParameters = typeof settings.allowMultiple === 'object'
423426
? settings.allowMultiple?.identifyingParameters ?? (settings.schema?.required as string[]) ?? []
424427
: (settings.schema?.required as string[]) ?? []

0 commit comments

Comments
 (0)