-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathplugin.ts
More file actions
50 lines (41 loc) · 1.19 KB
/
plugin.ts
File metadata and controls
50 lines (41 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { InputData, JSONSchemaInput, quicktype } from "quicktype-core";
import { writeFileSync } from "fs";
import type { JSONSchema4 } from "json-schema";
interface Serverless {
configSchemaHandler: {
schema: JSONSchema4;
};
}
class ConfigSchemaHandlerTypescriptDefinitionsPlugin {
private schema: JSONSchema4;
constructor(serverless: Serverless) {
this.schema = serverless.configSchemaHandler.schema;
}
commands = {
schema: {
usage: "Get JSON schema definition and generate TS definitions",
lifecycleEvents: ["generate"],
},
};
hooks = {
"schema:generate": this.generateSchema.bind(this),
};
async generateSchema() {
const schemaInput = new JSONSchemaInput(undefined);
await schemaInput.addSource({
name: "AWS",
schema: JSON.stringify(this.schema),
});
const inputData = new InputData();
inputData.addInput(schemaInput);
const { lines: serverlessTs } = await quicktype({
inputData,
lang: "typescript",
rendererOptions: {
"just-types": "true",
},
});
writeFileSync("index.d.ts", serverlessTs.join("\n"));
}
}
module.exports = ConfigSchemaHandlerTypescriptDefinitionsPlugin;