codify-plugin-lib 1.0.171 → 1.0.173

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 CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Plugin } from './plugin/plugin.js';
2
2
  export * from './errors.js';
3
+ export * from './messages/sender.js';
3
4
  export * from './plan/change-set.js';
4
5
  export * from './plan/plan.js';
5
6
  export * from './plan/plan-types.js';
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { MessageHandler } from './messages/handlers.js';
2
2
  export * from './errors.js';
3
+ export * from './messages/sender.js';
3
4
  export * from './plan/change-set.js';
4
5
  export * from './plan/plan.js';
5
6
  export * from './plan/plan-types.js';
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Send requests to the Codify CLI
3
+ */
4
+ declare class CodifyCliSenderImpl {
5
+ private readonly validateIpcMessageV2;
6
+ requestPressKeyToContinuePrompt(message?: string): Promise<void>;
7
+ private sendAndWaitForResponse;
8
+ }
9
+ export declare const CodifyCliSender: CodifyCliSenderImpl;
10
+ export {};
@@ -0,0 +1,47 @@
1
+ import { Ajv } from 'ajv';
2
+ import { IpcMessageV2Schema, MessageCmd } from 'codify-schemas';
3
+ import { nanoid } from 'nanoid';
4
+ const ajv = new Ajv({
5
+ strict: true,
6
+ });
7
+ /**
8
+ * Send requests to the Codify CLI
9
+ */
10
+ class CodifyCliSenderImpl {
11
+ validateIpcMessageV2 = ajv.compile(IpcMessageV2Schema);
12
+ async requestPressKeyToContinuePrompt(message) {
13
+ await this.sendAndWaitForResponse({
14
+ cmd: MessageCmd.PRESS_KEY_TO_CONTINUE_REQUEST,
15
+ data: {
16
+ promptMessage: message,
17
+ }
18
+ });
19
+ }
20
+ async sendAndWaitForResponse(message) {
21
+ return new Promise((resolve, reject) => {
22
+ const requestId = nanoid(8);
23
+ const listener = (data) => {
24
+ if (data.requestId === requestId) {
25
+ process.removeListener('message', listener);
26
+ if (!this.validateIpcMessageV2(data)) {
27
+ throw new Error(`Invalid response for request.
28
+ Request:
29
+ ${JSON.stringify(message, null, 2)}
30
+ Response:
31
+ ${JSON.stringify(data, null, 2)}
32
+ Error:
33
+ ${JSON.stringify(this.validateIpcMessageV2.errors, null, 2)}`);
34
+ }
35
+ resolve(data);
36
+ }
37
+ };
38
+ process.on('message', listener);
39
+ const ipcMessage = {
40
+ ...message,
41
+ requestId,
42
+ };
43
+ process.send(ipcMessage);
44
+ });
45
+ }
46
+ }
47
+ export const CodifyCliSender = new CodifyCliSenderImpl();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "codify-plugin-lib",
3
- "version": "1.0.171",
3
+ "version": "1.0.173",
4
4
  "description": "Library plugin library",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -16,7 +16,7 @@
16
16
  "dependencies": {
17
17
  "ajv": "^8.12.0",
18
18
  "ajv-formats": "^2.1.1",
19
- "codify-schemas": "1.0.74",
19
+ "codify-schemas": "1.0.76",
20
20
  "@npmcli/promise-spawn": "^7.0.1",
21
21
  "@homebridge/node-pty-prebuilt-multiarch": "^0.12.0-beta.5",
22
22
  "uuid": "^10.0.0",
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ import { MessageHandler } from './messages/handlers.js';
2
2
  import { Plugin } from './plugin/plugin.js';
3
3
 
4
4
  export * from './errors.js'
5
+ export * from './messages/sender.js'
5
6
  export * from './plan/change-set.js'
6
7
  export * from './plan/plan.js'
7
8
  export * from './plan/plan-types.js'
@@ -0,0 +1,60 @@
1
+ import { Ajv } from 'ajv';
2
+ import {
3
+ IpcMessageV2,
4
+ IpcMessageV2Schema,
5
+ MessageCmd, PressKeyToContinueRequestData
6
+ } from 'codify-schemas';
7
+ import { nanoid } from 'nanoid';
8
+
9
+ const ajv = new Ajv({
10
+ strict: true,
11
+ });
12
+
13
+ /**
14
+ * Send requests to the Codify CLI
15
+ */
16
+ class CodifyCliSenderImpl {
17
+ private readonly validateIpcMessageV2 = ajv.compile(IpcMessageV2Schema);
18
+
19
+ async requestPressKeyToContinuePrompt(message?: string): Promise<void> {
20
+ await this.sendAndWaitForResponse(<IpcMessageV2>{
21
+ cmd: MessageCmd.PRESS_KEY_TO_CONTINUE_REQUEST,
22
+ data: <PressKeyToContinueRequestData>{
23
+ promptMessage: message,
24
+ }
25
+ })
26
+ }
27
+
28
+ private async sendAndWaitForResponse(message: IpcMessageV2): Promise<IpcMessageV2> {
29
+ return new Promise((resolve, reject) => {
30
+ const requestId = nanoid(8);
31
+ const listener = (data: IpcMessageV2)=> {
32
+ if (data.requestId === requestId) {
33
+ process.removeListener('message', listener);
34
+
35
+ if (!this.validateIpcMessageV2(data)) {
36
+ throw new Error(`Invalid response for request.
37
+ Request:
38
+ ${JSON.stringify(message, null, 2)}
39
+ Response:
40
+ ${JSON.stringify(data, null, 2)}
41
+ Error:
42
+ ${JSON.stringify(this.validateIpcMessageV2.errors, null, 2)}`);
43
+ }
44
+
45
+ resolve(data);
46
+ }
47
+ }
48
+
49
+ process.on('message', listener);
50
+
51
+ const ipcMessage = {
52
+ ...message,
53
+ requestId,
54
+ }
55
+ process.send!(ipcMessage)
56
+ })
57
+ }
58
+ }
59
+
60
+ export const CodifyCliSender = new CodifyCliSenderImpl();