@codifycli/plugin-core 1.2.5-beta.3 → 1.2.5-beta.4

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 (61) hide show
  1. package/.claude/settings.local.json +13 -0
  2. package/bin/build.js +0 -0
  3. package/dist/bin/build.d.ts +1 -0
  4. package/dist/bin/build.js +80 -0
  5. package/dist/bin/deploy-plugin.d.ts +2 -0
  6. package/dist/bin/deploy-plugin.js +8 -0
  7. package/dist/entities/change-set.d.ts +24 -0
  8. package/dist/entities/change-set.js +152 -0
  9. package/dist/entities/errors.d.ts +4 -0
  10. package/dist/entities/errors.js +7 -0
  11. package/dist/entities/plan-types.d.ts +25 -0
  12. package/dist/entities/plan-types.js +1 -0
  13. package/dist/entities/plan.d.ts +15 -0
  14. package/dist/entities/plan.js +127 -0
  15. package/dist/entities/plugin.d.ts +16 -0
  16. package/dist/entities/plugin.js +80 -0
  17. package/dist/entities/resource-options.d.ts +31 -0
  18. package/dist/entities/resource-options.js +76 -0
  19. package/dist/entities/resource-types.d.ts +11 -0
  20. package/dist/entities/resource-types.js +1 -0
  21. package/dist/entities/resource.d.ts +42 -0
  22. package/dist/entities/resource.js +303 -0
  23. package/dist/entities/stateful-parameter.d.ts +29 -0
  24. package/dist/entities/stateful-parameter.js +46 -0
  25. package/dist/entities/transform-parameter.d.ts +4 -0
  26. package/dist/entities/transform-parameter.js +2 -0
  27. package/dist/plan/change-set.d.ts +1 -1
  28. package/dist/plan/plan.js +12 -1
  29. package/dist/pty/background-pty.js +5 -2
  30. package/dist/pty/seqeuntial-pty.js +6 -2
  31. package/dist/pty/vitest.config.d.ts +2 -0
  32. package/dist/pty/vitest.config.js +11 -0
  33. package/dist/resource/stateful-parameter.d.ts +165 -0
  34. package/dist/resource/stateful-parameter.js +94 -0
  35. package/dist/scripts/deploy.d.ts +1 -0
  36. package/dist/scripts/deploy.js +2 -0
  37. package/dist/test.d.ts +1 -0
  38. package/dist/test.js +5 -0
  39. package/dist/utils/codify-spawn.d.ts +29 -0
  40. package/dist/utils/codify-spawn.js +136 -0
  41. package/dist/utils/index.d.ts +11 -0
  42. package/dist/utils/index.js +18 -2
  43. package/dist/utils/internal-utils.d.ts +12 -0
  44. package/dist/utils/internal-utils.js +74 -0
  45. package/dist/utils/load-resources.d.ts +1 -0
  46. package/dist/utils/load-resources.js +46 -0
  47. package/dist/utils/package-json-utils.d.ts +12 -0
  48. package/dist/utils/package-json-utils.js +34 -0
  49. package/dist/utils/spawn-2.d.ts +5 -0
  50. package/dist/utils/spawn-2.js +7 -0
  51. package/dist/utils/spawn.d.ts +29 -0
  52. package/dist/utils/spawn.js +124 -0
  53. package/dist/utils/utils.d.ts +18 -0
  54. package/dist/utils/utils.js +86 -0
  55. package/package.json +1 -1
  56. package/src/plan/change-set.ts +1 -1
  57. package/src/plan/plan.test.ts +33 -0
  58. package/src/plan/plan.ts +13 -1
  59. package/src/pty/background-pty.ts +5 -2
  60. package/src/pty/seqeuntial-pty.ts +6 -2
  61. package/src/utils/index.ts +19 -2
