Skip to content

Commit d216954

Browse files
committed
feat: Add cli message sender and the first use case for it (sending a request for showing a <press any key to continue> prompt
1 parent 68c8b22 commit d216954

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codify-plugin-lib",
3-
"version": "1.0.171",
3+
"version": "1.0.173",
44
"description": "Library plugin library",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",
@@ -16,7 +16,7 @@
1616
"dependencies": {
1717
"ajv": "^8.12.0",
1818
"ajv-formats": "^2.1.1",
19-
"codify-schemas": "1.0.74",
19+
"codify-schemas": "1.0.76",
2020
"@npmcli/promise-spawn": "^7.0.1",
2121
"@homebridge/node-pty-prebuilt-multiarch": "^0.12.0-beta.5",
2222
"uuid": "^10.0.0",

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { MessageHandler } from './messages/handlers.js';
22
import { Plugin } from './plugin/plugin.js';
33

44
export * from './errors.js'
5+
export * from './messages/sender.js'
56
export * from './plan/change-set.js'
67
export * from './plan/plan.js'
78
export * from './plan/plan-types.js'

src/messages/sender.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { Ajv } from 'ajv';
2+
import { IpcMessageV2, IpcMessageV2Schema, MessageCmd, PressKeyToContinueRequestData } from 'codify-schemas';
3+
import { nanoid } from 'nanoid';
4+
5+
const ajv = new Ajv({
6+
strict: true,
7+
});
8+
9+
/**
10+
* Send requests to the Codify CLI
11+
*/
12+
class CodifyCliSenderImpl {
13+
private readonly validateIpcMessageV2 = ajv.compile(IpcMessageV2Schema);
14+
15+
async requestPressKeyToContinuePrompt(message?: string): Promise<void> {
16+
await this.sendAndWaitForResponse(<IpcMessageV2>{
17+
cmd: MessageCmd.PRESS_KEY_TO_CONTINUE_REQUEST,
18+
data: <PressKeyToContinueRequestData>{
19+
promptMessage: message,
20+
}
21+
})
22+
}
23+
24+
private async sendAndWaitForResponse(message: IpcMessageV2): Promise<IpcMessageV2> {
25+
return new Promise((resolve) => {
26+
const requestId = nanoid(8);
27+
const listener = (data: IpcMessageV2) => {
28+
if (data.requestId === requestId) {
29+
process.removeListener('message', listener);
30+
31+
if (!this.validateIpcMessageV2(data)) {
32+
throw new Error(`Invalid response for request.
33+
Request:
34+
${JSON.stringify(message, null, 2)}
35+
Response:
36+
${JSON.stringify(data, null, 2)}
37+
Error:
38+
${JSON.stringify(this.validateIpcMessageV2.errors, null, 2)}`);
39+
}
40+
41+
resolve(data);
42+
}
43+
}
44+
45+
process.on('message', listener);
46+
47+
const ipcMessage = {
48+
...message,
49+
requestId,
50+
}
51+
process.send!(ipcMessage)
52+
})
53+
}
54+
}
55+
56+
export const CodifyCliSender = new CodifyCliSenderImpl();

0 commit comments

Comments
 (0)