-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathalign-monorepo-dependencies.js
105 lines (89 loc) · 2.68 KB
/
align-monorepo-dependencies.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
'use strict';
// Manually align internal dependencies in packages/compass/package.json due to
// private packages and peer dependencies being ignored by `lerna version`
// command (see https://github.com/lerna/lerna/issues/1575)
const path = require('path');
const {
runInDir,
withProgress,
updatePackageJson,
} = require('@mongodb-js/monorepo-tools');
const LERNA_BIN = path.resolve(
__dirname,
'..',
'node_modules',
'.bin',
'lerna'
);
const NO_STAGE = process.argv.includes('--no-stage');
const NO_COMMIT = process.argv.includes('--no-commit');
const NO_PACKAGE_LOCK = process.argv.includes('--no-package-lock');
async function main() {
const packages = JSON.parse(
(await runInDir(`${LERNA_BIN} list --all --json --toposort`)).stdout
);
const packageToVersionMap = new Map(
packages.map((pkg) => [
pkg.name,
/^\d+\.\d+\.\d+-.+/.test(pkg.version)
? `${pkg.version}`
: `^${pkg.version}`,
])
);
for (const pkg of packages) {
await withProgress(
`Aligning monorepo dependencies versions for package ${pkg.name}`,
async () => {
await updatePackageJson(pkg.location, (packageJson) => {
for (const depType of [
'dependencies',
'devDependencies',
'peerDependencies',
]) {
if (!packageJson[depType]) {
continue;
}
for (const depName of Object.keys(packageJson[depType])) {
if (packageToVersionMap.has(depName)) {
const version = packageToVersionMap.get(depName);
packageJson[depType][depName] = version;
}
}
}
return packageJson;
});
}
);
}
if (!NO_PACKAGE_LOCK) {
await withProgress(
'Updating node_modules and package-lock at root',
async () => {
// We do full install here so not only package-lock is updated, but your
// local dependencies are up to date and ready for publish step
await runInDir('npm install');
}
);
}
if (!NO_STAGE) {
await withProgress('Staging changes for commit', async () => {
const updatedPackageLockFiles = packages
.map((pkg) => `${pkg.location}/package.json`)
.join(' ');
await runInDir(`git add package-lock.json ${updatedPackageLockFiles}`);
});
}
if (!NO_COMMIT) {
await withProgress('Committing changes', async () => {
await runInDir(
`git commit -m "chore(release): Update packages dependencies versions"`
);
});
}
}
process.on('unhandledRejection', (err) => {
console.error();
console.error(err.stack || err.message || err);
process.exitCode = 1;
});
main();