Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codifycli/plugin-core",
"version": "1.2.0",
"version": "1.2.1",
"description": "TypeScript library for building Codify plugins to manage system resources (applications, CLI tools, settings) through infrastructure-as-code",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
27 changes: 27 additions & 0 deletions src/plan/change-set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,33 @@ describe('Change set tests', () => {
expect(cs.operation).to.eq(ResourceOperation.DESTROY);
})

it('treats empty objects {} as no-op', () => {
const desired = {
keyboard: {},
dock: {},
}

const current = {
keyboard: {},
dock: {},
}

const cs = ChangeSet.calculateModification(desired, current);
expect(cs.parameterChanges.length).to.eq(0);
expect(cs.operation).to.eq(ResourceOperation.NOOP);
})

it('treats empty object {} as absent when current has no value', () => {
const desired = {
keyboard: {},
dock: {},
}

const cs = ChangeSet.calculateModification(desired, {});
expect(cs.parameterChanges.length).to.eq(0);
expect(cs.operation).to.eq(ResourceOperation.NOOP);
})

it('correctly determines array equality 5', () => {
const arrA = [{ key1: 'b' }, { key1: 'a' }, { key1: 'a' }];
const arrB = [{ key1: 'a' }, { key1: 'a' }, { key1: 'b' }];
Expand Down
13 changes: 10 additions & 3 deletions src/plan/change-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ export class ChangeSet<T extends StringIndexedObject> {
): ParameterChange<T>[] {
const parameterChangeSet = new Array<ParameterChange<T>>();

// Filter out null and undefined values or else the diff below will not work
// Filter out null, undefined, [], and {} — all treated as "no value"
const desired = Object.fromEntries(
Object.entries(desiredParameters).filter(([, v]) => v !== null && v !== undefined)
Object.entries(desiredParameters).filter(([, v]) => !ChangeSet.isAbsent(v))
) as Partial<T>

const current = Object.fromEntries(
Object.entries(currentParameters).filter(([, v]) => v !== null && v !== undefined)
Object.entries(currentParameters).filter(([, v]) => !ChangeSet.isAbsent(v))
) as Partial<T>

for (const k of new Set([...Object.keys(current), ...Object.keys(desired)])) {
Expand Down Expand Up @@ -227,6 +227,13 @@ export class ChangeSet<T extends StringIndexedObject> {
return orderOfOperations[Math.max(indexPrev, indexNext)];
}

private static isAbsent(v: unknown): boolean {
if (v === null || v === undefined) return true;
if (Array.isArray(v)) return v.length === 0;
if (typeof v === 'object') return Object.keys(v as object).length === 0;
return false;
}

private static isSame(
desired: unknown,
current: unknown,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function resolvePathWithVariables(pathWithVariables: string) {
export function addVariablesToPath(pathWithoutVariables: string) {
let result = pathWithoutVariables;
for (const [key, value] of Object.entries(process.env)) {
if (!value || !path.isAbsolute(value) || value === '/' || key === 'HOME' || key === 'PATH' || key === 'SHELL' || key === 'PWD') {
if (!value || !path.isAbsolute(value) || value === '/' || value === homeDirectory || key === 'HOME' || key === 'PATH' || key === 'SHELL' || key === 'PWD') {
continue;
}

Expand Down
Loading