-
Notifications
You must be signed in to change notification settings - Fork 32
/
unifyVersions.js
executable file
·126 lines (108 loc) · 4.53 KB
/
unifyVersions.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env node
/*
* Copyright (c) Gala Games Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* This script is used to enforce the same version of a dependency in all packages.
*
* First, it checks if the version of the dependency in the root package.json is
* the same as version in child package.json files. If the version is different,
* it ends with an error.
*
* Then, it updates all versions of the dependencies in all package.json files,
* and chaincode template package lock file.
*/
const fs = require("fs");
const childProcess = require("child_process");
const packages = [
".",
"./chain-api",
"./chain-cli",
"./chain-cli/chaincode-template",
"./chain-client",
"./chain-connect",
"./chain-test",
"./chaincode"
].map((p) => {
const packageJsonPath = require.resolve(`${p}/package.json`);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const packageJson = require(packageJsonPath);
return { packageJsonPath, packageJson };
});
const optionalVersion = process.argv[2];
if (optionalVersion) {
console.log("Applying version from command line:", optionalVersion);
// just a sanity check
if (!optionalVersion.startsWith("2.0.")) {
console.error("Version must start with '2.0.'");
process.exit(1);
}
packages.forEach(({ packageJson, packageJsonPath }) => {
console.log(`Updating '${packageJson.name}' to version '${optionalVersion}'...`);
packageJson.version = optionalVersion;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, undefined, 2) + "\n");
});
}
const versions = new Set(packages.map((p) => p.packageJson.version));
if (versions.size !== 1) {
console.error("Versions are not the same for all packages!");
packages.forEach((p) =>
console.error(` - ${p.packageJson.version} of ${p.packageJson.name} in ${p.packageJsonPath}`)
);
process.exit(1);
}
const versionToApply = versions.values().next().value;
const depsToUpdate = packages.map((p) => p.packageJson.name);
packages.forEach(({ packageJson, packageJsonPath }) => {
console.log(`Unifying dependencies of '${packageJson.name}'...`);
const updated = [];
depsToUpdate.forEach((dep) => {
["dependencies", "devDependencies", "peerDependencies"].forEach((groupKey) => {
const current = packageJson[groupKey]?.[dep];
if (!!current && current !== versionToApply) {
console.log(` - updating '${dep}' from '${current}' to '${versionToApply}' in '${groupKey}'`);
packageJson[groupKey][dep] = versionToApply;
updated.push(groupKey);
}
});
});
if (updated.length === 0) {
console.log(` - everything is up to date`);
} else {
console.log(` - saving changes in '${packageJsonPath}'`);
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, undefined, 2) + "\n");
}
});
// manually apply changes to chaincode-template package-lock.json
console.log("Updating package-lock.json in 'chain-cli/chaincode-template'...");
const chaincodeTemplatePackageLockPath = require.resolve("./chain-cli/chaincode-template/package-lock.json");
// eslint-disable-next-line @typescript-eslint/no-var-requires
const chaincodeTemplatePackageLock = require(chaincodeTemplatePackageLockPath);
chaincodeTemplatePackageLock.version = versionToApply;
chaincodeTemplatePackageLock.packages[""].version = versionToApply;
// we don't want to keep versions of SDK modules in the lock file
Object.keys(chaincodeTemplatePackageLock.packages)
.filter((key) => key.startsWith("node_modules/@gala-chain"))
.forEach((key) => {
delete chaincodeTemplatePackageLock.packages[key];
});
console.log(` - saving changes in '${chaincodeTemplatePackageLockPath}'`);
fs.writeFileSync(
chaincodeTemplatePackageLockPath,
JSON.stringify(chaincodeTemplatePackageLock, undefined, 2) + "\n"
);
// execute `npm install` in the root directory to update the lock file and licenses
childProcess.execSync("npm install");
// execute `npm run build` in chain-cli to update the new version in README.md and oclif.manifest.json
childProcess.execSync("npm run build", { cwd: "chain-cli" });