Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update dependencies script #314

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
57 changes: 57 additions & 0 deletions scripts/update-dependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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' });
}

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<string>}
*/
async function getLatestVersion(name) {
const response = await fetch(`https://registry.npmjs.org/${name}/latest`);
const json = await response.json();
return json.version;
}

updateDependencies();