Skip to content

Commit 4e20a5e

Browse files
committed
feat: Added variable resolution for paths. Changed match length to be greater than 1
1 parent 970c80f commit 4e20a5e

File tree

5 files changed

+58
-7
lines changed

5 files changed

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

src/plan/plan.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export class Plan<T extends StringIndexedObject> {
263263
const { matcher: parameterMatcher, id } = settings;
264264
const matcher = (desired: Partial<T>, currentArray: Partial<T>[]): Partial<T> | undefined => {
265265
const matched = currentArray.filter((c) => parameterMatcher(desired, c))
266-
if (matched.length > 0) {
266+
if (matched.length > 1) {
267267
console.log(`Resource: ${id} did not uniquely match resources when allow multiple is set to true`)
268268
}
269269

src/resource/resource-settings.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import isObjectsEqual from 'lodash.isequal'
44
import path from 'node:path';
55

66
import { ArrayStatefulParameter, StatefulParameter } from '../stateful-parameter/stateful-parameter.js';
7-
import { areArraysEqual, tildify, untildify } from '../utils/utils.js';
7+
import { addVariablesToPath, areArraysEqual, resolvePathWithVariables, tildify, untildify } from '../utils/utils.js';
88

99
export interface InputTransformation {
1010
to: (input: any) => Promise<any> | any;
@@ -302,7 +302,13 @@ export interface StatefulParameterSetting extends DefaultParameterSetting {
302302

303303
const ParameterEqualsDefaults: Partial<Record<ParameterSettingType, (a: unknown, b: unknown) => boolean>> = {
304304
'boolean': (a: unknown, b: unknown) => Boolean(a) === Boolean(b),
305-
'directory': (a: unknown, b: unknown) => path.resolve(untildify(String(a))) === path.resolve(untildify(String(b))),
305+
'directory': (a: unknown, b: unknown) => {
306+
const notCaseSensitive = process.platform === 'darwin';
307+
const transformedA = path.resolve(resolvePathWithVariables(untildify(notCaseSensitive ? String(a).toLowerCase() : String(a))))
308+
const transformedB = path.resolve(resolvePathWithVariables(untildify(notCaseSensitive ? String(b).toLowerCase() : String(b))))
309+
310+
return transformedA === transformedB;
311+
},
306312
'number': (a: unknown, b: unknown) => Number(a) === Number(b),
307313
'string': (a: unknown, b: unknown) => String(a) === String(b),
308314
'version': (desired: unknown, current: unknown) => String(current).includes(String(desired)),
@@ -362,8 +368,8 @@ export function resolveFnFromEqualsFnOrString(
362368

363369
const ParameterTransformationDefaults: Partial<Record<ParameterSettingType, InputTransformation>> = {
364370
'directory': {
365-
to: (a: unknown) => path.resolve(untildify(String(a))),
366-
from: (a: unknown) => tildify(String(a)),
371+
to: (a: unknown) => path.resolve(resolvePathWithVariables((untildify(String(a))))),
372+
from: (a: unknown) => addVariablesToPath(tildify(String(a))),
367373
},
368374
'string': {
369375
to: String,

src/utils/utils.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from 'vitest';
2-
import { splitUserConfig } from './utils.js';
2+
import { addVariablesToPath, resolvePathWithVariables, splitUserConfig } from './utils.js';
3+
import os from 'node:os';
34

45
describe('Utils tests', () => {
56
it('Can split a config correctly', () => {
@@ -26,4 +27,25 @@ describe('Utils tests', () => {
2627
propD: 'propD',
2728
})
2829
})
30+
31+
it('Can remove variables from a path', () => {
32+
const testPath1 = '$HOME/my/path';
33+
const result1 = resolvePathWithVariables(testPath1);
34+
35+
const home = os.homedir();
36+
expect(result1).to.eq(home + '/my/path');
37+
38+
39+
const testPath2 = '/var$HOME/my/path';
40+
const result2 = resolvePathWithVariables(testPath2);
41+
expect(result2).to.eq('/var' + home + '/my/path');
42+
})
43+
44+
it('Can add variables to a path', () => {
45+
const testPath1 = os.homedir() + '/my/path';
46+
const result1 = addVariablesToPath(testPath1);
47+
48+
const home = os.homedir();
49+
expect(result1).to.eq('$HOME/my/path');
50+
})
2951
})

src/utils/utils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ResourceConfig, StringIndexedObject } from 'codify-schemas';
22
import os from 'node:os';
3+
import path from 'node:path';
34

45
export function isDebug(): boolean {
56
return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
@@ -37,6 +38,28 @@ export function tildify(pathWithTilde: string) {
3738
return homeDirectory ? pathWithTilde.replace(homeDirectory, '~') : pathWithTilde;
3839
}
3940

41+
export function resolvePathWithVariables(pathWithVariables: string) {
42+
// @ts-expect-error Ignore this for now
43+
return pathWithVariables.replace(/\$([A-Z_]+[A-Z0-9_]*)|\${([A-Z0-9_]*)}/ig, (_, a, b) => process.env[a || b])
44+
}
45+
46+
export function addVariablesToPath(pathWithoutVariables: string) {
47+
let result = pathWithoutVariables;
48+
for (const [key, value] of Object.entries(process.env)) {
49+
if (!value || !path.isAbsolute(value) || value === '/') {
50+
continue;
51+
}
52+
53+
result = result.replaceAll(value, `$${key}`)
54+
}
55+
56+
return result;
57+
}
58+
59+
export function unhome(pathWithHome: string): string {
60+
return pathWithHome.includes('$HOME') ? pathWithHome.replaceAll('$HOME', os.homedir()) : pathWithHome;
61+
}
62+
4063
export function areArraysEqual(
4164
isElementEqual: ((desired: unknown, current: unknown) => boolean) | undefined,
4265
desired: unknown,

0 commit comments

Comments
 (0)