-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmerge-package-json.js
49 lines (41 loc) · 1.4 KB
/
merge-package-json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import fs from 'fs';
// combines package.jsons and writes them to /tmp/merged-package.json
function mergePackageJson() {
const templatePackage = JSON.parse(fs.readFileSync("package.json", "utf8"));
if (fs.existsSync("/app/package.json")) {
const servicePackage = JSON.parse(
fs.readFileSync("/app/package.json", "utf8")
);
servicePackage.dependencies = servicePackage.dependencies || {};
servicePackage.dependencies = {
...servicePackage.dependencies,
...templatePackage.dependencies,
};
warnAboutVersionDifferences(templatePackage, servicePackage);
fs.writeFileSync(
"/tmp/merged-package.json",
JSON.stringify(servicePackage, null, 2)
);
} else {
// just use the template package.json
fs.writeFileSync(
"/tmp/merged-package.json",
JSON.stringify(templatePackage, null, 2)
);
}
}
function warnAboutVersionDifferences(templatePackage, servicePackage) {
Object.keys(templatePackage.dependencies).forEach((dep) => {
if (
servicePackage.dependencies[dep] &&
templatePackage.dependencies[dep] !== servicePackage.dependencies[dep]
) {
const winner = "template";
// QUESTION: Can we cope with compatible definitions?
console.warn(
`Warning: Dependency ${dep} has different versions in template and service package.json. Using ${winner} version.`
);
}
});
}
mergePackageJson();