codify-plugin-lib 1.0.182-beta2 → 1.0.182-beta4

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/index.d.ts CHANGED
@@ -11,4 +11,6 @@ export * from './resource/resource.js';
11
11
  export * from './resource/resource-settings.js';
12
12
  export * from './stateful-parameter/stateful-parameter.js';
13
13
  export * from './utils/index.js';
14
+ export * from './utils/verbosity-level.js';
15
+ export * from './utils/functions.js';
14
16
  export declare function runPlugin(plugin: Plugin): Promise<void>;
package/dist/index.js CHANGED
@@ -11,6 +11,8 @@ export * from './resource/resource.js';
11
11
  export * from './resource/resource-settings.js';
12
12
  export * from './stateful-parameter/stateful-parameter.js';
13
13
  export * from './utils/index.js';
14
+ export * from './utils/verbosity-level.js';
15
+ export * from './utils/functions.js';
14
16
  export async function runPlugin(plugin) {
15
17
  const messageHandler = new MessageHandler(plugin);
16
18
  process.on('message', (message) => messageHandler.onMessage(message));
@@ -4,8 +4,8 @@ import { BackgroundPty } from '../pty/background-pty.js';
4
4
  import { getPty } from '../pty/index.js';
5
5
  import { SequentialPty } from '../pty/seqeuntial-pty.js';
6
6
  import { ResourceController } from '../resource/resource-controller.js';
7
- import { VerbosityLevel } from '../utils/internal-utils.js';
8
7
  import { ptyLocalStorage } from '../utils/pty-local-storage.js';
8
+ import { VerbosityLevel } from '../utils/verbosity-level.js';
9
9
  export class Plugin {
10
10
  name;
11
11
  resourceControllers;
@@ -6,7 +6,7 @@ import * as fs from 'node:fs/promises';
6
6
  import stripAnsi from 'strip-ansi';
7
7
  import { debugLog } from '../utils/debug.js';
8
8
  import { Shell, Utils } from '../utils/index.js';
9
- import { VerbosityLevel } from '../utils/internal-utils.js';
9
+ import { VerbosityLevel } from '../utils/verbosity-level.js';
10
10
  import { SpawnError } from './index.js';
11
11
  import { PromiseQueue } from './promise-queue.js';
12
12
  EventEmitter.defaultMaxListeners = 1000;
@@ -2,7 +2,7 @@ import pty from '@homebridge/node-pty-prebuilt-multiarch';
2
2
  import { EventEmitter } from 'node:events';
3
3
  import stripAnsi from 'strip-ansi';
4
4
  import { Shell, Utils } from '../utils/index.js';
5
- import { VerbosityLevel } from '../utils/internal-utils.js';
5
+ import { VerbosityLevel } from '../utils/verbosity-level.js';
6
6
  import { SpawnError, SpawnStatus } from './index.js';
7
7
  EventEmitter.defaultMaxListeners = 1000;
8
8
  /**
@@ -1,6 +1,6 @@
1
1
  import isObjectsEqual from 'lodash.isequal';
2
2
  import path from 'node:path';
3
- import { addVariablesToPath, areArraysEqual, resolvePathWithVariables, tildify, untildify } from '../utils/internal-utils.js';
3
+ import { addVariablesToPath, areArraysEqual, resolvePathWithVariables, tildify, untildify } from '../utils/functions.js';
4
4
  const ParameterEqualsDefaults = {
5
5
  'boolean': (a, b) => Boolean(a) === Boolean(b),
6
6
  'directory': (a, b) => {
@@ -0,0 +1,12 @@
1
+ import { ResourceConfig, StringIndexedObject } from 'codify-schemas';
2
+ export declare function splitUserConfig<T extends StringIndexedObject>(config: ResourceConfig & T): {
3
+ parameters: T;
4
+ coreParameters: ResourceConfig;
5
+ };
6
+ export declare function setsEqual(set1: Set<unknown>, set2: Set<unknown>): boolean;
7
+ export declare function untildify(pathWithTilde: string): string;
8
+ export declare function tildify(pathWithTilde: string): string;
9
+ export declare function resolvePathWithVariables(pathWithVariables: string): string;
10
+ export declare function addVariablesToPath(pathWithoutVariables: string): string;
11
+ export declare function unhome(pathWithHome: string): string;
12
+ export declare function areArraysEqual(isElementEqual: ((desired: unknown, current: unknown) => boolean) | undefined, desired: unknown, current: unknown): boolean;
@@ -0,0 +1,74 @@
1
+ import os from 'node:os';
2
+ import path from 'node:path';
3
+ export function splitUserConfig(config) {
4
+ const coreParameters = {
5
+ type: config.type,
6
+ ...(config.name ? { name: config.name } : {}),
7
+ ...(config.dependsOn ? { dependsOn: config.dependsOn } : {}),
8
+ };
9
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
10
+ const { type, name, dependsOn, ...parameters } = config;
11
+ return {
12
+ parameters: parameters,
13
+ coreParameters,
14
+ };
15
+ }
16
+ export function setsEqual(set1, set2) {
17
+ return set1.size === set2.size && [...set1].every((v) => set2.has(v));
18
+ }
19
+ const homeDirectory = os.homedir();
20
+ export function untildify(pathWithTilde) {
21
+ return homeDirectory ? pathWithTilde.replace(/^~(?=$|\/|\\)/, homeDirectory) : pathWithTilde;
22
+ }
23
+ export function tildify(pathWithTilde) {
24
+ return homeDirectory ? pathWithTilde.replace(homeDirectory, '~') : pathWithTilde;
25
+ }
26
+ export function resolvePathWithVariables(pathWithVariables) {
27
+ // @ts-expect-error Ignore this for now
28
+ return pathWithVariables.replace(/\$([A-Z_]+[A-Z0-9_]*)|\${([A-Z0-9_]*)}/ig, (_, a, b) => process.env[a || b]);
29
+ }
30
+ export function addVariablesToPath(pathWithoutVariables) {
31
+ let result = pathWithoutVariables;
32
+ for (const [key, value] of Object.entries(process.env)) {
33
+ if (!value || !path.isAbsolute(value) || value === '/' || key === 'HOME' || key === 'PATH' || key === 'SHELL' || key === 'PWD') {
34
+ continue;
35
+ }
36
+ result = result.replaceAll(value, `$${key}`);
37
+ }
38
+ return result;
39
+ }
40
+ export function unhome(pathWithHome) {
41
+ return pathWithHome.includes('$HOME') ? pathWithHome.replaceAll('$HOME', os.homedir()) : pathWithHome;
42
+ }
43
+ export function areArraysEqual(isElementEqual, desired, current) {
44
+ if (!desired || !current) {
45
+ return false;
46
+ }
47
+ if (!Array.isArray(desired) || !Array.isArray(current)) {
48
+ throw new Error(`A non-array value:
49
+
50
+ Desired: ${JSON.stringify(desired, null, 2)}
51
+
52
+ Current: ${JSON.stringify(desired, null, 2)}
53
+
54
+ Was provided even though type array was specified.
55
+ `);
56
+ }
57
+ if (desired.length !== current.length) {
58
+ return false;
59
+ }
60
+ const desiredCopy = [...desired];
61
+ const currentCopy = [...current];
62
+ // Algorithm for to check equality between two un-ordered; un-hashable arrays using
63
+ // an isElementEqual method. Time: O(n^2)
64
+ for (let counter = desiredCopy.length - 1; counter >= 0; counter--) {
65
+ const idx = currentCopy.findIndex((e2) => (isElementEqual
66
+ ?? ((a, b) => a === b))(desiredCopy[counter], e2));
67
+ if (idx === -1) {
68
+ return false;
69
+ }
70
+ desiredCopy.splice(counter, 1);
71
+ currentCopy.splice(idx, 1);
72
+ }
73
+ return currentCopy.length === 0;
74
+ }
@@ -1,4 +1,5 @@
1
1
  import { OS } from 'codify-schemas';
2
+ export declare function isDebug(): boolean;
2
3
  export declare enum Shell {
3
4
  ZSH = "zsh",
4
5
  BASH = "bash",
@@ -1,5 +1,8 @@
1
1
  import os from 'node:os';
2
2
  import path from 'node:path';
3
+ export function isDebug() {
4
+ return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
5
+ }
3
6
  export var Shell;
4
7
  (function (Shell) {
5
8
  Shell["ZSH"] = "zsh";
@@ -1,10 +1,4 @@
1
1
  import { ResourceConfig, StringIndexedObject } from 'codify-schemas';
2
- export declare const VerbosityLevel: {
3
- level: number;
4
- get(): number;
5
- set(level: number): void;
6
- };
7
- export declare function isDebug(): boolean;
8
2
  export declare function splitUserConfig<T extends StringIndexedObject>(config: ResourceConfig & T): {
9
3
  parameters: T;
10
4
  coreParameters: ResourceConfig;
@@ -1,17 +1,5 @@
1
1
  import os from 'node:os';
2
2
  import path from 'node:path';
3
- export const VerbosityLevel = new class {
4
- level = 0;
5
- get() {
6
- return this.level;
7
- }
8
- set(level) {
9
- this.level = level;
10
- }
11
- };
12
- export function isDebug() {
13
- return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
14
- }
15
3
  export function splitUserConfig(config) {
16
4
  const coreParameters = {
17
5
  type: config.type,
@@ -0,0 +1,5 @@
1
+ export declare const VerbosityLevel: {
2
+ level: number;
3
+ get(): number;
4
+ set(level: number): void;
5
+ };
@@ -0,0 +1,9 @@
1
+ export const VerbosityLevel = new class {
2
+ level = 0;
3
+ get() {
4
+ return this.level;
5
+ }
6
+ set(level) {
7
+ this.level = level;
8
+ }
9
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codify-plugin-lib",
3
- "version": "1.0.182-beta2",
3
+ "version": "1.0.182-beta4",
4
4
  "description": "Library plugin library",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -13,6 +13,8 @@ export * from './resource/resource.js'
13
13
  export * from './resource/resource-settings.js'
14
14
  export * from './stateful-parameter/stateful-parameter.js'
15
15
  export * from './utils/index.js'
16
+ export * from './utils/verbosity-level.js'
17
+ export * from './utils/functions.js'
16
18
 
17
19
  export async function runPlugin(plugin: Plugin) {
18
20
  const messageHandler = new MessageHandler(plugin);
@@ -24,8 +24,8 @@ import { getPty } from '../pty/index.js';
24
24
  import { SequentialPty } from '../pty/seqeuntial-pty.js';
25
25
  import { Resource } from '../resource/resource.js';
26
26
  import { ResourceController } from '../resource/resource-controller.js';
27
- import { VerbosityLevel } from '../utils/internal-utils.js';
28
27
  import { ptyLocalStorage } from '../utils/pty-local-storage.js';
28
+ import { VerbosityLevel } from '../utils/verbosity-level.js';
29
29
 
30
30
  export class Plugin {
31
31
  planStorage: Map<string, Plan<any>>;
@@ -7,7 +7,7 @@ import stripAnsi from 'strip-ansi';
7
7
 
8
8
  import { debugLog } from '../utils/debug.js';
9
9
  import { Shell, Utils } from '../utils/index.js';
10
- import { VerbosityLevel } from '../utils/internal-utils.js';
10
+ import { VerbosityLevel } from '../utils/verbosity-level.js';
11
11
  import { IPty, SpawnError, SpawnOptions, SpawnResult } from './index.js';
12
12
  import { PromiseQueue } from './promise-queue.js';
13
13
 
@@ -3,7 +3,7 @@ import { EventEmitter } from 'node:events';
3
3
  import stripAnsi from 'strip-ansi';
4
4
 
5
5
  import { Shell, Utils } from '../utils/index.js';
6
- import { VerbosityLevel } from '../utils/internal-utils.js';
6
+ import { VerbosityLevel } from '../utils/verbosity-level.js';
7
7
  import { IPty, SpawnError, SpawnOptions, SpawnResult, SpawnStatus } from './index.js';
8
8
 
9
9
  EventEmitter.defaultMaxListeners = 1000;
@@ -1,6 +1,6 @@
1
1
  import { describe, expect, it } from 'vitest';
2
2
  import { SequentialPty } from './seqeuntial-pty.js';
3
- import { VerbosityLevel } from '../utils/internal-utils.js';
3
+ import { VerbosityLevel } from '../utils/functions.js';
4
4
 
5
5
  describe('SequentialPty tests', () => {
6
6
  it('Can launch a simple command', async () => {
@@ -7,7 +7,7 @@ import { CreatePlan, DestroyPlan, ModifyPlan } from '../plan/plan-types.js';
7
7
  import { ParameterChange } from '../plan/change-set.js';
8
8
  import { ResourceController } from './resource-controller.js';
9
9
  import { TestConfig, testPlan, TestResource, TestStatefulParameter } from '../utils/test-utils.test.js';
10
- import { tildify, untildify } from '../utils/internal-utils.js';
10
+ import { tildify, untildify } from '../utils/functions.js';
11
11
  import { ArrayStatefulParameter, StatefulParameter } from '../stateful-parameter/stateful-parameter.js';
12
12
  import { Plan } from '../plan/plan.js';
13
13
  import os from 'node:os';
@@ -10,7 +10,7 @@ import {
10
10
  resolvePathWithVariables,
11
11
  tildify,
12
12
  untildify
13
- } from '../utils/internal-utils.js';
13
+ } from '../utils/functions.js';
14
14
  import { RefreshContext } from './resource.js';
15
15
 
16
16
  export interface InputTransformation {
@@ -2,22 +2,6 @@ import { ResourceConfig, StringIndexedObject } from 'codify-schemas';
2
2
  import os from 'node:os';
3
3
  import path from 'node:path';
4
4
 
5
- export const VerbosityLevel = new class {
6
- level = 0;
7
-
8
- get() {
9
- return this.level;
10
- }
11
-
12
- set(level: number) {
13
- this.level = level;
14
- }
15
- }
16
-
17
- export function isDebug(): boolean {
18
- return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
19
- }
20
-
21
5
  export function splitUserConfig<T extends StringIndexedObject>(
22
6
  config: ResourceConfig & T
23
7
  ): { parameters: T; coreParameters: ResourceConfig } {
@@ -2,6 +2,10 @@ import { OS } from 'codify-schemas';
2
2
  import os from 'node:os';
3
3
  import path from 'node:path';
4
4
 
5
+ export function isDebug(): boolean {
6
+ return process.env.DEBUG != null && process.env.DEBUG.includes('codify'); // TODO: replace with debug library
7
+ }
8
+
5
9
  export enum Shell {
6
10
  ZSH = 'zsh',
7
11
  BASH = 'bash',
@@ -1,5 +1,5 @@
1
1
  import { describe, expect, it } from 'vitest';
2
- import { addVariablesToPath, resolvePathWithVariables, splitUserConfig } from './internal-utils.js';
2
+ import { addVariablesToPath, resolvePathWithVariables, splitUserConfig } from './functions.js';
3
3
  import os from 'node:os';
4
4
 
5
5
  describe('Utils tests', () => {
@@ -0,0 +1,11 @@
1
+ export const VerbosityLevel = new class {
2
+ level = 0;
3
+
4
+ get() {
5
+ return this.level;
6
+ }
7
+
8
+ set(level: number) {
9
+ this.level = level;
10
+ }
11
+ }