codify-plugin-lib 1.0.100 → 1.0.101
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 +1 -1
- package/dist/index.js +1 -1
- package/dist/plan/change-set.d.ts +2 -2
- package/dist/plan/plan.js +11 -8
- package/dist/resource/config-parser.d.ts +2 -2
- package/dist/resource/parsed-resource-settings.d.ts +17 -4
- package/dist/resource/parsed-resource-settings.js +25 -3
- package/dist/resource/resource-settings.d.ts +7 -6
- package/dist/resource/resource-settings.js +21 -5
- package/dist/stateful-parameter/stateful-parameter-controller.d.ts +21 -0
- package/dist/stateful-parameter/stateful-parameter-controller.js +82 -0
- package/dist/stateful-parameter/stateful-parameter.d.ts +144 -0
- package/dist/stateful-parameter/stateful-parameter.js +43 -0
- package/dist/utils/utils.d.ts +1 -2
- package/dist/utils/utils.js +3 -2
- package/package.json +4 -2
- package/src/index.ts +1 -1
- package/src/plan/change-set.ts +5 -5
- package/src/plan/plan.test.ts +6 -1
- package/src/plan/plan.ts +21 -16
- package/src/resource/config-parser.ts +3 -3
- package/src/resource/parsed-resource-settings.ts +53 -6
- package/src/resource/resource-settings.test.ts +101 -0
- package/src/resource/resource-settings.ts +34 -9
- package/src/{resource/stateful-parameter.test.ts → stateful-parameter/stateful-parameter-controller.test.ts} +91 -12
- package/src/stateful-parameter/stateful-parameter-controller.ts +112 -0
- package/src/{resource → stateful-parameter}/stateful-parameter.ts +9 -67
- package/src/utils/test-utils.test.ts +1 -1
- package/src/utils/utils.ts +9 -4
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { StringIndexedObject } from 'codify-schemas';
|
|
2
2
|
|
|
3
3
|
import { Plan } from '../plan/plan.js';
|
|
4
|
-
import { ArrayParameterSetting, ParameterSetting } from '
|
|
4
|
+
import { ArrayParameterSetting, ParameterSetting } from '../resource/resource-settings.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* A stateful parameter represents a parameter that holds state on the system (can be created, destroyed) but
|
|
@@ -101,83 +101,25 @@ export abstract class StatefulParameter<T extends StringIndexedObject, V extends
|
|
|
101
101
|
* - Homebrew formulas are arrays
|
|
102
102
|
* - Pyenv python versions are arrays
|
|
103
103
|
*/
|
|
104
|
-
export abstract class ArrayStatefulParameter<T extends StringIndexedObject, V>
|
|
104
|
+
export abstract class ArrayStatefulParameter<T extends StringIndexedObject, V> {
|
|
105
105
|
|
|
106
106
|
/**
|
|
107
|
-
* Parameter
|
|
107
|
+
* Parameter settings for the stateful parameter. Stateful parameters share the same parameter settings as
|
|
108
|
+
* regular parameters except that they cannot be of type 'stateful'. See {@link ParameterSetting} for more
|
|
109
|
+
* information on available settings. Type must be 'array'.
|
|
110
|
+
*
|
|
111
|
+
* @return The parameter settings
|
|
108
112
|
*/
|
|
109
113
|
getSettings(): ArrayParameterSetting {
|
|
110
114
|
return { type: 'array' }
|
|
111
115
|
}
|
|
112
116
|
|
|
113
|
-
/**
|
|
114
|
-
* It is not recommended to override the `add` method. A addItem helper method is available to operate on
|
|
115
|
-
* individual elements of the desired array. See {@link StatefulParameter.add} for more info.
|
|
116
|
-
*
|
|
117
|
-
* @param valuesToAdd The array of values to add
|
|
118
|
-
* @param plan The overall plan
|
|
119
|
-
*
|
|
120
|
-
*/
|
|
121
|
-
async add(valuesToAdd: V[], plan: Plan<T>): Promise<void> {
|
|
122
|
-
for (const value of valuesToAdd) {
|
|
123
|
-
await this.addItem(value, plan);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* It is not recommended to override the `modify` method. `addItem` and `removeItem` will be called accordingly based
|
|
129
|
-
* on the modifications. See {@link StatefulParameter.modify} for more info.
|
|
130
|
-
*
|
|
131
|
-
* @param newValues The new array value
|
|
132
|
-
* @param previousValues The previous array value
|
|
133
|
-
* @param plan The overall plan
|
|
134
|
-
*/
|
|
135
|
-
async modify(newValues: V[], previousValues: V[], plan: Plan<T>): Promise<void> {
|
|
136
|
-
|
|
137
|
-
// TODO: I don't think this works with duplicate elements. Solve at another time
|
|
138
|
-
const valuesToAdd = newValues.filter((n) => !previousValues.some((p) => {
|
|
139
|
-
if (this.getSettings()?.isElementEqual) {
|
|
140
|
-
return this.getSettings().isElementEqual!(n, p);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return n === p;
|
|
144
|
-
}));
|
|
145
|
-
|
|
146
|
-
const valuesToRemove = previousValues.filter((p) => !newValues.some((n) => {
|
|
147
|
-
if (this.getSettings().isElementEqual) {
|
|
148
|
-
return this.getSettings().isElementEqual!(n, p);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
return n === p;
|
|
152
|
-
}));
|
|
153
|
-
|
|
154
|
-
for (const value of valuesToAdd) {
|
|
155
|
-
await this.addItem(value, plan)
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
for (const value of valuesToRemove) {
|
|
159
|
-
await this.removeItem(value, plan)
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* It is not recommended to override the `remove` method. A removeItem helper method is available to operate on
|
|
165
|
-
* individual elements of the desired array. See {@link StatefulParameter.remove} for more info.
|
|
166
|
-
*
|
|
167
|
-
* @param valuesToAdd The array of values to add
|
|
168
|
-
* @param plan The overall plan
|
|
169
|
-
*
|
|
170
|
-
*/
|
|
171
|
-
async remove(valuesToRemove: V[], plan: Plan<T>): Promise<void> {
|
|
172
|
-
for (const value of valuesToRemove) {
|
|
173
|
-
await this.removeItem(value as V, plan);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
117
|
/**
|
|
178
118
|
* See {@link StatefulParameter.refresh} for more info.
|
|
179
119
|
*
|
|
180
120
|
* @param desired The desired value to refresh
|
|
121
|
+
* @param config The desired config
|
|
122
|
+
*
|
|
181
123
|
* @return The current value on the system or null if not found.
|
|
182
124
|
*/
|
|
183
125
|
abstract refresh(desired: V[] | null, config: Partial<T>): Promise<V[] | null>;
|
|
@@ -3,7 +3,7 @@ import { ResourceSettings } from '../resource/resource-settings.js';
|
|
|
3
3
|
import { Plan } from '../plan/plan.js';
|
|
4
4
|
import { Resource } from '../resource/resource.js';
|
|
5
5
|
import { CreatePlan, DestroyPlan } from '../plan/plan-types.js';
|
|
6
|
-
import { ArrayStatefulParameter, StatefulParameter } from '../
|
|
6
|
+
import { ArrayStatefulParameter, StatefulParameter } from '../stateful-parameter/stateful-parameter.js';
|
|
7
7
|
import { ParsedResourceSettings } from '../resource/parsed-resource-settings.js';
|
|
8
8
|
|
|
9
9
|
export function testPlan<T extends StringIndexedObject>(params: {
|
package/src/utils/utils.ts
CHANGED
|
@@ -3,8 +3,6 @@ import { ResourceConfig, StringIndexedObject } from 'codify-schemas';
|
|
|
3
3
|
import { SpawnOptions } from 'node:child_process';
|
|
4
4
|
import os from 'node:os';
|
|
5
5
|
|
|
6
|
-
import { ArrayParameterSetting } from '../resource/resource-settings.js';
|
|
7
|
-
|
|
8
6
|
export enum SpawnStatus {
|
|
9
7
|
SUCCESS = 'success',
|
|
10
8
|
ERROR = 'error',
|
|
@@ -111,7 +109,11 @@ export function untildify(pathWithTilde: string) {
|
|
|
111
109
|
return homeDirectory ? pathWithTilde.replace(/^~(?=$|\/|\\)/, homeDirectory) : pathWithTilde;
|
|
112
110
|
}
|
|
113
111
|
|
|
114
|
-
export function areArraysEqual(
|
|
112
|
+
export function areArraysEqual(
|
|
113
|
+
isElementEqual: ((desired: unknown, current: unknown) => boolean) | undefined,
|
|
114
|
+
desired: unknown,
|
|
115
|
+
current: unknown
|
|
116
|
+
): boolean {
|
|
115
117
|
if (!Array.isArray(desired) || !Array.isArray(current)) {
|
|
116
118
|
throw new Error(`A non-array value:
|
|
117
119
|
|
|
@@ -133,7 +135,10 @@ Was provided even though type array was specified.
|
|
|
133
135
|
// Algorithm for to check equality between two un-ordered; un-hashable arrays using
|
|
134
136
|
// an isElementEqual method. Time: O(n^2)
|
|
135
137
|
for (let counter = desiredCopy.length - 1; counter >= 0; counter--) {
|
|
136
|
-
const idx = currentCopy.findIndex((e2) => (
|
|
138
|
+
const idx = currentCopy.findIndex((e2) => (
|
|
139
|
+
isElementEqual
|
|
140
|
+
?? ((a, b) => a === b))(desiredCopy[counter], e2
|
|
141
|
+
))
|
|
137
142
|
|
|
138
143
|
if (idx === -1) {
|
|
139
144
|
return false;
|