codify-plugin-lib 1.0.10 → 1.0.12

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.
@@ -1,8 +1,9 @@
1
1
  import { Resource } from './resource';
2
2
  import { ApplyRequestData, PlanRequestData, PlanResponseData, ResourceConfig, ValidateRequestData, ValidateResponseData } from 'codify-schemas';
3
+ import { Plan } from './plan';
3
4
  export declare class Plugin {
4
5
  resources: Map<string, Resource<ResourceConfig>>;
5
- planStorage: Map<string, any>;
6
+ planStorage: Map<string, Plan>;
6
7
  constructor(resources: Map<string, Resource<ResourceConfig>>);
7
8
  onInitialize(): Promise<void>;
8
9
  validate(data: ValidateRequestData): Promise<ValidateResponseData>;
@@ -29,6 +29,16 @@ class Plugin {
29
29
  return plan.toResponse();
30
30
  }
31
31
  async apply(data) {
32
+ const { planId } = data;
33
+ const plan = this.planStorage.get(planId);
34
+ if (!plan) {
35
+ throw new Error(`Plan with id: ${planId} was not found`);
36
+ }
37
+ const resource = this.resources.get(plan.getResourceType());
38
+ if (!resource) {
39
+ throw new Error('Malformed plan with resource that cannot be found');
40
+ }
41
+ await resource.apply(plan);
32
42
  }
33
43
  async crossValidateResources(configs) { }
34
44
  }
@@ -7,7 +7,7 @@ export declare abstract class Resource<T extends ResourceConfig> {
7
7
  abstract getTypeId(): string;
8
8
  onInitialize(): Promise<void>;
9
9
  plan(desiredConfig: T): Promise<Plan>;
10
- apply(plan: Plan): Promise<any>;
10
+ apply(plan: Plan): Promise<void>;
11
11
  abstract validate(config: ResourceConfig): Promise<boolean>;
12
12
  abstract getCurrentConfig(): Promise<T | null>;
13
13
  abstract calculateOperation(change: ParameterChange): ResourceOperation.MODIFY | ResourceOperation.RECREATE;
@@ -26,25 +26,16 @@ class Resource {
26
26
  return plan_1.Plan.create(new change_set_1.ChangeSet(resourceOperation, parameterChangeSet), desiredConfig);
27
27
  }
28
28
  async apply(plan) {
29
- if (plan.getResourceType()) {
30
- throw new Error('Internal error: Plan set to wrong resource during apply');
29
+ if (plan.getResourceType() !== this.getTypeId()) {
30
+ throw new Error(`Internal error: Plan set to wrong resource during apply. Expected ${this.getTypeId()} but got: ${plan.getResourceType()}`);
31
31
  }
32
32
  const changeSet = plan.changeSet;
33
33
  switch (plan.changeSet.operation) {
34
- case codify_schemas_1.ResourceOperation.CREATE:
35
- await this.applyCreate(changeSet);
36
- break;
37
- case codify_schemas_1.ResourceOperation.MODIFY:
38
- await this.applyModify(changeSet);
39
- break;
40
- case codify_schemas_1.ResourceOperation.RECREATE:
41
- await this.applyRecreate(changeSet);
42
- break;
43
- case codify_schemas_1.ResourceOperation.DESTROY:
44
- await this.applyDestroy(changeSet);
45
- break;
34
+ case codify_schemas_1.ResourceOperation.CREATE: return this.applyCreate(changeSet);
35
+ case codify_schemas_1.ResourceOperation.MODIFY: return this.applyModify(changeSet);
36
+ case codify_schemas_1.ResourceOperation.RECREATE: return this.applyRecreate(changeSet);
37
+ case codify_schemas_1.ResourceOperation.DESTROY: return this.applyDestroy(changeSet);
46
38
  }
47
- return null;
48
39
  }
49
40
  }
50
41
  exports.Resource = Resource;
@@ -21,7 +21,10 @@ const SupportedRequests = {
21
21
  'apply': {
22
22
  requestValidator: codify_schemas_1.ApplyRequestDataSchema,
23
23
  responseValidator: codify_schemas_1.ApplyResponseDataSchema,
24
- handler: async (plugin, data) => plugin.apply(data)
24
+ handler: async (plugin, data) => {
25
+ await plugin.apply(data);
26
+ return null;
27
+ }
25
28
  }
26
29
  };
27
30
  class MessageHandler {
@@ -12,6 +12,6 @@ type CodifySpawnOptions = {
12
12
  cwd?: string;
13
13
  stdioString?: boolean;
14
14
  } & SpawnOptions;
15
- export declare function codifySpawn(cmd: string, args: string[], opts?: CodifySpawnOptions, extras?: Record<any, any>): Promise<SpawnResult>;
15
+ export declare function codifySpawn(cmd: string, args?: string[], opts?: Omit<CodifySpawnOptions, 'stdio' | 'stdioString' | 'shell'>, extras?: Record<any, any>): Promise<SpawnResult>;
16
16
  export declare function isDebug(): boolean;
17
17
  export {};
@@ -13,7 +13,7 @@ var SpawnStatus;
13
13
  async function codifySpawn(cmd, args, opts, extras) {
14
14
  try {
15
15
  const stdio = isDebug() ? 'inherit' : 'pipe';
16
- const result = await (0, promise_spawn_1.default)(cmd, args, { ...opts, stdio, stdioString: true, shell: true }, extras);
16
+ const result = await (0, promise_spawn_1.default)(cmd, args ?? [], { ...opts, stdio, stdioString: true, shell: true }, extras);
17
17
  const status = (result.code === 0 && !result.stderr)
18
18
  ? SpawnStatus.SUCCESS
19
19
  : SpawnStatus.ERROR;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codify-plugin-lib",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -18,15 +18,15 @@ type CodifySpawnOptions = {
18
18
 
19
19
  export async function codifySpawn(
20
20
  cmd: string,
21
- args: string[],
22
- opts?: CodifySpawnOptions,
21
+ args?: string[],
22
+ opts?: Omit<CodifySpawnOptions, 'stdio' | 'stdioString' | 'shell'>,
23
23
  extras?: Record<any, any>,
24
24
  ): Promise<SpawnResult> {
25
25
  try {
26
26
  const stdio = isDebug() ? 'inherit' : 'pipe';
27
27
  const result = await promiseSpawn(
28
28
  cmd,
29
- args,
29
+ args ?? [],
30
30
  { ...opts, stdio, stdioString: true, shell: true },
31
31
  extras
32
32
  );