Skip to content

Commit c4b0718

Browse files
authored
chore: copy package.json properly (#900)
1 parent 2f55292 commit c4b0718

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

Diff for: scripts/generate-clients/copy-to-clients.js

+44-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
const { join } = require("path");
22
const { copySync, ensureDirSync } = require("fs-extra");
3-
const { readdirSync, lstatSync, readFileSync, existsSync } = require("fs");
3+
const {
4+
readdirSync,
5+
lstatSync,
6+
readFileSync,
7+
existsSync,
8+
writeFileSync
9+
} = require("fs");
410
const { CODE_GEN_OUTPUT_DIR } = require("./code-gen-dir");
511

612
const getOverwritablePredicate = packageName => pathName => {
@@ -12,6 +18,7 @@ const getOverwritablePredicate = packageName => pathName => {
1218
"runtimeConfig.ts",
1319
"runtimeConfig.browser.ts",
1420
"runtimeConfig.shared.ts",
21+
"runtimeConfig.rn.ts",
1522
"index.ts",
1623
"endpoints.ts"
1724
];
@@ -25,6 +32,31 @@ const getOverwritablePredicate = packageName => pathName => {
2532
);
2633
};
2734

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+
*/
45+
const mergeManifest = (fromContent, toContent) => {
46+
const merged = {};
47+
const fromNames = Object.keys(fromConfig);
48+
for (const name of fromNames) {
49+
if (typeof toContent[name] === "object") {
50+
merged[name] = mergeManifest(fromContent[name], toContent[name]);
51+
} else {
52+
// If key (say dependency) is present in both codegen and
53+
// package.json, we prefer latter
54+
merged[name] = toContent[name] || fromContent[name];
55+
}
56+
}
57+
return merged;
58+
};
59+
2860
async function copyToClients(clientsDir) {
2961
for (const modelName of readdirSync(CODE_GEN_OUTPUT_DIR)) {
3062
if (modelName === "source") continue;
@@ -49,7 +81,17 @@ async function copyToClients(clientsDir) {
4981
for (const packageSub of readdirSync(artifactPath)) {
5082
const packageSubPath = join(artifactPath, packageSub);
5183
const destSubPath = join(destPath, packageSub);
52-
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+
) {
5395
//Overwrite the directories and files that are overwritable, or not yet exists
5496
if (lstatSync(packageSubPath).isDirectory()) ensureDirSync(destSubPath);
5597
copySync(packageSubPath, destSubPath, {

0 commit comments

Comments
 (0)