Skip to content

Commit

Permalink
Added script to check version-sync between workspace (#3486)
Browse files Browse the repository at this point in the history
* ✨ Added script for verifying dep-versions across workspace

* ✨ Added script for verifying dep-versions across workspace

* ✨ Added script for verifying dep-versions across workspace

* ✨ Added script for verifying dep-versions across workspace

* ✨ Added script for verifying dep-versions across workspace

* 📝 Better texts

* 📝 Better texts

* 📝 Added comments

* 📝 Updated minoer package-differences

* 📝 Upgraded stylelint

* 📝 Upgraded babel-loader

* 📝 Add ^ to ds-css versionsing across repo

* ♻️ Small refactor to reduce use of forEach
  • Loading branch information
KenAJoh authored Jan 17, 2025
1 parent 627d6d9 commit 8b5bfb4
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 184 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname $0)/_/husky.sh"

yarn lint-staged
yarn lint-staged && yarn tsx ./scripts/in-sync-versions.ts
2 changes: 1 addition & 1 deletion @navikt/aksel-icons/figma-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@svgr/core": "^8.1.0",
"@types/node": "^18.15.11",
"@types/postcss-url": "^10.0.4",
"@types/react": "^18.0.0",
"@types/react": "^18.3.11",
"@types/react-dom": "^18.0.0",
"@vitejs/plugin-react": "^4.2.1",
"postcss": "^8.4.31",
Expand Down
2 changes: 1 addition & 1 deletion @navikt/aksel-icons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"react": "^18.0.0",
"rehype-parse": "8.0.4",
"tsc-alias": "1.8.8",
"tsx": "^4.7.1",
"tsx": "^4.19.1",
"typescript": "5.5.4",
"unified": "10.1.2",
"vitest": "^2.1.8"
Expand Down
4 changes: 2 additions & 2 deletions @navikt/aksel-stylelint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
"concurrently": "9.0.1",
"postcss-selector-parser": "^6.0.13",
"postcss-value-parser": "^4.2.0",
"stylelint": "^14.8.5",
"stylelint": "^16.10.0",
"stylelint-test-rule-node": "^0.2.1",
"tsx": "^4.7.1",
"tsx": "^4.19.1",
"typescript": "5.5.4"
}
}
4 changes: 2 additions & 2 deletions @navikt/aksel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"homepage": "https://aksel.nav.no/grunnleggende/kode/kommandolinje",
"dependencies": {
"@navikt/ds-css": "7.9.1",
"@navikt/ds-css": "^7.9.1",
"axios": "1.7.4",
"chalk": "4.1.0",
"clipboardy": "^2.3.0",
Expand All @@ -40,7 +40,7 @@
"is-git-clean": "1.1.0",
"jscodeshift": "^0.15.1",
"jscodeshift-add-imports": "1.0.11",
"lodash": "4.17.21",
"lodash": "^4.17.21",
"react-scanner": "^1.1.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion @navikt/core/css/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"postcss-cli": "^9.0.0",
"postcss-combine-duplicated-selectors": "10.0.3",
"postcss-import": "^14.1.0",
"tsx": "^4.7.1",
"tsx": "^4.19.1",
"vitest": "^2.1.8"
},
"browserslist": [
Expand Down
4 changes: 2 additions & 2 deletions aksel.nav.no/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@
"@sanity/block-tools": "3.59.0",
"@sanity/schema": "3.59.0",
"@types/jscodeshift": "^0.11.11",
"@types/react": "^18.0.0",
"@types/react": "^18.3.11",
"@types/react-dom": "^18.0.0",
"autoprefixer": "^10.4.20",
"babel-loader": "^8.2.0",
"babel-loader": "^9.1.3",
"copyfiles": "^2.4.1",
"cross-env": "^7.0.0",
"jsdom": "25.0.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@navikt/aksel-icons": "file:./../../@navikt/aksel-icons",
"@navikt/ds-css": "file:./../../@navikt/core/css",
"@navikt/ds-react": "file:./../../@navikt/core/react",
"@types/react": "^18.2.48",
"@types/react": "^18.3.11",
"@types/react-dom": "^18.2.18",
"astro": "^4.15.8",
"react": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/referansesider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@eslint/js": "^9.9.0",
"@tanstack/router-devtools": "^1.57.17",
"@tanstack/router-plugin": "^1.57.15",
"@types/react": "^18.3.3",
"@types/react": "^18.3.11",
"@types/react-dom": "^18.3.0",
"@vitejs/plugin-react-swc": "^3.5.0",
"@vitest/eslint-plugin": "^1.1.4",
Expand Down
2 changes: 1 addition & 1 deletion examples/shadow-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.2.48",
"@types/react": "^18.3.11",
"@types/react-dom": "^18.2.18",
"@vitejs/plugin-react": "^4.2.1",
"typescript": "5.5.4",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
"stylelint-config-standard": "^36.0.0",
"stylelint-declaration-block-no-ignored-properties": "^2.6.0",
"stylelint-value-no-unknown-custom-properties": "^6.0.0",
"tsx": "^4.19.1",
"typescript": "5.5.4",
"vite": "^5.4.6",
"vite-plugin-turbosnap": "^1.0.3",
Expand Down
119 changes: 119 additions & 0 deletions scripts/in-sync-versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { execSync } from "child_process";
import { readFileSync } from "fs";
import { join } from "path";

validateVersions();

function validateVersions() {
const workspaces = getYarnWorkspacesList();

if (workspaces.length === 0) {
console.error("No workspaces found");
return;
}

const dependencies = new Map<string, string[]>();

for (const { location } of workspaces) {
const packageJson = JSON.parse(
readFileSync(join(location, "./package.json"), { encoding: "utf-8" }),
);

for (const localDependency of [
packageJson.dependencies,
packageJson.devDependencies,
packageJson.peerDependencies,
]) {
if (!localDependency) {
continue;
}

for (const [dependency, version] of Object.entries(localDependency)) {
if (!dependencies.has(dependency)) {
dependencies.set(dependency, []);
}
dependencies.get(dependency)?.push(version as string);
}
}
}

const warnings: { dependency: string; filteredVersions: string[] }[] = [];

for (const [dependency, versions] of dependencies) {
/**
* While we could resolve these cases using "semver" or other packages,
* we are going to keep it simple and just check "regular" cases.
*/
const filteredVersions = versions.filter(
(version) => version !== "*" && !version.includes(">="),
);

const versionsAreEqual = filteredVersions.every(
(version) => version === filteredVersions[0],
);

if (!versionsAreEqual) {
warnings.push({
dependency,
/* To keep console simple, we hide duplicates */
filteredVersions: [...new Set(filteredVersions)],
});
}
}

if (warnings.length > 0) {
console.warn(
"\nWorkspaces local dependency versions not synced across repository:",
);

for (const { dependency, filteredVersions } of warnings) {
logWarning(dependency, filteredVersions);
}

console.warn(
"\nPlease make sure all workspaces have the same version for each dependency.\n\n",
);
}
}

/**
* Get the list of workspaces in the repository by using the `yarn workspaces list` command
*/
function getYarnWorkspacesList(): { location: string; name: string }[] {
try {
const execCommand = execSync("yarn workspaces list --json", {
encoding: "utf-8",
});

const workspaces = execCommand
.split("\n")
.filter(Boolean)
.map((line) => {
const { location, name } = JSON.parse(line);
return { location, name };
});

return workspaces;
} catch {
console.error("Failed to get workspaces list");
}
return [];
}

/**
* Adds some colors to better highlight the warning
*/
function logWarning(dependency: string, filteredVersions: string[]) {
const colors = {
reset: "\x1b[0m",
red: "\x1b[31m",
yellow: "\x1b[33m",
};

const coloredDependency = `${colors.yellow}${dependency}${colors.reset}`;
const coloredVersions = `${colors.red}${filteredVersions.join(", ")}${
colors.reset
}`;

console.warn(`- ${coloredDependency}: ${coloredVersions}`);
}
Loading

0 comments on commit 8b5bfb4

Please sign in to comment.