Skip to content

Commit 028a362

Browse files
authored
feat(codegen): add script to copy models from local directory (#1675)
1 parent a3fab9f commit 028a362

File tree

5 files changed

+69
-3
lines changed

5 files changed

+69
-3
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"description": "AWS SDK for JavaScript from the future",
66
"main": "index.js",
77
"scripts": {
8+
"copy-models": "node ./scripts/copy-models",
89
"generate-clients": "node ./scripts/generate-clients",
910
"bootstrap": "yarn",
1011
"clean": "yarn clear-build-cache && yarn clear-build-info && lerna clean",

scripts/copy-models/index.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// @ts-check
2+
const yargs = require("yargs");
3+
4+
const { promises: fsPromises } = require("fs");
5+
const { join } = require("path");
6+
const { spawnProcess } = require("../utils/spawn-process");
7+
8+
const { models } = yargs
9+
.alias("m", "models")
10+
.string("m")
11+
.describe("m", "The path to directory with aws-models.")
12+
.demandOption(["models"])
13+
.help().argv;
14+
15+
(async () => {
16+
const OUTPUT_DIR = join(__dirname, "..", "..", "codegen", "sdk-codegen", "aws-models");
17+
18+
const files = await fsPromises.readdir(models.toString(), {
19+
withFileTypes: true,
20+
});
21+
22+
const smithyModelsFiles = files
23+
.filter((file) => file.isDirectory())
24+
.map((dir) => join(models.toString(), dir.name, `smithy/model.json`));
25+
26+
for (const smithyModelsFile of smithyModelsFiles) {
27+
try {
28+
// Test if file exists.
29+
await fsPromises.stat(smithyModelsFile);
30+
// File exists, copy it.
31+
try {
32+
const fileContent = (await fsPromises.readFile(smithyModelsFile))
33+
.toString()
34+
// Fix for issue SMITHY-95
35+
.replace('"smithy.api#authDefinition": {},', "");
36+
37+
const sdkIdRE = /"sdkId": "([^"]*)"/;
38+
const sdkId = fileContent.match(sdkIdRE)[1].toLowerCase().replace(/\s/g, "-");
39+
40+
const versionRE = /"version": "([^"]*)"/;
41+
const version = fileContent.match(versionRE)[1];
42+
43+
// Copy file.
44+
const outputFile = join(OUTPUT_DIR, `${sdkId}.${version}.json`);
45+
await fsPromises.writeFile(outputFile, fileContent);
46+
} catch (e) {
47+
// Copy failed, log.
48+
console.log(smithyModelsFile);
49+
console.log(e.message);
50+
}
51+
} catch (e) {
52+
// File doesn't exist, ignore.
53+
console.log(e.message);
54+
}
55+
}
56+
57+
// Prettify copied models
58+
await spawnProcess(join(__dirname, "..", "..", "node_modules", ".bin", "prettier"), [
59+
"--write",
60+
`${OUTPUT_DIR}/*.json`,
61+
]);
62+
})();

scripts/generate-clients/code-gen.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const path = require("path");
33
const { emptyDirSync } = require("fs-extra");
44
const { copyFileSync, readdirSync, lstatSync } = require("fs");
5-
const { spawnProcess } = require("./spawn-process");
5+
const { spawnProcess } = require("../utils/spawn-process");
66
const { CODE_GEN_ROOT, CODE_GEN_SDK_ROOT, TEMP_CODE_GEN_INPUT_DIR } = require("./code-gen-dir");
77
const Glob = require("glob");
88

scripts/generate-clients/code-prettify.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// @ts-check
2-
const { spawnProcess } = require("./spawn-process");
2+
const { spawnProcess } = require("../utils/spawn-process");
33
const path = require("path");
44

55
const prettifyCode = async (dir) => {
6-
await spawnProcess(path.join(__dirname, "..", "..", "node_modules", ".bin", "prettier"), ["--write", `${dir}/**/*.{ts,js,md,json}`]);
6+
await spawnProcess(path.join(__dirname, "..", "..", "node_modules", ".bin", "prettier"), [
7+
"--write",
8+
`${dir}/**/*.{ts,js,md,json}`,
9+
]);
710
};
811

912
module.exports = {

0 commit comments

Comments
 (0)