Skip to content

Commit

Permalink
feat(codegen): add script to copy models from local directory (#1675)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Nov 12, 2020
1 parent a3fab9f commit 028a362
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -5,6 +5,7 @@
"description": "AWS SDK for JavaScript from the future",
"main": "index.js",
"scripts": {
"copy-models": "node ./scripts/copy-models",
"generate-clients": "node ./scripts/generate-clients",
"bootstrap": "yarn",
"clean": "yarn clear-build-cache && yarn clear-build-info && lerna clean",
Expand Down
62 changes: 62 additions & 0 deletions scripts/copy-models/index.js
@@ -0,0 +1,62 @@
// @ts-check
const yargs = require("yargs");

const { promises: fsPromises } = require("fs");
const { join } = require("path");
const { spawnProcess } = require("../utils/spawn-process");

const { models } = yargs
.alias("m", "models")
.string("m")
.describe("m", "The path to directory with aws-models.")
.demandOption(["models"])
.help().argv;

(async () => {
const OUTPUT_DIR = join(__dirname, "..", "..", "codegen", "sdk-codegen", "aws-models");

const files = await fsPromises.readdir(models.toString(), {
withFileTypes: true,
});

const smithyModelsFiles = files
.filter((file) => file.isDirectory())
.map((dir) => join(models.toString(), dir.name, `smithy/model.json`));

for (const smithyModelsFile of smithyModelsFiles) {
try {
// Test if file exists.
await fsPromises.stat(smithyModelsFile);
// File exists, copy it.
try {
const fileContent = (await fsPromises.readFile(smithyModelsFile))
.toString()
// Fix for issue SMITHY-95
.replace('"smithy.api#authDefinition": {},', "");

const sdkIdRE = /"sdkId": "([^"]*)"/;
const sdkId = fileContent.match(sdkIdRE)[1].toLowerCase().replace(/\s/g, "-");

const versionRE = /"version": "([^"]*)"/;
const version = fileContent.match(versionRE)[1];

// Copy file.
const outputFile = join(OUTPUT_DIR, `${sdkId}.${version}.json`);
await fsPromises.writeFile(outputFile, fileContent);
} catch (e) {
// Copy failed, log.
console.log(smithyModelsFile);
console.log(e.message);
}
} catch (e) {
// File doesn't exist, ignore.
console.log(e.message);
}
}

// Prettify copied models
await spawnProcess(join(__dirname, "..", "..", "node_modules", ".bin", "prettier"), [
"--write",
`${OUTPUT_DIR}/*.json`,
]);
})();
2 changes: 1 addition & 1 deletion scripts/generate-clients/code-gen.js
Expand Up @@ -2,7 +2,7 @@
const path = require("path");
const { emptyDirSync } = require("fs-extra");
const { copyFileSync, readdirSync, lstatSync } = require("fs");
const { spawnProcess } = require("./spawn-process");
const { spawnProcess } = require("../utils/spawn-process");
const { CODE_GEN_ROOT, CODE_GEN_SDK_ROOT, TEMP_CODE_GEN_INPUT_DIR } = require("./code-gen-dir");
const Glob = require("glob");

Expand Down
7 changes: 5 additions & 2 deletions scripts/generate-clients/code-prettify.js
@@ -1,9 +1,12 @@
// @ts-check
const { spawnProcess } = require("./spawn-process");
const { spawnProcess } = require("../utils/spawn-process");
const path = require("path");

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

module.exports = {
Expand Down
File renamed without changes.

0 comments on commit 028a362

Please sign in to comment.