@@ -2,15 +2,17 @@ import {
22 InitializeResponseData ,
33 InitializeResponseDataSchema ,
44 PlanResponseData ,
5- PlanResponseDataSchema
5+ PlanResponseDataSchema ,
6+ ValidateResponseData ,
7+ ValidateResponseDataSchema
68} from 'codify-schemas' ;
79
8- import { ConfigBlock } from '../../entities/index.js' ;
910import { ResourceConfig } from '../../entities/resource-config.js' ;
1011import { ajv } from '../../utils/ajv.js' ;
1112import { PluginIpcBridge } from '../ipc-bridge.js' ;
1213
1314const initializeResponseValidator = ajv . compile ( InitializeResponseDataSchema ) ;
15+ const validateResponseValidator = ajv . compile ( ValidateResponseDataSchema ) ;
1416const planResponseValidator = ajv . compile ( PlanResponseDataSchema ) ;
1517
1618export class Plugin {
@@ -26,9 +28,10 @@ export class Plugin {
2628 this . path = path ;
2729 }
2830
29- async initialize ( ipcBridge ?: PluginIpcBridge ) : Promise < InitializeResponseData > {
30- ipcBridge = ipcBridge ?? await PluginIpcBridge . create ( this . path ) ;
31- const initializeResponse = await ipcBridge . sendMessageForResult ( { cmd : 'initialize' } ) ;
31+ async initialize ( ) : Promise < InitializeResponseData > {
32+ this . ipcBridge = await PluginIpcBridge . create ( this . path ) ;
33+
34+ const initializeResponse = await this . ipcBridge . sendMessageForResult ( { cmd : 'initialize' , data : { } } ) ;
3235
3336 if ( ! this . validateInitializeResponse ( initializeResponse ) ) {
3437 throw new Error ( `Invalid initialize response from plugin: ${ this . name } ` ) ;
@@ -41,9 +44,15 @@ export class Plugin {
4144 return initializeResponse ;
4245 }
4346
44- async validate ( configs : ConfigBlock [ ] ) : Promise < string [ ] > {
45- const response = await this . ipcBridge ! . sendMessageForResult ( { cmd : 'validate' , data : { configs } } ) ;
46- return response as string [ ] ;
47+ async validate ( configs : ResourceConfig [ ] ) : Promise < ValidateResponseData > {
48+ const rawConfigs = configs . map ( ( c ) => c . raw ) ;
49+ const response = await this . ipcBridge ! . sendMessageForResult ( { cmd : 'validate' , data : { configs : rawConfigs } } ) ;
50+
51+ if ( ! this . validateValidateResponse ( response ) ) {
52+ throw new Error ( `Invalid validate response from plugin: ${ this . name } ` ) ;
53+ }
54+
55+ return response ;
4756 }
4857
4958 async plan ( resource : ResourceConfig ) : Promise < PlanResponseData > {
@@ -61,15 +70,23 @@ export class Plugin {
6170 }
6271
6372 private validateInitializeResponse ( response : unknown ) : response is InitializeResponseData {
64- if ( initializeResponseValidator ( response ) ) {
73+ if ( ! initializeResponseValidator ( response ) ) {
6574 throw new Error ( `Invalid initialize response from plugin: ${ this . name } . Error: ${ initializeResponseValidator . errors } ` )
6675 }
6776
6877 return true ;
6978 }
7079
80+ private validateValidateResponse ( response : unknown ) : response is ValidateResponseData {
81+ if ( ! validateResponseValidator ( response ) ) {
82+ throw new Error ( `Invalid validate response from plugin: ${ this . name } . Error: ${ initializeResponseValidator . errors } ` )
83+ }
84+
85+ return true ;
86+ }
87+
7188 private validatePlanResponse ( response : unknown ) : response is PlanResponseData {
72- if ( planResponseValidator ( response ) ) {
89+ if ( ! planResponseValidator ( response ) ) {
7390 throw new Error ( `Invalid plan response from plugin: ${ this . name } . Error: ${ initializeResponseValidator . errors } ` )
7491 }
7592
0 commit comments