@codifycli/plugin-core 1.0.0-beta1

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.
Files changed (152) hide show
  1. package/.eslintignore +2 -0
  2. package/.eslintrc.json +30 -0
  3. package/.github/workflows/release.yaml +19 -0
  4. package/.github/workflows/unit-test-ci.yaml +18 -0
  5. package/.prettierrc.json +1 -0
  6. package/bin/build.js +189 -0
  7. package/dist/bin/build.d.ts +1 -0
  8. package/dist/bin/build.js +80 -0
  9. package/dist/bin/deploy-plugin.d.ts +2 -0
  10. package/dist/bin/deploy-plugin.js +8 -0
  11. package/dist/common/errors.d.ts +8 -0
  12. package/dist/common/errors.js +24 -0
  13. package/dist/entities/change-set.d.ts +24 -0
  14. package/dist/entities/change-set.js +152 -0
  15. package/dist/entities/errors.d.ts +4 -0
  16. package/dist/entities/errors.js +7 -0
  17. package/dist/entities/plan-types.d.ts +25 -0
  18. package/dist/entities/plan-types.js +1 -0
  19. package/dist/entities/plan.d.ts +15 -0
  20. package/dist/entities/plan.js +127 -0
  21. package/dist/entities/plugin.d.ts +16 -0
  22. package/dist/entities/plugin.js +80 -0
  23. package/dist/entities/resource-options.d.ts +31 -0
  24. package/dist/entities/resource-options.js +76 -0
  25. package/dist/entities/resource-types.d.ts +11 -0
  26. package/dist/entities/resource-types.js +1 -0
  27. package/dist/entities/resource.d.ts +42 -0
  28. package/dist/entities/resource.js +303 -0
  29. package/dist/entities/stateful-parameter.d.ts +29 -0
  30. package/dist/entities/stateful-parameter.js +46 -0
  31. package/dist/entities/transform-parameter.d.ts +4 -0
  32. package/dist/entities/transform-parameter.js +2 -0
  33. package/dist/errors.d.ts +4 -0
  34. package/dist/errors.js +7 -0
  35. package/dist/index.d.ts +20 -0
  36. package/dist/index.js +26 -0
  37. package/dist/messages/handlers.d.ts +14 -0
  38. package/dist/messages/handlers.js +134 -0
  39. package/dist/messages/sender.d.ts +11 -0
  40. package/dist/messages/sender.js +57 -0
  41. package/dist/plan/change-set.d.ts +53 -0
  42. package/dist/plan/change-set.js +153 -0
  43. package/dist/plan/plan-types.d.ts +23 -0
  44. package/dist/plan/plan-types.js +1 -0
  45. package/dist/plan/plan.d.ts +66 -0
  46. package/dist/plan/plan.js +328 -0
  47. package/dist/plugin/plugin.d.ts +24 -0
  48. package/dist/plugin/plugin.js +200 -0
  49. package/dist/pty/background-pty.d.ts +21 -0
  50. package/dist/pty/background-pty.js +127 -0
  51. package/dist/pty/index.d.ts +50 -0
  52. package/dist/pty/index.js +20 -0
  53. package/dist/pty/promise-queue.d.ts +5 -0
  54. package/dist/pty/promise-queue.js +26 -0
  55. package/dist/pty/seqeuntial-pty.d.ts +17 -0
  56. package/dist/pty/seqeuntial-pty.js +119 -0
  57. package/dist/pty/vitest.config.d.ts +2 -0
  58. package/dist/pty/vitest.config.js +11 -0
  59. package/dist/resource/config-parser.d.ts +11 -0
  60. package/dist/resource/config-parser.js +21 -0
  61. package/dist/resource/parsed-resource-settings.d.ts +47 -0
  62. package/dist/resource/parsed-resource-settings.js +196 -0
  63. package/dist/resource/resource-controller.d.ts +36 -0
  64. package/dist/resource/resource-controller.js +402 -0
  65. package/dist/resource/resource-settings.d.ts +303 -0
  66. package/dist/resource/resource-settings.js +147 -0
  67. package/dist/resource/resource.d.ts +144 -0
  68. package/dist/resource/resource.js +44 -0
  69. package/dist/resource/stateful-parameter.d.ts +165 -0
  70. package/dist/resource/stateful-parameter.js +94 -0
  71. package/dist/scripts/deploy.d.ts +1 -0
  72. package/dist/scripts/deploy.js +2 -0
  73. package/dist/stateful-parameter/stateful-parameter-controller.d.ts +21 -0
  74. package/dist/stateful-parameter/stateful-parameter-controller.js +81 -0
  75. package/dist/stateful-parameter/stateful-parameter.d.ts +144 -0
  76. package/dist/stateful-parameter/stateful-parameter.js +43 -0
  77. package/dist/test.d.ts +1 -0
  78. package/dist/test.js +5 -0
  79. package/dist/utils/codify-spawn.d.ts +29 -0
  80. package/dist/utils/codify-spawn.js +136 -0
  81. package/dist/utils/debug.d.ts +2 -0
  82. package/dist/utils/debug.js +10 -0
  83. package/dist/utils/file-utils.d.ts +23 -0
  84. package/dist/utils/file-utils.js +186 -0
  85. package/dist/utils/functions.d.ts +12 -0
  86. package/dist/utils/functions.js +74 -0
  87. package/dist/utils/index.d.ts +46 -0
  88. package/dist/utils/index.js +271 -0
  89. package/dist/utils/internal-utils.d.ts +12 -0
  90. package/dist/utils/internal-utils.js +74 -0
  91. package/dist/utils/load-resources.d.ts +1 -0
  92. package/dist/utils/load-resources.js +46 -0
  93. package/dist/utils/package-json-utils.d.ts +12 -0
  94. package/dist/utils/package-json-utils.js +34 -0
  95. package/dist/utils/pty-local-storage.d.ts +2 -0
  96. package/dist/utils/pty-local-storage.js +2 -0
  97. package/dist/utils/spawn-2.d.ts +5 -0
  98. package/dist/utils/spawn-2.js +7 -0
  99. package/dist/utils/spawn.d.ts +29 -0
  100. package/dist/utils/spawn.js +124 -0
  101. package/dist/utils/utils.d.ts +18 -0
  102. package/dist/utils/utils.js +86 -0
  103. package/dist/utils/verbosity-level.d.ts +5 -0
  104. package/dist/utils/verbosity-level.js +9 -0
  105. package/package.json +59 -0
  106. package/rollup.config.js +24 -0
  107. package/src/common/errors.test.ts +43 -0
  108. package/src/common/errors.ts +31 -0
  109. package/src/errors.ts +8 -0
  110. package/src/index.test.ts +6 -0
  111. package/src/index.ts +30 -0
  112. package/src/messages/handlers.test.ts +329 -0
  113. package/src/messages/handlers.ts +181 -0
  114. package/src/messages/sender.ts +69 -0
  115. package/src/plan/change-set.test.ts +280 -0
  116. package/src/plan/change-set.ts +236 -0
  117. package/src/plan/plan-types.ts +27 -0
  118. package/src/plan/plan.test.ts +413 -0
  119. package/src/plan/plan.ts +499 -0
  120. package/src/plugin/plugin.test.ts +533 -0
  121. package/src/plugin/plugin.ts +291 -0
  122. package/src/pty/background-pty.test.ts +69 -0
  123. package/src/pty/background-pty.ts +154 -0
  124. package/src/pty/index.test.ts +129 -0
  125. package/src/pty/index.ts +66 -0
  126. package/src/pty/promise-queue.ts +33 -0
  127. package/src/pty/seqeuntial-pty.ts +151 -0
  128. package/src/pty/sequential-pty.test.ts +194 -0
  129. package/src/resource/config-parser.ts +42 -0
  130. package/src/resource/parsed-resource-settings.test.ts +186 -0
  131. package/src/resource/parsed-resource-settings.ts +307 -0
  132. package/src/resource/resource-controller-stateful-mode.test.ts +253 -0
  133. package/src/resource/resource-controller.test.ts +1081 -0
  134. package/src/resource/resource-controller.ts +563 -0
  135. package/src/resource/resource-settings.test.ts +1213 -0
  136. package/src/resource/resource-settings.ts +545 -0
  137. package/src/resource/resource.ts +157 -0
  138. package/src/stateful-parameter/stateful-parameter-controller.test.ts +244 -0
  139. package/src/stateful-parameter/stateful-parameter-controller.ts +111 -0
  140. package/src/stateful-parameter/stateful-parameter.ts +160 -0
  141. package/src/utils/debug.ts +11 -0
  142. package/src/utils/file-utils.test.ts +7 -0
  143. package/src/utils/file-utils.ts +231 -0
  144. package/src/utils/functions.ts +103 -0
  145. package/src/utils/index.ts +340 -0
  146. package/src/utils/internal-utils.test.ts +52 -0
  147. package/src/utils/pty-local-storage.ts +3 -0
  148. package/src/utils/test-utils.test.ts +96 -0
  149. package/src/utils/verbosity-level.ts +11 -0
  150. package/tsconfig.json +26 -0
  151. package/tsconfig.test.json +9 -0
  152. package/vitest.config.ts +10 -0
