Skip to content

Commit 1acd79a

Browse files
authored
feat(endpoint): add wire protocol filtering option to copy models script (#4068)
* feat(endpoint): add wire protocol filtering option to copy models script * feat(endpoint): copy models throw on not finding skdId or protocol
1 parent 5401a39 commit 1acd79a

File tree

1 file changed

+61
-16
lines changed

1 file changed

+61
-16
lines changed

scripts/copy-models/index.js

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,53 @@
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+
throw new Error("unable to find SDK ID in model file");
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+
const parts = trait.split("aws.protocols#");
44+
if (parts.length !== 0) {
45+
return parts.pop();
46+
}
47+
}
48+
}
49+
throw new Error("unable to determine wire protocol in model file");
50+
};
51+
1552
(async () => {
1653
const OUTPUT_DIR = join(__dirname, "..", "..", "codegen", "sdk-codegen", "aws-models");
1754

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

63+
const observedProtocols = new Set();
64+
2665
for (const smithyModelsFile of smithyModelsFiles) {
2766
try {
2867
// Test if file exists.
2968
await fsPromises.stat(smithyModelsFile);
3069
// File exists, copy it.
70+
const absolutePath = resolve(smithyModelsFile);
71+
3172
try {
32-
const fileContent = (await fsPromises.readFile(smithyModelsFile)).toString();
73+
const model = require(absolutePath);
74+
const sdkId = getSdkId(model).toLowerCase().replace(/\s/g, "-");
75+
const protocol = getWireProtocol(model);
3376

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

37-
// Copy file.
38-
const outputFile = join(OUTPUT_DIR, `${sdkId}.json`);
39-
await fsPromises.writeFile(outputFile, fileContent);
79+
if (!protocols || protocols.split(",").includes(protocol)) {
80+
// Copy file.
81+
const outputFile = join(OUTPUT_DIR, `${sdkId}.json`);
82+
await fsPromises.writeFile(outputFile, JSON.stringify(model, null, 2));
83+
console.log("Copied", outputFile);
84+
}
4085
} catch (e) {
4186
// Copy failed, log.
42-
console.log(smithyModelsFile);
87+
console.log("Failed to copy", absolutePath);
4388
console.log(e.message);
4489
}
4590
} catch (e) {
4691
// File doesn't exist, ignore.
47-
console.log(e.message);
92+
console.log("File not found", e.message);
4893
}
4994
}
5095

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

0 commit comments

Comments
 (0)