diff --git a/scripts/copy-models/index.js b/scripts/copy-models/index.js index 13e12c732f2c..87299c368793 100644 --- a/scripts/copy-models/index.js +++ b/scripts/copy-models/index.js @@ -1,6 +1,9 @@ // @ts-check const yargs = require("yargs"); +const { promises: fsPromises } = require("fs"); +const { join } = require("path"); + const { models } = yargs .alias("m", "models") .string("m") @@ -9,5 +12,50 @@ const { models } = yargs .help().argv; (async () => { - console.log(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( + __dirname, + "..", + "..", + "codegen", + "sdk-codegen", + "aws-models", + `${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); + } + } })();