Skip to content

Commit

Permalink
chore: add utility to fix packages versions
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Sep 21, 2021
1 parent 59fe326 commit b652897
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ jobs:
with:
custom-glob: packages

version_consistency:
name: Check version consistency of packages
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: install node v12
uses: actions/setup-node@v1
with:
node-version: 12
- name: verify packages version consistency accross sub-modules
run: npm run check:versions

prettier:
name: Check coding style
runs-on: ubuntu-latest
Expand Down
91 changes: 91 additions & 0 deletions utils/check_package_version_consistency.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

const fs = require("fs");
const path = require("path");

const doDebug = false;
async function main() {

const dependencies = {};

async function exploreFolder(folderToExplore) {
const subFolders = await fs.promises.readdir(folderToExplore);
for (const subFolder of subFolders) {

const folderPath = path.join(folderToExplore, subFolder);
const packagejson = path.join(folderPath, "package.json");
if (fs.existsSync(packagejson)) {
if (doDebug) {
console.log("exploring ", folderPath);
}
const packageJson = JSON.parse(await fs.promises.readFile(packagejson, "utf8"));
if (packageJson.dependencies) {
for (const [moduleName, version] of Object.entries(packageJson.dependencies).concat(Object.entries(packageJson.devDependencies))) {
dependencies[moduleName] = dependencies[moduleName] || {};
dependencies[moduleName][version] = dependencies[moduleName][version] || [];
dependencies[moduleName][version].push(subFolder);
}
}
}
}
}

const rootFolder = path.join(__dirname, "..");
await exploreFolder(path.join(rootFolder, "packages"));
await exploreFolder(path.join(rootFolder, "examples/servients"));
await exploreFolder(path.join(rootFolder, "examples/templates"));
await exploreFolder(path.join(rootFolder, "examples/security"));

let nbErrors = 0;
// finding the packages that are present with multiple versions
for (const [module, versionPackages] of Object.entries(dependencies)) {
const versions = Object.keys(versionPackages);
if (versions.length !== 1) {
console.log("Warning module ", module, " has multiple versions ", versions.join(" "));
console.log(versionPackages);
nbErrors++;
}
}
if (nbErrors > 0) {

console.log(" => Please fix the error above first and rerun");
process.exit(1);
}


const oftenUsedPackages = [];
const rarelyUsedPackages = [];
for (const [module, versionPackages] of Object.entries(dependencies)) {
if (module.match(/^@node-wot/)) {
continue;
}
const theVersion = Object.keys(versionPackages)[0];
const usedInMoreThanOne = (versionPackages[theVersion].length > 1);
if (usedInMoreThanOne) {
if (doDebug) {
console.log("considering package that is used more than once: ", module);
}
oftenUsedPackages.push(`"${module}": "${theVersion}",`);
} else {
if (doDebug) {
console.log("ignoring package that is only used once: ", module);
}
rarelyUsedPackages.push(`"${module}": "${theVersion}",`);
}
}

console.log("Good ! the version number of all modules used are all consistent !");

const displayOftenUsedPackages = false;
if (displayOftenUsedPackages) {
// ---
console.log("\nSuggestion: now you can manually update the devDependencies section of the main package.json");
console.log("with the following packages. Those packages are installed more than once in one of the sub modules");

console.log("-----------\n")

console.log(oftenUsedPackages.sort().join("\n"));
}
process.exit(0);
}

main();

0 comments on commit b652897

Please sign in to comment.