|
| 1 | +import { JSONSchema } from '@apidevtools/json-schema-ref-parser'; |
| 2 | +import { Ajv } from 'ajv'; |
| 3 | +import { IpcMessage, IpcMessageSchema, MessageStatus, ResourceSchema } from 'codify-schemas'; |
| 4 | +import mergeJsonSchemas from 'merge-json-schemas'; |
| 5 | +import { ChildProcess, fork } from 'node:child_process'; |
| 6 | + |
| 7 | +import { codifySpawn } from '../src/utils/codify-spawn.js'; |
| 8 | + |
| 9 | +const ajv = new Ajv({ |
| 10 | + strict: true |
| 11 | +}); |
| 12 | +const ipcMessageValidator = ajv.compile(IpcMessageSchema); |
| 13 | + |
| 14 | +function sendMessageAndAwaitResponse(process: ChildProcess, message: IpcMessage): Promise<any> { |
| 15 | + return new Promise((resolve, reject) => { |
| 16 | + process.on('message', (response: IpcMessage) => { |
| 17 | + if (!ipcMessageValidator(response)) { |
| 18 | + throw new Error(`Invalid message from plugin. ${JSON.stringify(message, null, 2)}`); |
| 19 | + } |
| 20 | + |
| 21 | + // Wait for the message response. Other messages such as sudoRequest may be sent before the response returns |
| 22 | + if (response.cmd === message.cmd + '_Response') { |
| 23 | + if (response.status === MessageStatus.SUCCESS) { |
| 24 | + resolve(response.data) |
| 25 | + } else { |
| 26 | + reject(new Error(String(response.data))) |
| 27 | + } |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + // Send message last to ensure listeners are all registered |
| 32 | + process.send(message); |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +await codifySpawn('rm -rf ./dist') |
| 37 | +await codifySpawn('npm run rollup -- -f es'); |
| 38 | + |
| 39 | +const plugin = fork( |
| 40 | + './dist/index.js', |
| 41 | + [], |
| 42 | + { |
| 43 | + // Use default true to test plugins in secure mode (un-able to request sudo directly) |
| 44 | + detached: true, |
| 45 | + env: { ...process.env }, |
| 46 | + execArgv: ['--import', 'tsx/esm', '--inspect=9221'], |
| 47 | + }, |
| 48 | +) |
| 49 | + |
| 50 | +const initializeResult = await sendMessageAndAwaitResponse(plugin, { |
| 51 | + cmd: 'initialize', |
| 52 | + data: {} |
| 53 | +}) |
| 54 | + |
| 55 | +const { resourceDefinitions } = initializeResult; |
| 56 | +const resourceTypes = resourceDefinitions.map((i) => i.type); |
| 57 | + |
| 58 | +const schemasMap = new Map<string, JSONSchema>() |
| 59 | +for (const type of resourceTypes) { |
| 60 | + const resourceInfo = await sendMessageAndAwaitResponse(plugin, { |
| 61 | + cmd: 'getResourceInfo', |
| 62 | + data: { type } |
| 63 | + }) |
| 64 | + |
| 65 | + schemasMap.set(type, resourceInfo.schema); |
| 66 | +} |
| 67 | + |
| 68 | +const mergedSchemas = [...schemasMap.entries()].map(([type, schema]) => { |
| 69 | + // const resolvedSchema = await $RefParser.dereference(schema) |
| 70 | + const resourceSchema = JSON.parse(JSON.stringify(ResourceSchema)); |
| 71 | + |
| 72 | + delete resourceSchema.$id; |
| 73 | + delete resourceSchema.$schema; |
| 74 | + resourceSchema.description = `Resource type: "${type}" | ${resourceSchema.title}`; |
| 75 | + delete resourceSchema.title; |
| 76 | + delete resourceSchema.properties.type; |
| 77 | + |
| 78 | + if (schema) { |
| 79 | + delete schema.$id; |
| 80 | + delete schema.$schema; |
| 81 | + delete schema.title; |
| 82 | + } |
| 83 | + |
| 84 | + return mergeJsonSchemas([schema ?? {}, resourceSchema, { properties: { type: { const: type, type: 'string' } } }]); |
| 85 | + }); |
| 86 | + |
| 87 | + |
| 88 | +await codifySpawn('rm -rf ./dist') |
| 89 | +await codifySpawn('npm run rollup'); // re-run rollup without building for es |
| 90 | + |
| 91 | +console.log('JSON Schemas for all resources') |
| 92 | +console.log(JSON.stringify(mergedSchemas, null, 2)); |
| 93 | + |
| 94 | +process.exit(0) |
| 95 | + |
| 96 | + |
| 97 | + |
0 commit comments