codify-plugin-lib 1.0.153 → 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 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 > 0) {
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,12 +1,12 @@
1
1
  import isObjectsEqual from 'lodash.isequal';
2
2
  import path from 'node:path';
3
- import { areArraysEqual, tildify, unhome, 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
6
  'directory': (a, b) => {
7
7
  const notCaseSensitive = process.platform === 'darwin';
8
- const transformedA = path.resolve(unhome(untildify(notCaseSensitive ? String(a).toLowerCase() : String(a))));
9
- const transformedB = path.resolve(unhome(untildify(notCaseSensitive ? String(b).toLowerCase() : String(b))));
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
10
  return transformedA === transformedB;
11
11
  },
12
12
  'number': (a, b) => Number(a) === Number(b),
@@ -53,8 +53,8 @@ export function resolveFnFromEqualsFnOrString(fnOrString) {
53
53
  }
54
54
  const ParameterTransformationDefaults = {
55
55
  'directory': {
56
- to: (a) => path.resolve(unhome(untildify(String(a)))),
57
- from: (a) => tildify(String(a)),
56
+ to: (a) => path.resolve(resolvePathWithVariables((untildify(String(a))))),
57
+ from: (a) => addVariablesToPath(tildify(String(a))),
58
58
  },
59
59
  'string': {
60
60
  to: String,
@@ -7,5 +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;
10
12
  export declare function unhome(pathWithHome: string): string;
11
13
  export declare function areArraysEqual(isElementEqual: ((desired: unknown, current: unknown) => boolean) | undefined, desired: unknown, current: unknown): boolean;
@@ -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,20 @@ 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
+ }
28
43
  export function unhome(pathWithHome) {
29
44
  return pathWithHome.includes('$HOME') ? pathWithHome.replaceAll('$HOME', os.homedir()) : pathWithHome;
30
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codify-plugin-lib",
3
- "version": "1.0.153",
3
+ "version": "1.0.154",
4
4
  "description": "Library plugin library",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
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 > 0) {
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, unhome, 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;
@@ -304,8 +304,8 @@ const ParameterEqualsDefaults: Partial<Record<ParameterSettingType, (a: unknown,
304
304
  'boolean': (a: unknown, b: unknown) => Boolean(a) === Boolean(b),
305
305
  'directory': (a: unknown, b: unknown) => {
306
306
  const notCaseSensitive = process.platform === 'darwin';
307
- const transformedA = path.resolve(unhome(untildify(notCaseSensitive ? String(a).toLowerCase() : String(a))))
308
- const transformedB = path.resolve(unhome(untildify(notCaseSensitive ? String(b).toLowerCase() : String(b))))
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
309
 
310
310
  return transformedA === transformedB;
311
311
  },
@@ -368,8 +368,8 @@ export function resolveFnFromEqualsFnOrString(
368
368
 
369
369
  const ParameterTransformationDefaults: Partial<Record<ParameterSettingType, InputTransformation>> = {
370
370
  'directory': {
371
- to: (a: unknown) => path.resolve(unhome(untildify(String(a)))),
372
- from: (a: unknown) => tildify(String(a)),
371
+ to: (a: unknown) => path.resolve(resolvePathWithVariables((untildify(String(a))))),
372
+ from: (a: unknown) => addVariablesToPath(tildify(String(a))),
373
373
  },
374
374
  'string': {
375
375
  to: String,
@@ -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
  })
@@ -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,24 @@ 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
+
40
59
  export function unhome(pathWithHome: string): string {
41
60
  return pathWithHome.includes('$HOME') ? pathWithHome.replaceAll('$HOME', os.homedir()) : pathWithHome;
42
61
  }