From 64009d6ebfb2e3f7db13412acf624662c1dc4539 Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Sun, 17 Nov 2024 08:37:17 +0100 Subject: [PATCH 1/2] chore: update dependencies script --- package.json | 3 +- scripts/update-dependencies.js | 52 ++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 scripts/update-dependencies.js diff --git a/package.json b/package.json index 28e411cf..1af9fafe 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "format": "pnpm --parallel format", "lint": "pnpm --parallel lint && eslint --cache --cache-location node_modules/.eslintcache", "test": "vitest run --silent", - "test:ui": "vitest --ui" + "test:ui": "vitest --ui", + "update-dependencies": "node ./scripts/update-dependencies.js" }, "devDependencies": { "@changesets/cli": "^2.27.9", diff --git a/scripts/update-dependencies.js b/scripts/update-dependencies.js new file mode 100644 index 00000000..af018dfa --- /dev/null +++ b/scripts/update-dependencies.js @@ -0,0 +1,52 @@ +import { execSync } from 'node:child_process'; +import fs from 'node:fs'; + +async function updateDependencies() { + execSync('pnpm update --recursive', { stdio: 'inherit' }); + await updateAddonDependencies(); + execSync('pnpm install', { stdio: 'inherit' }); +} + +async function updateAddonDependencies() { + const addonsBasePath = './packages/addons/'; + const addonFolders = fs + .readdirSync(addonsBasePath, { withFileTypes: true }) + .filter((item) => item.isDirectory()) + .map((item) => item.name) + .filter((x) => x != 'node_modules' && !x.startsWith('_')); + + for (const addonFolder of addonFolders) { + const filePath = `${addonsBasePath}/${addonFolder}/index.ts`; + if (!fs.existsSync(filePath)) continue; + + console.log(`Checking deps for ${addonFolder} addon`); + + let content = fs.readFileSync(filePath, { encoding: 'utf8' }); + + // regex to extract package name and version from `sv.dependency` and `sv.devDependency` + const regex = /sv\.(?:dependency|devDependency)\(['"]([^'"]+)['"],\s*['"]([^'"]+)['"]\)/g; + const matches = Array.from(content.matchAll(regex)); + + for (const match of matches) { + const [fullMatch, name, version] = match; + const newVersion = await getLatestVersion(name); + const updatedMatch = fullMatch.replace(version, `^${newVersion}`); + content = content.replace(fullMatch, updatedMatch); + } + + fs.writeFileSync(filePath, content); + } +} + +/** + * Gets the latest version of given package from the npm registry + * @param {string} name + * @returns {Promise} + */ +async function getLatestVersion(name) { + const response = await fetch(`https://registry.npmjs.org/${name}/latest`); + const json = await response.json(); + return json.version; +} + +updateDependencies(); From 5db26621b627a7ca5c429b413fbc0d86a9ac0f94 Mon Sep 17 00:00:00 2001 From: Manuel Serret Date: Sun, 17 Nov 2024 08:40:41 +0100 Subject: [PATCH 2/2] comments --- scripts/update-dependencies.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/update-dependencies.js b/scripts/update-dependencies.js index af018dfa..73f540d3 100644 --- a/scripts/update-dependencies.js +++ b/scripts/update-dependencies.js @@ -2,8 +2,13 @@ import { execSync } from 'node:child_process'; import fs from 'node:fs'; async function updateDependencies() { + // update all packages present in `package.json` execSync('pnpm update --recursive', { stdio: 'inherit' }); + + // update all packages that are used during scaffolding of `addons` await updateAddonDependencies(); + + // re-install all dependencies execSync('pnpm install', { stdio: 'inherit' }); }