@@ -0,0 +1,13 @@
1
+ {
2
+ "permissions": {
3
+ "allow": [
4
+ "Bash(grep -r \"COMMAND_REQUEST\\\\|PRESS_KEY_TO_CONTINUE_REQUEST\\\\|CODIFY_CREDENTIALS_REQUEST\" /Users/kevinwang/Projects/codify-parent/codify/src --include=\"*.ts\" ! -name \"*.test.ts\")",
5
+ "Read(//Users/kevinwang/Projects/codify-parent/codify-schemas/**)",
6
+ "Bash(npm run *)"
7
+ ]
8
+ },
9
+ "disabledMcpjsonServers": [
10
+ "supabase",
11
+ "codify"
12
+ ]
13
+ }
package/bin/build.js CHANGED
File without changes
@@ -0,0 +1 @@
1
+ export declare function build(): Promise<void>;
@@ -0,0 +1,80 @@
1
+ import { Ajv } from 'ajv';
2
+ import { IpcMessageSchema, MessageStatus, ResourceSchema } from 'codify-schemas';
3
+ // @ts-ignore
4
+ import mergeJsonSchemas from 'merge-json-schemas';
5
+ import { fork } from 'node:child_process';
6
+ import fs from 'node:fs/promises';
7
+ import path from 'node:path';
8
+ import * as url from 'node:url';
9
+ import { codifySpawn } from '../utils/codify-spawn.js';
10
+ const ajv = new Ajv({
11
+ strict: true
12
+ });
13
+ const ipcMessageValidator = ajv.compile(IpcMessageSchema);
14
+ function sendMessageAndAwaitResponse(process, message) {
15
+ return new Promise((resolve, reject) => {
16
+ process.on('message', (response) => {
17
+ if (!ipcMessageValidator(response)) {
18
+ throw new Error(`Invalid message from plugin. ${JSON.stringify(message, null, 2)}`);
19
+ }
20
+ // Wait for the message response. Other messages such as sudoRequest may be sent before the response returns
21
+ if (response.cmd === message.cmd + '_Response') {
22
+ if (response.status === MessageStatus.SUCCESS) {
23
+ resolve(response.data);
24
+ }
25
+ else {
26
+ reject(new Error(String(response.data)));
27
+ }
28
+ }
29
+ });
30
+ // Send message last to ensure listeners are all registered
31
+ process.send(message);
32
+ });
33
+ }
34
+ export async function build() {
35
+ await fs.rm('./dist', { force: true, recursive: true });
36
+ await codifySpawn('npm run rollup -- -f es');
37
+ const plugin = fork('./dist/index.js', [], {
38
+ // Use default true to test plugins in secure mode (un-able to request sudo directly)
39
+ detached: true,
40
+ env: { ...process.env },
41
+ execArgv: ['--import', 'tsx/esm'],
42
+ });
43
+ const initializeResult = await sendMessageAndAwaitResponse(plugin, {
44
+ cmd: 'initialize',
45
+ data: {}
46
+ });
47
+ const { resourceDefinitions } = initializeResult;
48
+ const resourceTypes = resourceDefinitions.map((i) => i.type);
49
+ const schemasMap = new Map();
50
+ for (const type of resourceTypes) {
51
+ const resourceInfo = await sendMessageAndAwaitResponse(plugin, {
52
+ cmd: 'getResourceInfo',
53
+ data: { type }
54
+ });
55
+ schemasMap.set(type, resourceInfo.schema);
56
+ }
57
+ const mergedSchemas = [...schemasMap.entries()].map(([type, schema]) => {
58
+ // const resolvedSchema = await $RefParser.dereference(schema)
59
+ const resourceSchema = JSON.parse(JSON.stringify(ResourceSchema));
60
+ delete resourceSchema.$id;
61
+ delete resourceSchema.$schema;
62
+ delete resourceSchema.title;
63
+ delete resourceSchema.oneOf;
64
+ delete resourceSchema.properties.type;
65
+ if (schema) {
66
+ delete schema.$id;
67
+ delete schema.$schema;
68
+ delete schema.title;
69
+ delete schema.oneOf;
70
+ }
71
+ return mergeJsonSchemas([schema ?? {}, resourceSchema, { properties: { type: { const: type, type: 'string' } } }]);
72
+ });
73
+ await fs.rm('./dist', { force: true, recursive: true });
74
+ await codifySpawn('npm run rollup'); // re-run rollup without building for es
75
+ console.log('Generated JSON Schemas for all resources');
76
+ const distFolder = path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), '..', 'dist');
77
+ const schemaOutputPath = path.resolve(distFolder, 'schemas.json');
78
+ await fs.writeFile(schemaOutputPath, JSON.stringify(mergedSchemas, null, 2));
79
+ console.log('Successfully wrote schema to ./dist/schemas.json');
80
+ }
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+ import path from 'node:path';
3
+ import * as fs from 'node:fs';
4
+ import { build } from './build.js';
5
+ const packageJson = fs.readFileSync(path.join(process.env['npm_config_local_prefix'], 'package.json'), 'utf8');
6
+ const { name: libraryName, version: libraryVersion } = JSON.parse(packageJson);
7
+ console.log(libraryName, libraryVersion);
8
+ await build();
@@ -0,0 +1,24 @@
1
+ import { ParameterOperation, ResourceOperation, StringIndexedObject } from 'codify-schemas';
2
+ import { ParameterOptions } from './plan-types.js';
3
+ export interface ParameterChange<T extends StringIndexedObject> {
4
+ name: keyof T & string;
5
+ operation: ParameterOperation;
6
+ previousValue: any | null;
7
+ newValue: any | null;
8
+ }
9
+ export declare class ChangeSet<T extends StringIndexedObject> {
10
+ operation: ResourceOperation;
11
+ parameterChanges: Array<ParameterChange<T>>;
12
+ constructor(operation: ResourceOperation, parameterChanges: Array<ParameterChange<T>>);
13
+ get desiredParameters(): T;
14
+ get currentParameters(): T;
15
+ static calculateParameterChangeSet<T extends StringIndexedObject>(desired: T | null, current: T | null, options: {
16
+ statefulMode: boolean;
17
+ parameterOptions?: Record<keyof T, ParameterOptions>;
18
+ }): ParameterChange<T>[];
19
+ static combineResourceOperations(prev: ResourceOperation, next: ResourceOperation): ResourceOperation;
20
+ static isSame(desired: unknown, current: unknown, options?: ParameterOptions): boolean;
21
+ private static calculateStatefulModeChangeSet;
22
+ private static calculateStatelessModeChangeSet;
23
+ private static addDefaultValues;
24
+ }
@@ -0,0 +1,152 @@
1
+ import { ParameterOperation, ResourceOperation } from 'codify-schemas';
2
+ export class ChangeSet {
3
+ operation;
4
+ parameterChanges;
5
+ constructor(operation, parameterChanges) {
6
+ this.operation = operation;
7
+ this.parameterChanges = parameterChanges;
8
+ }
9
+ get desiredParameters() {
10
+ return this.parameterChanges
11
+ .reduce((obj, pc) => ({
12
+ ...obj,
13
+ [pc.name]: pc.newValue,
14
+ }), {});
15
+ }
16
+ get currentParameters() {
17
+ return this.parameterChanges
18
+ .reduce((obj, pc) => ({
19
+ ...obj,
20
+ [pc.name]: pc.previousValue,
21
+ }), {});
22
+ }
23
+ static calculateParameterChangeSet(desired, current, options) {
24
+ if (options.statefulMode) {
25
+ return ChangeSet.calculateStatefulModeChangeSet(desired, current, options.parameterOptions);
26
+ }
27
+ else {
28
+ return ChangeSet.calculateStatelessModeChangeSet(desired, current, options.parameterOptions);
29
+ }
30
+ }
31
+ static combineResourceOperations(prev, next) {
32
+ const orderOfOperations = [
33
+ ResourceOperation.NOOP,
34
+ ResourceOperation.MODIFY,
35
+ ResourceOperation.RECREATE,
36
+ ResourceOperation.CREATE,
37
+ ResourceOperation.DESTROY,
38
+ ];
39
+ const indexPrev = orderOfOperations.indexOf(prev);
40
+ const indexNext = orderOfOperations.indexOf(next);
41
+ return orderOfOperations[Math.max(indexPrev, indexNext)];
42
+ }
43
+ static isSame(desired, current, options) {
44
+ if (options?.isEqual) {
45
+ return options.isEqual(desired, current);
46
+ }
47
+ if (Array.isArray(desired) && Array.isArray(current)) {
48
+ const sortedDesired = desired.map((x) => x).sort();
49
+ const sortedCurrent = current.map((x) => x).sort();
50
+ if (sortedDesired.length !== sortedCurrent.length) {
51
+ return false;
52
+ }
53
+ if (options?.isElementEqual) {
54
+ return sortedDesired.every((value, index) => options.isElementEqual(value, sortedCurrent[index]));
55
+ }
56
+ return JSON.stringify(sortedDesired) === JSON.stringify(sortedCurrent);
57
+ }
58
+ return desired === current;
59
+ }
60
+ static calculateStatefulModeChangeSet(desired, current, parameterOptions) {
61
+ const parameterChangeSet = new Array();
62
+ const _desired = Object.fromEntries(Object.entries(desired ?? {}).filter(([, v]) => v != null));
63
+ const _current = Object.fromEntries(Object.entries(current ?? {}).filter(([, v]) => v != null));
64
+ this.addDefaultValues(_desired, parameterOptions);
65
+ for (const [k, v] of Object.entries(_current)) {
66
+ if (_desired[k] == null) {
67
+ parameterChangeSet.push({
68
+ name: k,
69
+ previousValue: v,
70
+ newValue: null,
71
+ operation: ParameterOperation.REMOVE,
72
+ });
73
+ delete _current[k];
74
+ continue;
75
+ }
76
+ if (!ChangeSet.isSame(_desired[k], _current[k], parameterOptions?.[k])) {
77
+ parameterChangeSet.push({
78
+ name: k,
79
+ previousValue: v,
80
+ newValue: _desired[k],
81
+ operation: ParameterOperation.MODIFY,
82
+ });
83
+ delete _current[k];
84
+ delete _desired[k];
85
+ continue;
86
+ }
87
+ parameterChangeSet.push({
88
+ name: k,
89
+ previousValue: v,
90
+ newValue: _desired[k],
91
+ operation: ParameterOperation.NOOP,
92
+ });
93
+ delete _current[k];
94
+ delete _desired[k];
95
+ }
96
+ if (Object.keys(_current).length !== 0) {
97
+ throw Error('Diff algorithm error');
98
+ }
99
+ for (const [k, v] of Object.entries(_desired)) {
100
+ parameterChangeSet.push({
101
+ name: k,
102
+ previousValue: null,
103
+ newValue: v,
104
+ operation: ParameterOperation.ADD,
105
+ });
106
+ }
107
+ return parameterChangeSet;
108
+ }
109
+ static calculateStatelessModeChangeSet(desired, current, parameterOptions) {
110
+ const parameterChangeSet = new Array();
111
+ const _desired = Object.fromEntries(Object.entries(desired ?? {}).filter(([, v]) => v != null));
112
+ const _current = Object.fromEntries(Object.entries(current ?? {}).filter(([, v]) => v != null));
113
+ this.addDefaultValues(_desired, parameterOptions);
114
+ for (const [k, v] of Object.entries(_desired)) {
115
+ if (_current[k] == null) {
116
+ parameterChangeSet.push({
117
+ name: k,
118
+ previousValue: null,
119
+ newValue: v,
120
+ operation: ParameterOperation.ADD,
121
+ });
122
+ continue;
123
+ }
124
+ if (!ChangeSet.isSame(_desired[k], _current[k], parameterOptions?.[k])) {
125
+ parameterChangeSet.push({
126
+ name: k,
127
+ previousValue: _current[k],
128
+ newValue: _desired[k],
129
+ operation: ParameterOperation.MODIFY,
130
+ });
131
+ continue;
132
+ }
133
+ parameterChangeSet.push({
134
+ name: k,
135
+ previousValue: v,
136
+ newValue: v,
137
+ operation: ParameterOperation.NOOP,
138
+ });
139
+ }
140
+ return parameterChangeSet;
141
+ }
142
+ static addDefaultValues(obj, options) {
143
+ Object.entries(options ?? {})
144
+ .filter(([, option]) => option.default !== undefined)
145
+ .map(([name, option]) => [name, option.default])
146
+ .forEach(([key, defaultValue]) => {
147
+ if (obj[key] === undefined) {
148
+ obj[key] = defaultValue;
149
+ }
150
+ });
151
+ }
152
+ }
@@ -0,0 +1,4 @@
1
+ export declare class SudoError extends Error {
2
+ command: string;
3
+ constructor(command: string);
4
+ }
@@ -0,0 +1,7 @@
1
+ export class SudoError extends Error {
2
+ command;
3
+ constructor(command) {
4
+ super();
5
+ this.command = command;
6
+ }
7
+ }
@@ -0,0 +1,25 @@
1
+ import { Plan } from './plan.js';
2
+ import { StringIndexedObject } from 'codify-schemas';
3
+ export interface ParameterOptions {
4
+ modifyOnChange?: boolean;
5
+ isEqual?: (desired: any, current: any) => boolean;
6
+ isElementEqual?: (desired: any, current: any) => boolean;
7
+ default?: unknown;
8
+ isStatefulParameter?: boolean;
9
+ }
10
+ export interface PlanOptions<T> {
11
+ statefulMode: boolean;
12
+ parameterOptions?: Record<keyof T, ParameterOptions>;
13
+ }
14
+ export interface CreatePlan<T extends StringIndexedObject> extends Plan<T> {
15
+ desiredConfig: T;
16
+ currentConfig: null;
17
+ }
18
+ export interface DestroyPlan<T extends StringIndexedObject> extends Plan<T> {
19
+ desiredConfig: null;
20
+ currentConfig: T;
21
+ }
22
+ export interface ModifyPlan<T extends StringIndexedObject> extends Plan<T> {
23
+ desiredConfig: T;
24
+ currentConfig: T;
25
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,15 @@
1
+ import { ChangeSet } from './change-set.js';
2
+ import { ApplyRequestData, PlanResponseData, ResourceConfig, StringIndexedObject } from 'codify-schemas';
3
+ import { PlanOptions } from './plan-types.js';
4
+ export declare class Plan<T extends StringIndexedObject> {
5
+ id: string;
6
+ changeSet: ChangeSet<T>;
7
+ resourceMetadata: ResourceConfig;
8
+ constructor(id: string, changeSet: ChangeSet<T>, resourceMetadata: ResourceConfig);
9
+ static create<T extends StringIndexedObject>(desiredParameters: Partial<T> | null, currentParameters: Partial<T> | null, resourceMetadata: ResourceConfig, options: PlanOptions<T>): Plan<T>;
10
+ getResourceType(): string;
11
+ static fromResponse<T extends ResourceConfig>(data: ApplyRequestData['plan'], defaultValues?: Partial<Record<keyof T, unknown>>): Plan<T>;
12
+ get desiredConfig(): T | null;
13
+ get currentConfig(): T | null;
14
+ toResponse(): PlanResponseData;
15
+ }
@@ -0,0 +1,127 @@
1
+ import { ChangeSet } from './change-set.js';
2
+ import { ParameterOperation, ResourceOperation, } from 'codify-schemas';
3
+ import { randomUUID } from 'crypto';
4
+ export class Plan {
5
+ id;
6
+ changeSet;
7
+ resourceMetadata;
8
+ constructor(id, changeSet, resourceMetadata) {
9
+ this.id = id;
10
+ this.changeSet = changeSet;
11
+ this.resourceMetadata = resourceMetadata;
12
+ }
13
+ static create(desiredParameters, currentParameters, resourceMetadata, options) {
14
+ const parameterOptions = options.parameterOptions ?? {};
15
+ const statefulParameterNames = new Set([...Object.entries(parameterOptions)]
16
+ .filter(([k, v]) => v.isStatefulParameter)
17
+ .map(([k, v]) => k));
18
+ const parameterChangeSet = ChangeSet.calculateParameterChangeSet(desiredParameters, currentParameters, { statefulMode: options.statefulMode, parameterOptions });
19
+ let resourceOperation;
20
+ if (!currentParameters && desiredParameters) {
21
+ resourceOperation = ResourceOperation.CREATE;
22
+ }
23
+ else if (currentParameters && !desiredParameters) {
24
+ resourceOperation = ResourceOperation.DESTROY;
25
+ }
26
+ else {
27
+ resourceOperation = parameterChangeSet
28
+ .filter((change) => change.operation !== ParameterOperation.NOOP)
29
+ .reduce((operation, curr) => {
30
+ let newOperation;
31
+ if (statefulParameterNames.has(curr.name)) {
32
+ newOperation = ResourceOperation.MODIFY;
33
+ }
34
+ else if (parameterOptions[curr.name]?.modifyOnChange) {
35
+ newOperation = parameterOptions[curr.name].modifyOnChange ? ResourceOperation.MODIFY : ResourceOperation.RECREATE;
36
+ }
37
+ else {
38
+ newOperation = ResourceOperation.RECREATE;
39
+ }
40
+ return ChangeSet.combineResourceOperations(operation, newOperation);
41
+ }, ResourceOperation.NOOP);
42
+ }
43
+ return new Plan(randomUUID(), new ChangeSet(resourceOperation, parameterChangeSet), resourceMetadata);
44
+ }
45
+ getResourceType() {
46
+ return this.resourceMetadata.type;
47
+ }
48
+ static fromResponse(data, defaultValues) {
49
+ if (!data) {
50
+ throw new Error('Data is empty');
51
+ }
52
+ addDefaultValues();
53
+ return new Plan(randomUUID(), new ChangeSet(data.operation, data.parameters), {
54
+ type: data.resourceType,
55
+ name: data.resourceName,
56
+ });
57
+ function addDefaultValues() {
58
+ Object.entries(defaultValues ?? {})
59
+ .forEach(([key, defaultValue]) => {
60
+ const configValueExists = data
61
+ .parameters
62
+ .some((p) => p.name === key);
63
+ if (configValueExists) {
64
+ return;
65
+ }
66
+ switch (data.operation) {
67
+ case ResourceOperation.CREATE: {
68
+ data.parameters.push({
69
+ name: key,
70
+ operation: ParameterOperation.ADD,
71
+ previousValue: null,
72
+ newValue: defaultValue,
73
+ });
74
+ break;
75
+ }
76
+ case ResourceOperation.DESTROY: {
77
+ data.parameters.push({
78
+ name: key,
79
+ operation: ParameterOperation.REMOVE,
80
+ previousValue: defaultValue,
81
+ newValue: null,
82
+ });
83
+ break;
84
+ }
85
+ case ResourceOperation.MODIFY:
86
+ case ResourceOperation.RECREATE:
87
+ case ResourceOperation.NOOP: {
88
+ data.parameters.push({
89
+ name: key,
90
+ operation: ParameterOperation.NOOP,
91
+ previousValue: defaultValue,
92
+ newValue: defaultValue,
93
+ });
94
+ break;
95
+ }
96
+ }
97
+ });
98
+ }
99
+ }
100
+ get desiredConfig() {
101
+ if (this.changeSet.operation === ResourceOperation.DESTROY) {
102
+ return null;
103
+ }
104
+ return {
105
+ ...this.resourceMetadata,
106
+ ...this.changeSet.desiredParameters,
107
+ };
108
+ }
109
+ get currentConfig() {
110
+ if (this.changeSet.operation === ResourceOperation.CREATE) {
111
+ return null;
112
+ }
113
+ return {
114
+ ...this.resourceMetadata,
115
+ ...this.changeSet.currentParameters,
116
+ };
117
+ }
118
+ toResponse() {
119
+ return {
120
+ planId: this.id,
121
+ operation: this.changeSet.operation,
122
+ resourceName: this.resourceMetadata.name,
123
+ resourceType: this.resourceMetadata.type,
124
+ parameters: this.changeSet.parameterChanges,
125
+ };
126
+ }
127
+ }
@@ -0,0 +1,16 @@
1
+ import { ApplyRequestData, InitializeResponseData, PlanRequestData, PlanResponseData, ResourceConfig, ValidateRequestData, ValidateResponseData } from 'codify-schemas';
2
+ import { Plan } from './plan.js';
3
+ import { Resource } from './resource.js';
4
+ export declare class Plugin {
5
+ name: string;
6
+ resources: Map<string, Resource<ResourceConfig>>;
7
+ planStorage: Map<string, Plan<any>>;
8
+ static create(name: string, resources: Resource<any>[]): Plugin;
9
+ constructor(name: string, resources: Map<string, Resource<ResourceConfig>>);
10
+ initialize(): Promise<InitializeResponseData>;
11
+ validate(data: ValidateRequestData): Promise<ValidateResponseData>;
12
+ plan(data: PlanRequestData): Promise<PlanResponseData>;
13
+ apply(data: ApplyRequestData): Promise<void>;
14
+ private resolvePlan;
15
+ protected crossValidateResources(configs: ResourceConfig[]): Promise<void>;
16
+ }
@@ -0,0 +1,80 @@
1
+ import { splitUserConfig } from '../utils/utils.js';
2
+ import { Plan } from './plan.js';
3
+ export class Plugin {
4
+ name;
5
+ resources;
6
+ planStorage;
7
+ static create(name, resources) {
8
+ const resourceMap = new Map(resources.map((r) => [r.typeId, r]));
9
+ return new Plugin(name, resourceMap);
10
+ }
11
+ constructor(name, resources) {
12
+ this.name = name;
13
+ this.resources = resources;
14
+ this.planStorage = new Map();
15
+ }
16
+ async initialize() {
17
+ for (const resource of this.resources.values()) {
18
+ await resource.onInitialize();
19
+ }
20
+ return {
21
+ resourceDefinitions: [...this.resources.values()]
22
+ .map((r) => ({
23
+ dependencies: r.dependencies,
24
+ type: r.typeId,
25
+ }))
26
+ };
27
+ }
28
+ async validate(data) {
29
+ const validationResults = [];
30
+ for (const config of data.configs) {
31
+ if (!this.resources.has(config.type)) {
32
+ throw new Error(`Resource type not found: ${config.type}`);
33
+ }
34
+ const { parameters, resourceMetadata } = splitUserConfig(config);
35
+ const validation = await this.resources
36
+ .get(config.type)
37
+ .validate(parameters, resourceMetadata);
38
+ validationResults.push(validation);
39
+ }
40
+ await this.crossValidateResources(data.configs);
41
+ return {
42
+ resourceValidations: validationResults
43
+ };
44
+ }
45
+ async plan(data) {
46
+ const type = data.desired?.type ?? data.state?.type;
47
+ if (!type || !this.resources.has(type)) {
48
+ throw new Error(`Resource type not found: ${type}`);
49
+ }
50
+ const plan = await this.resources.get(type).plan(data.desired ?? null, data.state ?? null, data.isStateful);
51
+ this.planStorage.set(plan.id, plan);
52
+ return plan.toResponse();
53
+ }
54
+ async apply(data) {
55
+ if (!data.planId && !data.plan) {
56
+ throw new Error('For applies either plan or planId must be supplied');
57
+ }
58
+ const plan = this.resolvePlan(data);
59
+ const resource = this.resources.get(plan.getResourceType());
60
+ if (!resource) {
61
+ throw new Error('Malformed plan with resource that cannot be found');
62
+ }
63
+ await resource.apply(plan);
64
+ }
65
+ resolvePlan(data) {
66
+ const { plan: planRequest, planId } = data;
67
+ if (planId) {
68
+ if (!this.planStorage.has(planId)) {
69
+ throw new Error(`Plan with id: ${planId} was not found`);
70
+ }
71
+ return this.planStorage.get(planId);
72
+ }
73
+ if (!planRequest?.resourceType || !this.resources.has(planRequest.resourceType)) {
74
+ throw new Error('Malformed plan. Resource type must be supplied or resource type was not found');
75
+ }
76
+ const resource = this.resources.get(planRequest.resourceType);
77
+ return Plan.fromResponse(planRequest, resource.defaultValues);
78
+ }
79
+ async crossValidateResources(configs) { }
80
+ }
@@ -0,0 +1,31 @@
1
+ import { StringIndexedObject } from 'codify-schemas';
2
+ import { ParameterOptions } from './plan-types.js';
3
+ import { ResourceParameterOptions } from './resource-types.js';
4
+ import { StatefulParameter } from './stateful-parameter.js';
5
+ import { TransformParameter } from './transform-parameter.js';
6
+ export interface ResourceOptions<T extends StringIndexedObject> {
7
+ callStatefulParameterRemoveOnDestroy?: boolean;
8
+ dependencies?: string[];
9
+ parameterOptions?: Partial<Record<keyof T, ResourceParameterOptions | ResourceStatefulParameterOptions<T> | ResourceTransformParameterOptions<T>>>;
10
+ schema?: unknown;
11
+ type: string;
12
+ }
13
+ export interface ResourceStatefulParameterOptions<T extends StringIndexedObject> {
14
+ order?: number;
15
+ statefulParameter: StatefulParameter<T, T[keyof T]>;
16
+ }
17
+ export interface ResourceTransformParameterOptions<T extends StringIndexedObject> {
18
+ order?: number;
19
+ transformParameter: TransformParameter<T>;
20
+ }
21
+ export declare class ResourceOptionsParser<T extends StringIndexedObject> {
22
+ private options;
23
+ constructor(options: ResourceOptions<T>);
24
+ get statefulParameters(): Map<keyof T, StatefulParameter<T, T[keyof T]>>;
25
+ get transformParameters(): Map<keyof T, TransformParameter<T>>;
26
+ get resourceParameters(): Map<keyof T, ResourceParameterOptions>;
27
+ get changeSetParameterOptions(): Record<keyof T, ParameterOptions>;
28
+ get defaultValues(): Partial<Record<keyof T, unknown>>;
29
+ get statefulParameterOrder(): Map<keyof T, number>;
30
+ get transformParameterOrder(): Map<keyof T, number>;
31
+ }