Skip to content

Commit 49011ca

Browse files
committed
fix: can remove the key from the codegen
1 parent 05bd6fc commit 49011ca

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

scripts/generate-clients/copy-to-clients.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,28 @@ const getOverwritablePredicate = packageName => pathName => {
3232
);
3333
};
3434

35+
/**
36+
* Copy the keys from newly-generated package.json to
37+
* existing package.json. For each keys in new package.json
38+
* we prefer the new key. Whereas for the values, we prefer
39+
* the values in the existing package.json.
40+
*
41+
* This behavior enables us removing dependencies/scripts
42+
* from codegen, but maintain the newer dependency versions
43+
* in existing package.json
44+
*/
3545
const mergeManifest = (fromContent, toContent) => {
3646
const merged = {};
37-
const toNames = Object.keys(toContent);
38-
for (const name of toNames) {
39-
if (!fromContent[name]) {
40-
merged[name] = toContent[name];
41-
continue;
42-
}
47+
const fromNames = Object.keys(fromConfig);
48+
for (const name of fromNames) {
4349
if (typeof toContent[name] === "object") {
4450
merged[name] = mergeManifest(fromContent[name], toContent[name]);
4551
} else {
52+
// If key (say dependency) is present in both codegen and
53+
// package.json, we prefer latter
4654
merged[name] = toContent[name] || fromContent[name];
4755
}
4856
}
49-
for (const name of Object.keys(fromContent)) {
50-
if (toNames.indexOf(name) < 0) {
51-
merged[name] = fromContent[name];
52-
}
53-
}
5457
return merged;
5558
};
5659

@@ -78,19 +81,23 @@ async function copyToClients(clientsDir) {
7881
for (const packageSub of readdirSync(artifactPath)) {
7982
const packageSubPath = join(artifactPath, packageSub);
8083
const destSubPath = join(destPath, packageSub);
81-
if (overwritablePredicate(packageSub) || !existsSync(destSubPath)) {
84+
if (packageSub === "package.json") {
85+
//copy manifest file
86+
const destManifest = existsSync(destSubPath)
87+
? JSON.parse(readFileSync(destSubPath).toString())
88+
: {};
89+
const mergedManifest = mergeManifest(packageManifest, destManifest);
90+
writeFileSync(destSubPath, JSON.stringify(mergedManifest, null, 2));
91+
} else if (
92+
overwritablePredicate(packageSub) ||
93+
!existsSync(destSubPath)
94+
) {
8295
//Overwrite the directories and files that are overwritable, or not yet exists
8396
if (lstatSync(packageSubPath).isDirectory()) ensureDirSync(destSubPath);
8497
copySync(packageSubPath, destSubPath, {
8598
overwrite: true
8699
});
87100
}
88-
if (packageSub === "package.json") {
89-
//copy manifest file
90-
const destManifest = JSON.parse(readFileSync(destSubPath).toString());
91-
const mergedManifest = mergeManifest(packageManifest, destManifest);
92-
writeFileSync(destSubPath, JSON.stringify(mergedManifest, null, 2));
93-
}
94101
}
95102
}
96103
}

0 commit comments

Comments
 (0)