Skip to content

Commit 20b41ae

Browse files
committed
chore: generate package static components from templates
1 parent e08b134 commit 20b41ae

11 files changed

+506
-43
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"cucumber": "0.5.x",
4242
"fs-extra": "^8.1.0",
4343
"generate-changelog": "^1.7.1",
44+
"handlebars": "^4.5.3",
4445
"husky": "^3.0.0",
4546
"jest": "^24.7.1",
4647
"jmespath": "^0.15.0",

scripts/generate-clients/code-gen.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@ const CODE_GEN_ROOT = path.normalize(
1313
async function generateClients(models) {
1414
console.info("models directory: ", models);
1515
if (models === CODE_GEN_INPUT_DIR) {
16-
// console.log("skipping copying models to codegen directory");
16+
// This script will clean the CODE_GEN_INPUT_DIR in execution.
17+
// throw to avoid input model being removed
1718
throw new Error(
1819
`models directory cannot be the same as ${CODE_GEN_INPUT_DIR}`
1920
);
2021
} else {
2122
console.log(`clearing code gen input folder...`);
2223
emptyDirSync(CODE_GEN_INPUT_DIR);
2324
console.log(`copying models from ${models} to ${CODE_GEN_INPUT_DIR}...`);
24-
// copySync(models, CODE_GEN_INPUT_DIR, {
25-
// overwrite: true
26-
// });
2725
for (const modelFileName of readdirSync(models)) {
2826
const modelPath = path.join(models, modelFileName);
2927
if (!lstatSync(modelPath).isFile()) continue;
Lines changed: 70 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const path = require("path");
1+
const { join, normalize } = require("path");
22
const { copySync, ensureDirSync } = require("fs-extra");
3+
const handlebars = require("handlebars");
34
const {
45
readdirSync,
56
lstatSync,
@@ -8,8 +9,8 @@ const {
89
writeFileSync
910
} = require("fs");
1011

11-
const CODE_GEN_OUTPUT_DIR = path.normalize(
12-
path.join(
12+
const CODE_GEN_OUTPUT_DIR = normalize(
13+
join(
1314
__dirname,
1415
"..",
1516
"..",
@@ -21,69 +22,101 @@ const CODE_GEN_OUTPUT_DIR = path.normalize(
2122
)
2223
);
2324

24-
const unOverridables = [
25-
"package.json",
26-
"tsconfig.es.json",
27-
"tsconfig.json",
28-
"tsconfig.test.json"
29-
];
25+
/**
26+
* templates are a black list of files we don't want codegen artifact
27+
* to override in the clients folder
28+
*/
29+
const templates = readdirSync(join(__dirname, "templates"))
30+
.filter(name => /.sample$/.test(name))
31+
.reduce((accumulator, curr) => {
32+
const templatePath = join(__dirname, "templates", curr);
33+
const template = readFileSync(templatePath).toString();
34+
accumulator[curr.replace(/.sample$/, "")] = template;
35+
return accumulator;
36+
}, {});
3037

3138
async function copyToClients(clientsDir) {
3239
for (const modelName of readdirSync(CODE_GEN_OUTPUT_DIR)) {
3340
if (modelName === "source") continue;
34-
const artifactPath = path.join(
41+
const artifactPath = join(
3542
CODE_GEN_OUTPUT_DIR,
3643
modelName,
3744
"typescript-codegen"
3845
);
39-
const packageManifestPath = path.join(artifactPath, "package.json");
46+
const packageManifestPath = join(artifactPath, "package.json");
4047
if (!existsSync(packageManifestPath)) {
4148
console.error(`${modelName} generates empty client, skip.`);
4249
continue;
4350
}
51+
4452
const packageManifest = JSON.parse(
4553
readFileSync(packageManifestPath).toString()
4654
);
47-
const packageName = packageManifest.name.replace("@aws-sdk/", "");
55+
const packageName = packageManifest.name;
4856
console.log(`copying ${packageName} from ${artifactPath} to ${clientsDir}`);
49-
const destPath = path.join(clientsDir, packageName);
50-
for (const packageSub of readdirSync(artifactPath)) {
51-
const packageSubPath = path.join(artifactPath, packageSub);
52-
const destSubPath = path.join(destPath, packageSub);
53-
if (unOverridables.indexOf(packageSub) >= 0) {
54-
if (!existsSync(destSubPath))
55-
copySync(packageSubPath, destSubPath, { overwrite: true });
56-
else if (packageSub === "package.json") {
57+
const destPath = join(clientsDir, packageName.replace("@aws-sdk/", ""));
58+
59+
//Data used to generate files from template
60+
const templateData = {
61+
year: new Date().getFullYear(),
62+
packageName
63+
};
64+
65+
for (const packageSub of [
66+
...readdirSync(artifactPath),
67+
...Object.keys(templates)
68+
]) {
69+
const packageSubPath = join(artifactPath, packageSub);
70+
const destSubPath = join(destPath, packageSub);
71+
72+
if (Object.keys(templates).indexOf(packageSub) >= 0) {
73+
if (packageSub === "package.json") {
5774
/**
5875
* Copy package.json content in detail.
5976
* Basically merge the generated package.json and dest package.json
6077
* but prefer the values from dest when they contain the same key
6178
* */
62-
const destManifest = JSON.parse(readFileSync(destSubPath).toString());
63-
const updatedManifest = {
64-
...packageManifest,
65-
...destManifest,
66-
scripts: {
67-
...packageManifest.scripts,
68-
...destManifest.scripts
69-
},
70-
dependencies: {
71-
...packageManifest.dependencies,
72-
...destManifest.dependencies
73-
},
74-
devDependencies: {
75-
...packageManifest.devDependencies,
76-
...destManifest.devDependencies
77-
}
78-
};
79+
const destManifest = JSON.parse(
80+
existsSync(destSubPath)
81+
? readFileSync(destSubPath).toString()
82+
: handlebars.compile(templates[packageSub])(templateData)
83+
);
84+
const updatedManifest = mergeManifest(packageManifest, destManifest);
7985
writeFileSync(destSubPath, JSON.stringify(updatedManifest, null, 2));
86+
} else if (!existsSync(destSubPath)) {
87+
//for files not yet exists and we have a template for it; generate from template
88+
const file = handlebars.compile(templates[packageSub])(templateData);
89+
writeFileSync(destSubPath, file);
90+
} else {
91+
//for files we have template but we already have a new version in clients folder, always prefer current one
92+
//PASS
8093
}
8194
} else {
95+
//For things not in codegen artifact black list, overwrite the existing ones.
8296
if (lstatSync(packageSubPath).isDirectory()) ensureDirSync(destSubPath);
8397
copySync(packageSubPath, destSubPath, { overwrite: true });
8498
}
8599
}
86100
}
87101
}
88102

103+
const mergeManifest = (source, dest) => {
104+
return {
105+
...source,
106+
...dest,
107+
scripts: {
108+
...source.scripts,
109+
...dest.scripts
110+
},
111+
dependencies: {
112+
...source.dependencies,
113+
...dest.dependencies
114+
},
115+
devDependencies: {
116+
...source.devDependencies,
117+
...dest.devDependencies
118+
}
119+
};
120+
};
121+
89122
module.exports = { copyToClients, CODE_GEN_OUTPUT_DIR };
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/node_modules/
2+
/build/
3+
/coverage/
4+
/docs/
5+
/types/
6+
/dist/
7+
*.tsbuildinfo
8+
*.tgz
9+
*.log
10+
package-lock.json
11+
12+
*.d.ts
13+
*.js
14+
*.js.map
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/coverage/
2+
/docs/
3+
tsconfig.test.json
4+
*.tsbuildinfo

0 commit comments

Comments
 (0)