Skip to content

Commit 5328d59

Browse files
committed
feat(endpoint): add wire protocol filtering option to copy models script
1 parent 5401a39 commit 5328d59

File tree

1 file changed

+58
-16
lines changed

1 file changed

+58
-16
lines changed

scripts/copy-models/index.js

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,50 @@
22
const yargs = require("yargs");
33

44
const { promises: fsPromises } = require("fs");
5-
const { join } = require("path");
6-
const { spawnProcess } = require("../utils/spawn-process");
5+
const { join, resolve } = require("path");
76

8-
const { models } = yargs(process.argv.slice(2))
7+
const { models, protocols } = yargs(process.argv.slice(2))
98
.alias("m", "models")
109
.string("m")
1110
.describe("m", "The path to directory with aws-models.")
1211
.demandOption(["models"])
12+
.alias("p", "protocols")
13+
.string("p")
14+
.describe(
15+
"p",
16+
"List of protocols to copy e.g. awsJson1,restXml, default all, list: \n" +
17+
`awsJson1_1,
18+
restJson1,
19+
awsJson1_0,
20+
awsQuery,
21+
restXml,
22+
ec2Query`
23+
)
1324
.help().argv;
1425

26+
const getSdkId = (model) => {
27+
const { shapes } = model;
28+
const service = Object.values(shapes).find((shape) => shape.type === "service") || {};
29+
const { traits } = service;
30+
for (const [trait, value] of Object.entries(traits)) {
31+
if (trait === "aws.api#service") {
32+
return value.sdkId;
33+
}
34+
}
35+
return "unknown";
36+
};
37+
38+
const getWireProtocol = (model) => {
39+
const { shapes } = model;
40+
const service = Object.values(shapes).find((shape) => shape.type === "service") || {};
41+
for (const trait of Object.keys(service.traits || {})) {
42+
if (trait.startsWith("aws.protocols#")) {
43+
return trait.split("aws.protocols#").pop() || "unknown";
44+
}
45+
}
46+
return "unknown";
47+
};
48+
1549
(async () => {
1650
const OUTPUT_DIR = join(__dirname, "..", "..", "codegen", "sdk-codegen", "aws-models");
1751

@@ -23,34 +57,42 @@ const { models } = yargs(process.argv.slice(2))
2357
.filter((file) => file.isDirectory())
2458
.map((dir) => join(models.toString(), dir.name, `smithy/model.json`));
2559

60+
const observedProtocols = new Set();
61+
2662
for (const smithyModelsFile of smithyModelsFiles) {
2763
try {
2864
// Test if file exists.
2965
await fsPromises.stat(smithyModelsFile);
3066
// File exists, copy it.
67+
const absolutePath = resolve(smithyModelsFile);
68+
3169
try {
32-
const fileContent = (await fsPromises.readFile(smithyModelsFile)).toString();
70+
const model = require(absolutePath);
71+
const sdkId = getSdkId(model).toLowerCase().replace(/\s/g, "-");
72+
const protocol = getWireProtocol(model);
3373

34-
const sdkIdRE = /"sdkId": "([^"]*)"/;
35-
const sdkId = fileContent.match(sdkIdRE)[1].toLowerCase().replace(/\s/g, "-");
74+
observedProtocols.add(protocol);
3675

37-
// Copy file.
38-
const outputFile = join(OUTPUT_DIR, `${sdkId}.json`);
39-
await fsPromises.writeFile(outputFile, fileContent);
76+
if (!protocols || protocols.split(",").includes(protocol)) {
77+
// Copy file.
78+
const outputFile = join(OUTPUT_DIR, `${sdkId}.json`);
79+
await fsPromises.writeFile(outputFile, JSON.stringify(model, null, 2));
80+
console.log("Copied", outputFile);
81+
}
4082
} catch (e) {
4183
// Copy failed, log.
42-
console.log(smithyModelsFile);
84+
console.log("Failed to copy", absolutePath);
4385
console.log(e.message);
4486
}
4587
} catch (e) {
4688
// File doesn't exist, ignore.
47-
console.log(e.message);
89+
console.log("File not found", e.message);
4890
}
4991
}
5092

51-
// Prettify copied models
52-
await spawnProcess(join(__dirname, "..", "..", "node_modules", ".bin", "prettier"), [
53-
"--write",
54-
`${OUTPUT_DIR}/*.json`,
55-
]);
93+
console.log("args:", {
94+
models,
95+
protocols,
96+
observedProtocols,
97+
});
5698
})();

0 commit comments

Comments
 (0)