Skip to content

Commit

Permalink
chore: generate package static components from templates
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP committed Dec 13, 2019
1 parent e08b134 commit 20b41ae
Show file tree
Hide file tree
Showing 11 changed files with 506 additions and 43 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"cucumber": "0.5.x",
"fs-extra": "^8.1.0",
"generate-changelog": "^1.7.1",
"handlebars": "^4.5.3",
"husky": "^3.0.0",
"jest": "^24.7.1",
"jmespath": "^0.15.0",
Expand Down
6 changes: 2 additions & 4 deletions scripts/generate-clients/code-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ const CODE_GEN_ROOT = path.normalize(
async function generateClients(models) {
console.info("models directory: ", models);
if (models === CODE_GEN_INPUT_DIR) {
// console.log("skipping copying models to codegen directory");
// This script will clean the CODE_GEN_INPUT_DIR in execution.
// throw to avoid input model being removed
throw new Error(
`models directory cannot be the same as ${CODE_GEN_INPUT_DIR}`
);
} else {
console.log(`clearing code gen input folder...`);
emptyDirSync(CODE_GEN_INPUT_DIR);
console.log(`copying models from ${models} to ${CODE_GEN_INPUT_DIR}...`);
// copySync(models, CODE_GEN_INPUT_DIR, {
// overwrite: true
// });
for (const modelFileName of readdirSync(models)) {
const modelPath = path.join(models, modelFileName);
if (!lstatSync(modelPath).isFile()) continue;
Expand Down
107 changes: 70 additions & 37 deletions scripts/generate-clients/copy-to-clients.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require("path");
const { join, normalize } = require("path");
const { copySync, ensureDirSync } = require("fs-extra");
const handlebars = require("handlebars");
const {
readdirSync,
lstatSync,
Expand All @@ -8,8 +9,8 @@ const {
writeFileSync
} = require("fs");

const CODE_GEN_OUTPUT_DIR = path.normalize(
path.join(
const CODE_GEN_OUTPUT_DIR = normalize(
join(
__dirname,
"..",
"..",
Expand All @@ -21,69 +22,101 @@ const CODE_GEN_OUTPUT_DIR = path.normalize(
)
);

const unOverridables = [
"package.json",
"tsconfig.es.json",
"tsconfig.json",
"tsconfig.test.json"
];
/**
* templates are a black list of files we don't want codegen artifact
* to override in the clients folder
*/
const templates = readdirSync(join(__dirname, "templates"))
.filter(name => /.sample$/.test(name))
.reduce((accumulator, curr) => {
const templatePath = join(__dirname, "templates", curr);
const template = readFileSync(templatePath).toString();
accumulator[curr.replace(/.sample$/, "")] = template;
return accumulator;
}, {});

async function copyToClients(clientsDir) {
for (const modelName of readdirSync(CODE_GEN_OUTPUT_DIR)) {
if (modelName === "source") continue;
const artifactPath = path.join(
const artifactPath = join(
CODE_GEN_OUTPUT_DIR,
modelName,
"typescript-codegen"
);
const packageManifestPath = path.join(artifactPath, "package.json");
const packageManifestPath = join(artifactPath, "package.json");
if (!existsSync(packageManifestPath)) {
console.error(`${modelName} generates empty client, skip.`);
continue;
}

const packageManifest = JSON.parse(
readFileSync(packageManifestPath).toString()
);
const packageName = packageManifest.name.replace("@aws-sdk/", "");
const packageName = packageManifest.name;
console.log(`copying ${packageName} from ${artifactPath} to ${clientsDir}`);
const destPath = path.join(clientsDir, packageName);
for (const packageSub of readdirSync(artifactPath)) {
const packageSubPath = path.join(artifactPath, packageSub);
const destSubPath = path.join(destPath, packageSub);
if (unOverridables.indexOf(packageSub) >= 0) {
if (!existsSync(destSubPath))
copySync(packageSubPath, destSubPath, { overwrite: true });
else if (packageSub === "package.json") {
const destPath = join(clientsDir, packageName.replace("@aws-sdk/", ""));

//Data used to generate files from template
const templateData = {
year: new Date().getFullYear(),
packageName
};

for (const packageSub of [
...readdirSync(artifactPath),
...Object.keys(templates)
]) {
const packageSubPath = join(artifactPath, packageSub);
const destSubPath = join(destPath, packageSub);

if (Object.keys(templates).indexOf(packageSub) >= 0) {
if (packageSub === "package.json") {
/**
* Copy package.json content in detail.
* Basically merge the generated package.json and dest package.json
* but prefer the values from dest when they contain the same key
* */
const destManifest = JSON.parse(readFileSync(destSubPath).toString());
const updatedManifest = {
...packageManifest,
...destManifest,
scripts: {
...packageManifest.scripts,
...destManifest.scripts
},
dependencies: {
...packageManifest.dependencies,
...destManifest.dependencies
},
devDependencies: {
...packageManifest.devDependencies,
...destManifest.devDependencies
}
};
const destManifest = JSON.parse(
existsSync(destSubPath)
? readFileSync(destSubPath).toString()
: handlebars.compile(templates[packageSub])(templateData)
);
const updatedManifest = mergeManifest(packageManifest, destManifest);
writeFileSync(destSubPath, JSON.stringify(updatedManifest, null, 2));
} else if (!existsSync(destSubPath)) {
//for files not yet exists and we have a template for it; generate from template
const file = handlebars.compile(templates[packageSub])(templateData);
writeFileSync(destSubPath, file);
} else {
//for files we have template but we already have a new version in clients folder, always prefer current one
//PASS
}
} else {
//For things not in codegen artifact black list, overwrite the existing ones.
if (lstatSync(packageSubPath).isDirectory()) ensureDirSync(destSubPath);
copySync(packageSubPath, destSubPath, { overwrite: true });
}
}
}
}

const mergeManifest = (source, dest) => {
return {
...source,
...dest,
scripts: {
...source.scripts,
...dest.scripts
},
dependencies: {
...source.dependencies,
...dest.dependencies
},
devDependencies: {
...source.devDependencies,
...dest.devDependencies
}
};
};

module.exports = { copyToClients, CODE_GEN_OUTPUT_DIR };
14 changes: 14 additions & 0 deletions scripts/generate-clients/templates/.gitignore.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/node_modules/
/build/
/coverage/
/docs/
/types/
/dist/
*.tsbuildinfo
*.tgz
*.log
package-lock.json

*.d.ts
*.js
*.js.map
4 changes: 4 additions & 0 deletions scripts/generate-clients/templates/.npmignore.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/coverage/
/docs/
tsconfig.test.json
*.tsbuildinfo
Loading

0 comments on commit 20b41ae

Please sign in to comment.