Skip to content

Commit e3d0e05

Browse files
committed
feat: add build scripts that performs rollup + generates JSON schema file
1 parent 2910ee9 commit e3d0e05

3 files changed

Lines changed: 212 additions & 2 deletions

File tree

package-lock.json

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

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"test:integration:dev": "tsx scripts/run-tests.ts",
1212
"test:integration": "vitest test/**/*.test.ts",
1313
"test": "vitest",
14-
"rollup": "rollup -c"
14+
"rollup": "rollup -c",
15+
"build": "tsx ./scripts/build.ts"
1516
},
1617
"keywords": [],
1718
"author": "",
@@ -57,7 +58,10 @@
5758
"tsc-watch": "^6.0.4",
5859
"typescript": "^5",
5960
"eslint-config-prettier": "^9.0.0",
60-
"tsx": "^4.7.2"
61+
"tsx": "^4.7.2",
62+
"@fastify/merge-json-schemas": "^0.2.0",
63+
"@apidevtools/json-schema-ref-parser": "^11.7.2",
64+
"merge-json-schemas": "^1.0.0"
6165
},
6266
"engines": {
6367
"node": ">=18.0.0"

scripts/build.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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

Comments
 (0)