|
1 | 1 | const AWS = require("aws-sdk");
|
2 | 2 | const schemas = new AWS.Schemas();
|
3 | 3 | const inputUtil = require("./input-util");
|
| 4 | +const patternBuilder = require("./pattern-builder"); |
4 | 5 | const inquirer = require("inquirer");
|
5 |
| -const prompt = inquirer.createPromptModule(); |
6 |
| -run(); |
7 |
| -async function run() { |
8 |
| - const registriesResponse = await schemas.listRegistries().promise(); |
9 |
| - const registries = [ |
10 |
| - ...new Set(registriesResponse.Registries.map(p => p.RegistryName)) |
11 |
| - ]; |
12 | 6 |
|
13 |
| - const registry = await prompt({ |
14 |
| - name: "id", |
15 |
| - type: "list", |
16 |
| - message: "Select registry", |
17 |
| - choices: registries |
18 |
| - }); |
19 |
| - const registryName = registry.id; |
20 |
| - const result = await schemas |
21 |
| - .listSchemas({ RegistryName: registryName }) |
| 7 | +async function run() { |
| 8 | + const registry = await inputUtil.getRegistry(schemas); |
| 9 | + const schemaResponse = await schemas |
| 10 | + .listSchemas({ RegistryName: registry.id }) |
22 | 11 | .promise();
|
23 |
| - const sources = [ |
24 |
| - ...new Set(result.Schemas.map(p => p.SchemaName.split("@")[0])) |
25 |
| - ]; |
26 |
| - const source = await prompt({ |
27 |
| - name: "id", |
28 |
| - type: "list", |
29 |
| - message: "Select source", |
30 |
| - choices: sources |
31 |
| - }); |
32 |
| - const detailTypes = result.Schemas.filter(p => |
33 |
| - p.SchemaName.startsWith(`${source.id}@`) |
34 |
| - ).map(p => p.SchemaName.split("@")[1]); |
35 | 12 |
|
36 |
| - const detailType = await prompt({ |
37 |
| - name: "id", |
38 |
| - type: "list", |
39 |
| - message: "Select source", |
40 |
| - choices: detailTypes |
41 |
| - }); |
| 13 | + const sourceName = await inputUtil.getSourceName(schemaResponse); |
| 14 | + const detailTypeName = await inputUtil.getDetailTypeName( |
| 15 | + schemaResponse, |
| 16 | + sourceName |
| 17 | + ); |
| 18 | + const schemaName = `${sourceName}@${detailTypeName}`; |
42 | 19 |
|
43 |
| - const schemaName = `${source.id}@${detailType.id}`; |
44 | 20 | const describeSchemaResponse = await schemas
|
45 |
| - .describeSchema({ RegistryName: registryName, SchemaName: schemaName }) |
| 21 | + .describeSchema({ RegistryName: registry.id, SchemaName: schemaName }) |
46 | 22 | .promise();
|
| 23 | + |
47 | 24 | const schema = JSON.parse(describeSchemaResponse.Content);
|
48 |
| - let pattern = { source: [source.id], "detail-type": [detailType.id] }; |
| 25 | + |
| 26 | + let pattern = patternBuilder.init(sourceName, detailTypeName); |
49 | 27 |
|
50 | 28 | let currentObject = schema.components.schemas.AWSEvent;
|
51 |
| - let pathArray = undefined; |
| 29 | + |
52 | 30 | let objectArray = [];
|
53 | 31 | while (true) {
|
54 |
| - let fieldList = Object.keys(currentObject.properties); |
| 32 | + const { property, chosenProp } = await inputUtil.getProperty( |
| 33 | + currentObject, |
| 34 | + objectArray |
| 35 | + ); |
55 | 36 |
|
56 |
| - const field = await prompt({ |
57 |
| - name: "id", |
58 |
| - type: "list", |
59 |
| - message: `Add ${currentObject} item`, |
60 |
| - choices: fieldList |
61 |
| - }); |
62 |
| - objectArray.push(field.id); |
63 |
| - const chosenProp = currentObject.properties[field.id]; |
64 | 37 | const path = chosenProp.$ref;
|
65 | 38 | if (path) {
|
66 |
| - pathArray = path && path.split("/"); |
67 |
| - pathArray.shift(); |
68 |
| - let current = schema; |
69 |
| - for (var node of pathArray) { |
70 |
| - current = current[node]; |
71 |
| - } |
72 |
| - currentObject = current; |
| 39 | + // If property points at reference, go to reference in schema and continue |
| 40 | + currentObject = findCurrent(path, schema); |
73 | 41 | continue;
|
74 | 42 | }
|
75 | 43 |
|
76 |
| - let answer = undefined; |
77 |
| - switch (chosenProp.type) { |
78 |
| - case "string": |
79 |
| - answer = await inputUtil.string(field.id); |
80 |
| - break; |
81 |
| - default: |
82 |
| - answer = await inputUtil.string(field.id); |
83 |
| - } |
84 |
| - let x = {}; |
85 |
| - let current = answer; |
86 |
| - for (let i = objectArray.length - 2; i >= 0; i--) { |
87 |
| - const newObj = {}; |
88 |
| - newObj[objectArray[i]] = current; |
89 |
| - x[objectArray[i]] = newObj; |
90 |
| - current = x[objectArray[i]]; |
91 |
| - } |
| 44 | + let answer = await inputUtil.getPropertyValue(chosenProp, property); |
| 45 | + |
| 46 | + let current = patternBuilder.getPatternSegment(answer, objectArray); |
| 47 | + |
| 48 | + pattern = patternBuilder.deepMerge(pattern, current); |
| 49 | + outputPattern(); |
92 | 50 |
|
93 |
| - pattern = mergeDeep(pattern, current); |
94 |
| - console.log("Generated pattern:"); |
95 |
| - console.log(JSON.stringify(pattern, null, 2)); |
96 |
| - pathArray = []; |
97 | 51 | objectArray = [];
|
98 |
| - |
99 | 52 | currentObject = schema.components.schemas.AWSEvent;
|
100 | 53 | }
|
101 |
| -} |
102 |
| - |
103 |
| - |
104 | 54 |
|
105 |
| -function isObject(item) { |
106 |
| - return item && typeof item === "object" && !Array.isArray(item); |
| 55 | + function outputPattern() { |
| 56 | + console.log("Generated pattern:"); |
| 57 | + console.log(JSON.stringify(pattern, null, 2)); |
| 58 | + console.log("Press ctrl+c to quit"); |
| 59 | + } |
107 | 60 | }
|
108 | 61 |
|
109 |
| -/** |
110 |
| - * Deep merge two objects. |
111 |
| - * @param target |
112 |
| - * @param ...sources |
113 |
| - */ |
114 |
| -function mergeDeep(target, ...sources) { |
115 |
| - if (!sources.length) return target; |
116 |
| - const source = sources.shift(); |
117 |
| - |
118 |
| - if (isObject(target) && isObject(source)) { |
119 |
| - for (const key in source) { |
120 |
| - if (isObject(source[key])) { |
121 |
| - if (!target[key]) Object.assign(target, { [key]: {} }); |
122 |
| - mergeDeep(target[key], source[key]); |
123 |
| - } else { |
124 |
| - Object.assign(target, { [key]: source[key] }); |
125 |
| - } |
126 |
| - } |
| 62 | +function findCurrent(path, schema) { |
| 63 | + const pathArray = path.split("/"); |
| 64 | + pathArray.shift(); // Remove leading # |
| 65 | + let current = schema; |
| 66 | + for (var node of pathArray) { |
| 67 | + current = current[node]; |
127 | 68 | }
|
128 |
| - |
129 |
| - return mergeDeep(target, ...sources); |
| 69 | + return current; |
130 | 70 | }
|
| 71 | + |
| 72 | +run(); |
0 commit comments