Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codegen): add script to copy models from local directory #1675

Merged
merged 4 commits into from Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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