codify-plugin-lib 1.0.152 → 1.0.154
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/plan/plan.js +1 -1
- package/dist/resource/resource-settings.js +9 -4
- package/dist/utils/utils.d.ts +3 -0
- package/dist/utils/utils.js +18 -0
- package/package.json +1 -1
- package/src/plan/plan.ts +1 -1
- package/src/resource/resource-settings.ts +10 -4
- package/src/utils/utils.test.ts +23 -1
- package/src/utils/utils.ts +23 -0
package/dist/plan/plan.js
CHANGED
|
@@ -159,7 +159,7 @@ export class Plan {
|
|
|
159
159
|
const { matcher: parameterMatcher, id } = settings;
|
|
160
160
|
const matcher = (desired, currentArray) => {
|
|
161
161
|
const matched = currentArray.filter((c) => parameterMatcher(desired, c));
|
|
162
|
-
if (matched.length >
|
|
162
|
+
if (matched.length > 1) {
|
|
163
163
|
console.log(`Resource: ${id} did not uniquely match resources when allow multiple is set to true`);
|
|
164
164
|
}
|
|
165
165
|
return matched[0];
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import isObjectsEqual from 'lodash.isequal';
|
|
2
2
|
import path from 'node:path';
|
|
3
|
-
import { areArraysEqual, tildify, untildify } from '../utils/utils.js';
|
|
3
|
+
import { addVariablesToPath, areArraysEqual, resolvePathWithVariables, tildify, untildify } from '../utils/utils.js';
|
|
4
4
|
const ParameterEqualsDefaults = {
|
|
5
5
|
'boolean': (a, b) => Boolean(a) === Boolean(b),
|
|
6
|
-
'directory': (a, b) =>
|
|
6
|
+
'directory': (a, b) => {
|
|
7
|
+
const notCaseSensitive = process.platform === 'darwin';
|
|
8
|
+
const transformedA = path.resolve(resolvePathWithVariables(untildify(notCaseSensitive ? String(a).toLowerCase() : String(a))));
|
|
9
|
+
const transformedB = path.resolve(resolvePathWithVariables(untildify(notCaseSensitive ? String(b).toLowerCase() : String(b))));
|
|
10
|
+
return transformedA === transformedB;
|
|
11
|
+
},
|
|
7
12
|
'number': (a, b) => Number(a) === Number(b),
|
|
8
13
|
'string': (a, b) => String(a) === String(b),
|
|
9
14
|
'version': (desired, current) => String(current).includes(String(desired)),
|
|
@@ -48,8 +53,8 @@ export function resolveFnFromEqualsFnOrString(fnOrString) {
|
|
|
48
53
|
}
|
|
49
54
|
const ParameterTransformationDefaults = {
|
|
50
55
|
'directory': {
|
|
51
|
-
to: (a) => path.resolve(untildify(String(a))),
|
|
52
|
-
from: (a) => tildify(String(a)),
|
|
56
|
+
to: (a) => path.resolve(resolvePathWithVariables((untildify(String(a))))),
|
|
57
|
+
from: (a) => addVariablesToPath(tildify(String(a))),
|
|
53
58
|
},
|
|
54
59
|
'string': {
|
|
55
60
|
to: String,
|
package/dist/utils/utils.d.ts
CHANGED
|
@@ -7,4 +7,7 @@ export declare function splitUserConfig<T extends StringIndexedObject>(config: R
|
|
|
7
7
|
export declare function setsEqual(set1: Set<unknown>, set2: Set<unknown>): boolean;
|
|
8
8
|
export declare function untildify(pathWithTilde: string): string;
|
|
9
9
|
export declare function tildify(pathWithTilde: string): string;
|
|
10
|
+
export declare function resolvePathWithVariables(pathWithVariables: string): string;
|
|
11
|
+
export declare function addVariablesToPath(pathWithoutVariables: string): string;
|
|
12
|
+
export declare function unhome(pathWithHome: string): string;
|
|
10
13
|
export declare function areArraysEqual(isElementEqual: ((desired: unknown, current: unknown) => boolean) | undefined, desired: unknown, current: unknown): boolean;
|
package/dist/utils/utils.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import os from 'node:os';
|
|
2
|
+
import path from 'node:path';
|
|
2
3
|
export function isDebug() {
|
|
3
4
|
return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
|
|
4
5
|
}
|
|
@@ -25,6 +26,23 @@ export function untildify(pathWithTilde) {
|
|
|
25
26
|
export function tildify(pathWithTilde) {
|
|
26
27
|
return homeDirectory ? pathWithTilde.replace(homeDirectory, '~') : pathWithTilde;
|
|
27
28
|
}
|
|
29
|
+
export function resolvePathWithVariables(pathWithVariables) {
|
|
30
|
+
// @ts-expect-error Ignore this for now
|
|
31
|
+
return pathWithVariables.replace(/\$([A-Z_]+[A-Z0-9_]*)|\${([A-Z0-9_]*)}/ig, (_, a, b) => process.env[a || b]);
|
|
32
|
+
}
|
|
33
|
+
export function addVariablesToPath(pathWithoutVariables) {
|
|
34
|
+
let result = pathWithoutVariables;
|
|
35
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
36
|
+
if (!value || !path.isAbsolute(value) || value === '/') {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
result = result.replaceAll(value, `$${key}`);
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
export function unhome(pathWithHome) {
|
|
44
|
+
return pathWithHome.includes('$HOME') ? pathWithHome.replaceAll('$HOME', os.homedir()) : pathWithHome;
|
|
45
|
+
}
|
|
28
46
|
export function areArraysEqual(isElementEqual, desired, current) {
|
|
29
47
|
if (!desired || !current) {
|
|
30
48
|
return false;
|
package/package.json
CHANGED
package/src/plan/plan.ts
CHANGED
|
@@ -263,7 +263,7 @@ export class Plan<T extends StringIndexedObject> {
|
|
|
263
263
|
const { matcher: parameterMatcher, id } = settings;
|
|
264
264
|
const matcher = (desired: Partial<T>, currentArray: Partial<T>[]): Partial<T> | undefined => {
|
|
265
265
|
const matched = currentArray.filter((c) => parameterMatcher(desired, c))
|
|
266
|
-
if (matched.length >
|
|
266
|
+
if (matched.length > 1) {
|
|
267
267
|
console.log(`Resource: ${id} did not uniquely match resources when allow multiple is set to true`)
|
|
268
268
|
}
|
|
269
269
|
|
|
@@ -4,7 +4,7 @@ import isObjectsEqual from 'lodash.isequal'
|
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
|
|
6
6
|
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';
|
|
8
8
|
|
|
9
9
|
export interface InputTransformation {
|
|
10
10
|
to: (input: any) => Promise<any> | any;
|
|
@@ -302,7 +302,13 @@ export interface StatefulParameterSetting extends DefaultParameterSetting {
|
|
|
302
302
|
|
|
303
303
|
const ParameterEqualsDefaults: Partial<Record<ParameterSettingType, (a: unknown, b: unknown) => boolean>> = {
|
|
304
304
|
'boolean': (a: unknown, b: unknown) => Boolean(a) === Boolean(b),
|
|
305
|
-
'directory': (a: unknown, b: unknown) =>
|
|
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
|
+
},
|
|
306
312
|
'number': (a: unknown, b: unknown) => Number(a) === Number(b),
|
|
307
313
|
'string': (a: unknown, b: unknown) => String(a) === String(b),
|
|
308
314
|
'version': (desired: unknown, current: unknown) => String(current).includes(String(desired)),
|
|
@@ -362,8 +368,8 @@ export function resolveFnFromEqualsFnOrString(
|
|
|
362
368
|
|
|
363
369
|
const ParameterTransformationDefaults: Partial<Record<ParameterSettingType, InputTransformation>> = {
|
|
364
370
|
'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))),
|
|
367
373
|
},
|
|
368
374
|
'string': {
|
|
369
375
|
to: String,
|
package/src/utils/utils.test.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
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';
|
|
3
4
|
|
|
4
5
|
describe('Utils tests', () => {
|
|
5
6
|
it('Can split a config correctly', () => {
|
|
@@ -26,4 +27,25 @@ describe('Utils tests', () => {
|
|
|
26
27
|
propD: 'propD',
|
|
27
28
|
})
|
|
28
29
|
})
|
|
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
|
+
})
|
|
29
51
|
})
|
package/src/utils/utils.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ResourceConfig, StringIndexedObject } from 'codify-schemas';
|
|
2
2
|
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
3
4
|
|
|
4
5
|
export function isDebug(): boolean {
|
|
5
6
|
return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
|
|
@@ -37,6 +38,28 @@ export function tildify(pathWithTilde: string) {
|
|
|
37
38
|
return homeDirectory ? pathWithTilde.replace(homeDirectory, '~') : pathWithTilde;
|
|
38
39
|
}
|
|
39
40
|
|
|
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
|
+
|
|
40
63
|
export function areArraysEqual(
|
|
41
64
|
isElementEqual: ((desired: unknown, current: unknown) => boolean) | undefined,
|
|
42
65
|
desired: unknown,
|