@@ -0,0 +1,165 @@
1
+ import { StringIndexedObject } from 'codify-schemas';
2
+ import { Plan } from '../plan/plan.js';
3
+ import { ArrayParameterSetting, ParameterSetting } from './resource-settings.js';
4
+ /**
5
+ * A stateful parameter represents a parameter that holds state on the system (can be created, destroyed) but
6
+ * is still tied to the overall lifecycle of a resource.
7
+ *
8
+ * **Examples include:**
9
+ * 1. Homebrew formulas are stateful parameters. They can be installed and uninstalled but they are still tied to the
10
+ * overall lifecycle of homebrew
11
+ * 2. Nvm installed node versions are stateful parameters. Nvm can install and uninstall different versions of Node but
12
+ * these versions are tied to the lifecycle of nvm. If nvm is uninstalled then so are the Node versions.
13
+ */
14
+ export declare abstract class StatefulParameter<T extends StringIndexedObject, V extends T[keyof T]> {
15
+ /**
16
+ * Parameter settings for the stateful parameter. Stateful parameters share the same parameter settings as
17
+ * regular parameters except that they cannot be of type 'stateful'. See {@link ParameterSetting} for more
18
+ * information on available settings.
19
+ *
20
+ * @return The parameter settings
21
+ */
22
+ getSettings(): ParameterSetting;
23
+ /**
24
+ * Refresh the status of the stateful parameter on the system. This method works similarly to {@link Resource.refresh}.
25
+ * Return the value of the stateful parameter or null if not found.
26
+ *
27
+ * @param desired The desired value of the user.
28
+ * @param config The desired config
29
+ *
30
+ * @return The value of the stateful parameter currently on the system or null if not found
31
+ */
32
+ abstract refresh(desired: V | null, config: Partial<T>): Promise<V | null>;
33
+ /**
34
+ * Create the stateful parameter on the system. This method is similar {@link Resource.create} except that its only
35
+ * applicable to the stateful parameter. For resource `CREATE` operations, this method will be called after the
36
+ * resource is successfully created. The add method is called when a ParameterChange is ADD in a plan. The add
37
+ * method is only called when the stateful parameter does not currently exist.
38
+ *
39
+ * **Example (Homebrew formula):**
40
+ * 1. Add is called with a value of:
41
+ * ```
42
+ * ['jq', 'jenv']
43
+ * ```
44
+ * 2. Add handles the request by calling `brew install --formulae jq jenv`
45
+ *
46
+ * @param valueToAdd The desired value of the stateful parameter.
47
+ * @param plan The overall plan that contains the ADD
48
+ */
49
+ abstract add(valueToAdd: V, plan: Plan<T>): Promise<void>;
50
+ /**
51
+ * Modify the state of a stateful parameter on the system. This method is similar to {@link Resource.modify} except that its only
52
+ * applicable to the stateful parameter.
53
+ *
54
+ * **Example (Git email parameter):**
55
+ * 1. Add is called with a value of:
56
+ * ```
57
+ * newValue: 'email+new@gmail.com', previousValue: 'email+old@gmail.com'
58
+ * ```
59
+ * 2. Modify handles the request by calling `git config --global user.email email+new@gmail.com`
60
+ *
61
+ * @param newValue The desired value of the stateful parameter
62
+ * @param previousValue The current value of the stateful parameter
63
+ * @param plan The overall plan
64
+ */
65
+ abstract modify(newValue: V, previousValue: V, plan: Plan<T>): Promise<void>;
66
+ /**
67
+ * Create the stateful parameter on the system. This method is similar {@link Resource.destroy} except that its only
68
+ * applicable to the stateful parameter. The remove method is only called when the stateful parameter already currently exist.
69
+ * This method corresponds to REMOVE parameter operations in a plan.
70
+ * For resource `DESTORY`, this method is only called if the {@link ResourceSettings.removeStatefulParametersBeforeDestroy}
71
+ * is set to true. This method will be called before the resource is destroyed.
72
+ *
73
+ * **Example (Homebrew formula):**
74
+ * 1. Remove is called with a value of:
75
+ * ```
76
+ * ['jq', 'jenv']
77
+ * ```
78
+ * 2. Remove handles the request by calling `brew uninstall --formulae jq jenv`
79
+ *
80
+ * @param valueToRemove The value to remove from the stateful parameter.
81
+ * @param plan The overall plan that contains the REMOVE
82
+ */
83
+ abstract remove(valueToRemove: V, plan: Plan<T>): Promise<void>;
84
+ }
85
+ /**
86
+ * A specialized version of {@link StatefulParameter } that is used for stateful parameters which are arrays.
87
+ * A stateful parameter represents a parameter that holds state on the system (can be created, destroyed) but
88
+ * is still tied to the overall lifecycle of a resource.
89
+ *
90
+ * **Examples:**
91
+ * - Homebrew formulas are arrays
92
+ * - Pyenv python versions are arrays
93
+ */
94
+ export declare abstract class ArrayStatefulParameter<T extends StringIndexedObject, V> extends StatefulParameter<T, any> {
95
+ /**
96
+ * Parameter level settings. Type must be 'array'.
97
+ */
98
+ getSettings(): ArrayParameterSetting;
99
+ /**
100
+ * It is not recommended to override the `add` method. A addItem helper method is available to operate on
101
+ * individual elements of the desired array. See {@link StatefulParameter.add} for more info.
102
+ *
103
+ * @param valuesToAdd The array of values to add
104
+ * @param plan The overall plan
105
+ *
106
+ */
107
+ add(valuesToAdd: V[], plan: Plan<T>): Promise<void>;
108
+ /**
109
+ * It is not recommended to override the `modify` method. `addItem` and `removeItem` will be called accordingly based
110
+ * on the modifications. See {@link StatefulParameter.modify} for more info.
111
+ *
112
+ * @param newValues The new array value
113
+ * @param previousValues The previous array value
114
+ * @param plan The overall plan
115
+ */
116
+ modify(newValues: V[], previousValues: V[], plan: Plan<T>): Promise<void>;
117
+ /**
118
+ * It is not recommended to override the `remove` method. A removeItem helper method is available to operate on
119
+ * individual elements of the desired array. See {@link StatefulParameter.remove} for more info.
120
+ *
121
+ * @param valuesToAdd The array of values to add
122
+ * @param plan The overall plan
123
+ *
124
+ */
125
+ remove(valuesToRemove: V[], plan: Plan<T>): Promise<void>;
126
+ /**
127
+ * See {@link StatefulParameter.refresh} for more info.
128
+ *
129
+ * @param desired The desired value to refresh
130
+ * @return The current value on the system or null if not found.
131
+ */
132
+ abstract refresh(desired: V[] | null, config: Partial<T>): Promise<V[] | null>;
133
+ /**
134
+ * Helper method that gets called when individual elements of the array need to be added. See {@link StatefulParameter.add}
135
+ * for more information.
136
+ *
137
+ * Example (Homebrew formula):
138
+ * 1. The stateful parameter receives an input of:
139
+ * ```
140
+ * ['jq', 'jenv', 'docker']
141
+ * ```
142
+ * 2. Internally the stateful parameter will iterate the array and call `addItem` for each element
143
+ * 3. Override addItem and install each formula using `brew install --formula jq`
144
+ *
145
+ * @param item The item to add (install)
146
+ * @param plan The overall plan
147
+ */
148
+ abstract addItem(item: V, plan: Plan<T>): Promise<void>;
149
+ /**
150
+ * Helper method that gets called when individual elements of the array need to be removed. See {@link StatefulParameter.remove}
151
+ * for more information.
152
+ *
153
+ * Example (Homebrew formula):
154
+ * 1. The stateful parameter receives an input of:
155
+ * ```
156
+ * ['jq', 'jenv', 'docker']
157
+ * ```
158
+ * 2. Internally the stateful parameter will iterate the array and call `removeItem` for each element
159
+ * 3. Override removeItem and uninstall each formula using `brew uninstall --formula jq`
160
+ *
161
+ * @param item The item to remove (uninstall)
162
+ * @param plan The overall plan
163
+ */
164
+ abstract removeItem(item: V, plan: Plan<T>): Promise<void>;
165
+ }
@@ -0,0 +1,94 @@
1
+ /**
2
+ * A stateful parameter represents a parameter that holds state on the system (can be created, destroyed) but
3
+ * is still tied to the overall lifecycle of a resource.
4
+ *
5
+ * **Examples include:**
6
+ * 1. Homebrew formulas are stateful parameters. They can be installed and uninstalled but they are still tied to the
7
+ * overall lifecycle of homebrew
8
+ * 2. Nvm installed node versions are stateful parameters. Nvm can install and uninstall different versions of Node but
9
+ * these versions are tied to the lifecycle of nvm. If nvm is uninstalled then so are the Node versions.
10
+ */
11
+ export class StatefulParameter {
12
+ /**
13
+ * Parameter settings for the stateful parameter. Stateful parameters share the same parameter settings as
14
+ * regular parameters except that they cannot be of type 'stateful'. See {@link ParameterSetting} for more
15
+ * information on available settings.
16
+ *
17
+ * @return The parameter settings
18
+ */
19
+ getSettings() {
20
+ return {};
21
+ }
22
+ }
23
+ /**
24
+ * A specialized version of {@link StatefulParameter } that is used for stateful parameters which are arrays.
25
+ * A stateful parameter represents a parameter that holds state on the system (can be created, destroyed) but
26
+ * is still tied to the overall lifecycle of a resource.
27
+ *
28
+ * **Examples:**
29
+ * - Homebrew formulas are arrays
30
+ * - Pyenv python versions are arrays
31
+ */
32
+ export class ArrayStatefulParameter extends StatefulParameter {
33
+ /**
34
+ * Parameter level settings. Type must be 'array'.
35
+ */
36
+ getSettings() {
37
+ return { type: 'array' };
38
+ }
39
+ /**
40
+ * It is not recommended to override the `add` method. A addItem helper method is available to operate on
41
+ * individual elements of the desired array. See {@link StatefulParameter.add} for more info.
42
+ *
43
+ * @param valuesToAdd The array of values to add
44
+ * @param plan The overall plan
45
+ *
46
+ */
47
+ async add(valuesToAdd, plan) {
48
+ for (const value of valuesToAdd) {
49
+ await this.addItem(value, plan);
50
+ }
51
+ }
52
+ /**
53
+ * It is not recommended to override the `modify` method. `addItem` and `removeItem` will be called accordingly based
54
+ * on the modifications. See {@link StatefulParameter.modify} for more info.
55
+ *
56
+ * @param newValues The new array value
57
+ * @param previousValues The previous array value
58
+ * @param plan The overall plan
59
+ */
60
+ async modify(newValues, previousValues, plan) {
61
+ // TODO: I don't think this works with duplicate elements. Solve at another time
62
+ const valuesToAdd = newValues.filter((n) => !previousValues.some((p) => {
63
+ if (this.getSettings()?.isElementEqual) {
64
+ return this.getSettings().isElementEqual(n, p);
65
+ }
66
+ return n === p;
67
+ }));
68
+ const valuesToRemove = previousValues.filter((p) => !newValues.some((n) => {
69
+ if (this.getSettings().isElementEqual) {
70
+ return this.getSettings().isElementEqual(n, p);
71
+ }
72
+ return n === p;
73
+ }));
74
+ for (const value of valuesToAdd) {
75
+ await this.addItem(value, plan);
76
+ }
77
+ for (const value of valuesToRemove) {
78
+ await this.removeItem(value, plan);
79
+ }
80
+ }
81
+ /**
82
+ * It is not recommended to override the `remove` method. A removeItem helper method is available to operate on
83
+ * individual elements of the desired array. See {@link StatefulParameter.remove} for more info.
84
+ *
85
+ * @param valuesToAdd The array of values to add
86
+ * @param plan The overall plan
87
+ *
88
+ */
89
+ async remove(valuesToRemove, plan) {
90
+ for (const value of valuesToRemove) {
91
+ await this.removeItem(value, plan);
92
+ }
93
+ }
94
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ console.log('Deploying!!');
2
+ export {};
@@ -0,0 +1,21 @@
1
+ import { StringIndexedObject } from 'codify-schemas';
2
+ import { Plan } from '../plan/plan.js';
3
+ import { ParsedParameterSetting } from '../resource/parsed-resource-settings.js';
4
+ import { ParameterSetting } from '../resource/resource-settings.js';
5
+ import { ArrayStatefulParameter, StatefulParameter } from './stateful-parameter.js';
6
+ /**
7
+ * This class is analogous to what {@link ResourceController} is for {@link Resource}
8
+ * It's a bit messy because this class supports both {@link StatefulParameter} and {@link ArrayStatefulParameter}
9
+ */
10
+ export declare class StatefulParameterController<T extends StringIndexedObject, V extends T[keyof T]> {
11
+ readonly sp: ArrayStatefulParameter<T, V> | StatefulParameter<T, V>;
12
+ readonly settings: ParameterSetting;
13
+ readonly parsedSettings: ParsedParameterSetting;
14
+ private readonly isArrayStatefulParameter;
15
+ constructor(statefulParameter: ArrayStatefulParameter<T, V> | StatefulParameter<T, V>);
16
+ refresh(desired: V | null, config: Partial<T>): Promise<V | null>;
17
+ add(valueToAdd: V, plan: Plan<T>): Promise<void>;
18
+ modify(newValue: V, previousValue: V, plan: Plan<T>): Promise<void>;
19
+ remove(valueToRemove: V, plan: Plan<T>): Promise<void>;
20
+ private calculateIsArrayStatefulParameter;
21
+ }
@@ -0,0 +1,81 @@
1
+ import { resolveElementEqualsFn, resolveEqualsFn, } from '../resource/resource-settings.js';
2
+ /**
3
+ * This class is analogous to what {@link ResourceController} is for {@link Resource}
4
+ * It's a bit messy because this class supports both {@link StatefulParameter} and {@link ArrayStatefulParameter}
5
+ */
6
+ export class StatefulParameterController {
7
+ sp;
8
+ settings;
9
+ parsedSettings;
10
+ isArrayStatefulParameter;
11
+ constructor(statefulParameter) {
12
+ this.sp = statefulParameter;
13
+ this.settings = statefulParameter.getSettings();
14
+ this.isArrayStatefulParameter = this.calculateIsArrayStatefulParameter();
15
+ this.parsedSettings = (this.isArrayStatefulParameter || this.settings.type === 'array') ? {
16
+ ...this.settings,
17
+ isEqual: resolveEqualsFn(this.settings),
18
+ isElementEqual: resolveElementEqualsFn(this.settings)
19
+ } : {
20
+ ...this.settings,
21
+ isEqual: resolveEqualsFn(this.settings),
22
+ };
23
+ }
24
+ async refresh(desired, config) {
25
+ return await this.sp.refresh(desired, config);
26
+ }
27
+ async add(valueToAdd, plan) {
28
+ if (!this.isArrayStatefulParameter) {
29
+ const sp = this.sp;
30
+ return sp.add(valueToAdd, plan);
31
+ }
32
+ const sp = this.sp;
33
+ const valuesToAdd = valueToAdd;
34
+ for (const value of valuesToAdd) {
35
+ await sp.addItem(value, plan);
36
+ }
37
+ }
38
+ async modify(newValue, previousValue, plan) {
39
+ if (!this.isArrayStatefulParameter) {
40
+ const sp = this.sp;
41
+ return sp.modify(newValue, previousValue, plan);
42
+ }
43
+ const sp = this.sp;
44
+ const settings = this.parsedSettings;
45
+ const newValues = newValue;
46
+ const previousValues = previousValue;
47
+ // TODO: I don't think this works with duplicate elements. Solve at another time
48
+ const valuesToAdd = newValues.filter((n) => !previousValues.some((p) => {
49
+ if (settings.isElementEqual) {
50
+ return settings.isElementEqual(n, p);
51
+ }
52
+ return n === p;
53
+ }));
54
+ const valuesToRemove = previousValues.filter((p) => !newValues.some((n) => {
55
+ if (settings.isElementEqual) {
56
+ return settings.isElementEqual(n, p);
57
+ }
58
+ return n === p;
59
+ }));
60
+ for (const value of valuesToAdd) {
61
+ await sp.addItem(value, plan);
62
+ }
63
+ for (const value of valuesToRemove) {
64
+ await sp.removeItem(value, plan);
65
+ }
66
+ }
67
+ async remove(valueToRemove, plan) {
68
+ if (!this.isArrayStatefulParameter) {
69
+ const sp = this.sp;
70
+ return sp.remove(valueToRemove, plan);
71
+ }
72
+ const sp = this.sp;
73
+ const valuesToRemove = valueToRemove;
74
+ for (const value of valuesToRemove) {
75
+ await sp.removeItem(value, plan);
76
+ }
77
+ }
78
+ calculateIsArrayStatefulParameter() {
79
+ return 'addItem' in this.sp && 'removeItem' in this.sp;
80
+ }
81
+ }
@@ -0,0 +1,144 @@
1
+ import { StringIndexedObject } from 'codify-schemas';
2
+ import { Plan } from '../plan/plan.js';
3
+ import { ArrayParameterSetting, ParameterSetting } from '../resource/resource-settings.js';
4
+ /**
5
+ * A stateful parameter represents a parameter that holds state on the system (can be created, destroyed) but
6
+ * is still tied to the overall lifecycle of a resource.
7
+ *
8
+ * **Examples include:**
9
+ * 1. Homebrew formulas are stateful parameters. They can be installed and uninstalled but they are still tied to the
10
+ * overall lifecycle of homebrew
11
+ * 2. Nvm installed node versions are stateful parameters. Nvm can install and uninstall different versions of Node but
12
+ * these versions are tied to the lifecycle of nvm. If nvm is uninstalled then so are the Node versions.
13
+ */
14
+ export declare abstract class StatefulParameter<T extends StringIndexedObject, V extends T[keyof T]> {
15
+ /**
16
+ * Parameter settings for the stateful parameter. Stateful parameters share the same parameter settings as
17
+ * regular parameters except that they cannot be of type 'stateful'. See {@link ParameterSetting} for more
18
+ * information on available settings.
19
+ *
20
+ * @return The parameter settings
21
+ */
22
+ getSettings(): ParameterSetting;
23
+ /**
24
+ * Refresh the status of the stateful parameter on the system. This method works similarly to {@link Resource.refresh}.
25
+ * Return the value of the stateful parameter or null if not found.
26
+ *
27
+ * @param desired The desired value of the user.
28
+ * @param config The desired config
29
+ *
30
+ * @return The value of the stateful parameter currently on the system or null if not found
31
+ */
32
+ abstract refresh(desired: V | null, config: Partial<T>): Promise<V | null>;
33
+ /**
34
+ * Create the stateful parameter on the system. This method is similar {@link Resource.create} except that its only
35
+ * applicable to the stateful parameter. For resource `CREATE` operations, this method will be called after the
36
+ * resource is successfully created. The add method is called when a ParameterChange is ADD in a plan. The add
37
+ * method is only called when the stateful parameter does not currently exist.
38
+ *
39
+ * **Example (Homebrew formula):**
40
+ * 1. Add is called with a value of:
41
+ * ```
42
+ * ['jq', 'jenv']
43
+ * ```
44
+ * 2. Add handles the request by calling `brew install --formulae jq jenv`
45
+ *
46
+ * @param valueToAdd The desired value of the stateful parameter.
47
+ * @param plan The overall plan that contains the ADD
48
+ */
49
+ abstract add(valueToAdd: V, plan: Plan<T>): Promise<void>;
50
+ /**
51
+ * Modify the state of a stateful parameter on the system. This method is similar to {@link Resource.modify} except that its only
52
+ * applicable to the stateful parameter.
53
+ *
54
+ * **Example (Git email parameter):**
55
+ * 1. Add is called with a value of:
56
+ * ```
57
+ * newValue: 'email+new@gmail.com', previousValue: 'email+old@gmail.com'
58
+ * ```
59
+ * 2. Modify handles the request by calling `git config --global user.email email+new@gmail.com`
60
+ *
61
+ * @param newValue The desired value of the stateful parameter
62
+ * @param previousValue The current value of the stateful parameter
63
+ * @param plan The overall plan
64
+ */
65
+ abstract modify(newValue: V, previousValue: V, plan: Plan<T>): Promise<void>;
66
+ /**
67
+ * Create the stateful parameter on the system. This method is similar {@link Resource.destroy} except that its only
68
+ * applicable to the stateful parameter. The remove method is only called when the stateful parameter already currently exist.
69
+ * This method corresponds to REMOVE parameter operations in a plan.
70
+ * For resource `DESTORY`, this method is only called if the {@link ResourceSettings.removeStatefulParametersBeforeDestroy}
71
+ * is set to true. This method will be called before the resource is destroyed.
72
+ *
73
+ * **Example (Homebrew formula):**
74
+ * 1. Remove is called with a value of:
75
+ * ```
76
+ * ['jq', 'jenv']
77
+ * ```
78
+ * 2. Remove handles the request by calling `brew uninstall --formulae jq jenv`
79
+ *
80
+ * @param valueToRemove The value to remove from the stateful parameter.
81
+ * @param plan The overall plan that contains the REMOVE
82
+ */
83
+ abstract remove(valueToRemove: V, plan: Plan<T>): Promise<void>;
84
+ }
85
+ /**
86
+ * A specialized version of {@link StatefulParameter } that is used for stateful parameters which are arrays.
87
+ * A stateful parameter represents a parameter that holds state on the system (can be created, destroyed) but
88
+ * is still tied to the overall lifecycle of a resource.
89
+ *
90
+ * **Examples:**
91
+ * - Homebrew formulas are arrays
92
+ * - Pyenv python versions are arrays
93
+ */
94
+ export declare abstract class ArrayStatefulParameter<T extends StringIndexedObject, V> {
95
+ /**
96
+ * Parameter settings for the stateful parameter. Stateful parameters share the same parameter settings as
97
+ * regular parameters except that they cannot be of type 'stateful'. See {@link ParameterSetting} for more
98
+ * information on available settings. Type must be 'array'.
99
+ *
100
+ * @return The parameter settings
101
+ */
102
+ getSettings(): ArrayParameterSetting;
103
+ /**
104
+ * See {@link StatefulParameter.refresh} for more info.
105
+ *
106
+ * @param desired The desired value to refresh
107
+ * @param config The desired config
108
+ *
109
+ * @return The current value on the system or null if not found.
110
+ */
111
+ abstract refresh(desired: V[] | null, config: Partial<T>): Promise<V[] | null>;
112
+ /**
113
+ * Helper method that gets called when individual elements of the array need to be added. See {@link StatefulParameter.add}
114
+ * for more information.
115
+ *
116
+ * Example (Homebrew formula):
117
+ * 1. The stateful parameter receives an input of:
118
+ * ```
119
+ * ['jq', 'jenv', 'docker']
120
+ * ```
121
+ * 2. Internally the stateful parameter will iterate the array and call `addItem` for each element
122
+ * 3. Override addItem and install each formula using `brew install --formula jq`
123
+ *
124
+ * @param item The item to add (install)
125
+ * @param plan The overall plan
126
+ */
127
+ abstract addItem(item: V, plan: Plan<T>): Promise<void>;
128
+ /**
129
+ * Helper method that gets called when individual elements of the array need to be removed. See {@link StatefulParameter.remove}
130
+ * for more information.
131
+ *
132
+ * Example (Homebrew formula):
133
+ * 1. The stateful parameter receives an input of:
134
+ * ```
135
+ * ['jq', 'jenv', 'docker']
136
+ * ```
137
+ * 2. Internally the stateful parameter will iterate the array and call `removeItem` for each element
138
+ * 3. Override removeItem and uninstall each formula using `brew uninstall --formula jq`
139
+ *
140
+ * @param item The item to remove (uninstall)
141
+ * @param plan The overall plan
142
+ */
143
+ abstract removeItem(item: V, plan: Plan<T>): Promise<void>;
144
+ }
@@ -0,0 +1,43 @@
1
+ /**
2
+ * A stateful parameter represents a parameter that holds state on the system (can be created, destroyed) but
3
+ * is still tied to the overall lifecycle of a resource.
4
+ *
5
+ * **Examples include:**
6
+ * 1. Homebrew formulas are stateful parameters. They can be installed and uninstalled but they are still tied to the
7
+ * overall lifecycle of homebrew
8
+ * 2. Nvm installed node versions are stateful parameters. Nvm can install and uninstall different versions of Node but
9
+ * these versions are tied to the lifecycle of nvm. If nvm is uninstalled then so are the Node versions.
10
+ */
11
+ export class StatefulParameter {
12
+ /**
13
+ * Parameter settings for the stateful parameter. Stateful parameters share the same parameter settings as
14
+ * regular parameters except that they cannot be of type 'stateful'. See {@link ParameterSetting} for more
15
+ * information on available settings.
16
+ *
17
+ * @return The parameter settings
18
+ */
19
+ getSettings() {
20
+ return {};
21
+ }
22
+ }
23
+ /**
24
+ * A specialized version of {@link StatefulParameter } that is used for stateful parameters which are arrays.
25
+ * A stateful parameter represents a parameter that holds state on the system (can be created, destroyed) but
26
+ * is still tied to the overall lifecycle of a resource.
27
+ *
28
+ * **Examples:**
29
+ * - Homebrew formulas are arrays
30
+ * - Pyenv python versions are arrays
31
+ */
32
+ export class ArrayStatefulParameter {
33
+ /**
34
+ * Parameter settings for the stateful parameter. Stateful parameters share the same parameter settings as
35
+ * regular parameters except that they cannot be of type 'stateful'. See {@link ParameterSetting} for more
36
+ * information on available settings. Type must be 'array'.
37
+ *
38
+ * @return The parameter settings
39
+ */
40
+ getSettings() {
41
+ return { type: 'array' };
42
+ }
43
+ }
package/dist/test.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/test.js ADDED
@@ -0,0 +1,5 @@
1
+ import { SequentialPty } from './pty/seqeuntial-pty.js';
2
+ import { VerbosityLevel } from './utils/verbosity-level.js';
3
+ VerbosityLevel.set(1);
4
+ const $ = new SequentialPty();
5
+ await $.spawn('sudo ls', { interactive: true });
@@ -0,0 +1,29 @@
1
+ import { SpawnOptions } from 'node:child_process';
2
+ export declare enum SpawnStatus {
3
+ SUCCESS = "success",
4
+ ERROR = "error"
5
+ }
6
+ export interface SpawnResult {
7
+ status: SpawnStatus;
8
+ data: string;
9
+ }
10
+ type CodifySpawnOptions = {
11
+ cwd?: string;
12
+ throws?: boolean;
13
+ requiresRoot?: boolean;
14
+ requestsTTY?: boolean;
15
+ } & Omit<SpawnOptions, 'detached' | 'shell' | 'stdio'>;
16
+ /**
17
+ *
18
+ * @param cmd Command to run. Ex: `rm -rf`
19
+ * @param opts Standard options for node spawn. Additional argument:
20
+ * throws determines if a shell will throw a JS error. Defaults to true
21
+ *
22
+ * @see promiseSpawn
23
+ * @see spawn
24
+ *
25
+ * @returns SpawnResult { status: SUCCESS | ERROR; data: string }
26
+ */
27
+ export declare function codifySpawn(cmd: string, opts?: CodifySpawnOptions): Promise<SpawnResult>;
28
+ export declare function isDebug(): boolean;
29
+ export {};