Skip to content

Commit 7066100

Browse files
authored
Merge pull request #2246 from modernweb-dev/chore/lint-versions-fix
chore: lint versions --fix
2 parents ca87c9b + 294c0f6 commit 7066100

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

scripts/lint-versions.js

100644100755
Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/* eslint-disable */
2-
const { readdirSync, existsSync, readFileSync } = require('fs');
1+
#!/usr/bin/env node
2+
const { readdirSync, existsSync, readFileSync, writeFileSync } = require('fs');
33

44
const getDirectories = source =>
55
readdirSync(source, { withFileTypes: true })
@@ -32,11 +32,11 @@ function readPackageJsonNameVersion(filePath) {
3232
}
3333

3434
function compareVersions(versionsA, versionsB) {
35-
let output = '';
35+
let output = {};
3636
const newVersions = { ...versionsA };
3737
Object.keys(versionsB).forEach(dep => {
3838
if (versionsA[dep] && versionsB[dep] && versionsA[dep] !== versionsB[dep]) {
39-
output += ` - "${dep}" should be "${versionsA[dep]}" but is "${versionsB[dep]}"\n`;
39+
output[dep] = [versionsA[dep], versionsB[dep]];
4040
}
4141
if (!newVersions[dep]) {
4242
newVersions[dep] = versionsB[dep];
@@ -53,28 +53,48 @@ let currentVersions = readPackageJsonDeps('./package.json');
5353
let endReturn = 0;
5454

5555
// find all versions in the monorepo
56-
['./packages', './demo/projects'].forEach(rootDir => {
57-
getDirectories(rootDir).forEach(subPackage => {
58-
const filePath = `${rootDir}/${subPackage}/package.json`;
59-
currentVersions = { ...currentVersions, ...readPackageJsonNameVersion(filePath) };
60-
});
61-
});
56+
for (const subPackage of getDirectories('./packages')) {
57+
const filePath = `./packages/${subPackage}/package.json`;
58+
currentVersions = { ...currentVersions, ...readPackageJsonNameVersion(filePath) };
59+
}
60+
61+
const fixes = new Map();
6262

6363
// lint all versions in packages
64-
['./packages', './demo/projects'].forEach(rootDir => {
65-
getDirectories(rootDir).forEach(subPackage => {
66-
const filePath = `${rootDir}/${subPackage}/package.json`;
67-
const subPackageVersions = readPackageJsonDeps(filePath);
68-
const { output, newVersions } = compareVersions(currentVersions, subPackageVersions);
69-
currentVersions = { ...newVersions };
70-
if (output) {
71-
console.log(`Version mismatches found in "${filePath}":`);
72-
console.log(output);
73-
console.log();
74-
endReturn = 1;
75-
}
76-
});
77-
});
64+
for (const subPackage of getDirectories('./packages')) {
65+
const filePath = `./packages/${subPackage}/package.json`;
66+
const subPackageVersions = readPackageJsonDeps(filePath);
67+
const { output, newVersions } = compareVersions(currentVersions, subPackageVersions);
68+
currentVersions = { ...newVersions };
69+
const entries = Object.entries(output);
70+
if (entries.length) {
71+
fixes.set(filePath, output);
72+
console.log(`Version mismatches found in "${filePath}":`);
73+
console.log(
74+
entries.reduce(
75+
(acc, [dep, [should, is]]) => `${acc} - "${dep}" should be "${should}" but is "${is}"\n`,
76+
'',
77+
),
78+
);
79+
console.log();
80+
endReturn = 1;
81+
}
82+
}
83+
84+
function fixJSON(filePath, changes) {
85+
const json = JSON.parse(readFileSync(filePath, 'utf8'));
86+
for (const [dep, [should]] of Object.entries(changes)) {
87+
json.dependencies[dep] = should;
88+
}
89+
writeFileSync(filePath, JSON.stringify(json, null, 2));
90+
}
91+
92+
if (fixes.size && process.argv.includes('--fix')) {
93+
for (const [filePath, changes] of fixes) {
94+
fixJSON(filePath, changes);
95+
}
96+
console.log('package.json files updated, run `yarn`');
97+
}
7898

7999
if (endReturn === 0) {
80100
console.log('All versions are aligned 💪');

0 commit comments

Comments
 (0)