Skip to content

Commit 228a322

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

File tree

1 file changed

+45
-17
lines changed

1 file changed

+45
-17
lines changed

scripts/copy-models/index.js

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,41 @@
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("p", "List of protocols to copy e.g. awsJson1,restXml, default all")
1315
.help().argv;
1416

17+
const getSdkId = (model) => {
18+
const { shapes } = model;
19+
const service = Object.values(shapes).find((shape) => shape.type === "service") || {};
20+
const { traits } = service;
21+
for (const [trait, value] of Object.entries(traits)) {
22+
if (trait === "aws.api#service") {
23+
return value.sdkId;
24+
}
25+
}
26+
return "unknown";
27+
};
28+
29+
const getWireProtocol = (model) => {
30+
const { shapes } = model;
31+
const service = Object.values(shapes).find((shape) => shape.type === "service") || {};
32+
for (const trait of Object.keys(service.traits || {})) {
33+
if (trait.startsWith("aws.protocols#")) {
34+
return trait.split("aws.protocols#").pop() || "unknown";
35+
}
36+
}
37+
return "unknown";
38+
};
39+
1540
(async () => {
1641
const OUTPUT_DIR = join(__dirname, "..", "..", "codegen", "sdk-codegen", "aws-models");
1742

@@ -28,29 +53,32 @@ const { models } = yargs(process.argv.slice(2))
2853
// Test if file exists.
2954
await fsPromises.stat(smithyModelsFile);
3055
// File exists, copy it.
31-
try {
32-
const fileContent = (await fsPromises.readFile(smithyModelsFile)).toString();
56+
const absolutePath = resolve(smithyModelsFile);
3357

34-
const sdkIdRE = /"sdkId": "([^"]*)"/;
35-
const sdkId = fileContent.match(sdkIdRE)[1].toLowerCase().replace(/\s/g, "-");
58+
try {
59+
const model = require(absolutePath);
60+
const sdkId = getSdkId(model).toLowerCase().replace(/\s/g, "-");
61+
const protocol = getWireProtocol(model);
3662

37-
// Copy file.
38-
const outputFile = join(OUTPUT_DIR, `${sdkId}.json`);
39-
await fsPromises.writeFile(outputFile, fileContent);
63+
if (!protocols || protocols.split(",").includes(protocol)) {
64+
// Copy file.
65+
const outputFile = join(OUTPUT_DIR, `${sdkId}.json`);
66+
await fsPromises.writeFile(outputFile, JSON.stringify(model, null, 2));
67+
console.log("Copied", outputFile);
68+
}
4069
} catch (e) {
4170
// Copy failed, log.
42-
console.log(smithyModelsFile);
71+
console.log("Failed to copy", absolutePath);
4372
console.log(e.message);
4473
}
4574
} catch (e) {
4675
// File doesn't exist, ignore.
47-
console.log(e.message);
76+
console.log("File not found", e.message);
4877
}
4978
}
5079

51-
// Prettify copied models
52-
await spawnProcess(join(__dirname, "..", "..", "node_modules", ".bin", "prettier"), [
53-
"--write",
54-
`${OUTPUT_DIR}/*.json`,
55-
]);
80+
console.log("args:", {
81+
models,
82+
protocols,
83+
});
5684
})();

0 commit comments

Comments
 (0)