Skip to content
Merged
Changes from 4 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
33 changes: 32 additions & 1 deletion scripts/generate-lockfiles.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync } from 'node:fs';
import { existsSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { spawnSync } from 'node:child_process';
import { getExampleDirs } from './common-lockfiles.mjs';
Expand All @@ -11,6 +11,15 @@ if (dirs.length === 0) {
process.exit(1);
}

const pnpmVersion = getPnpmVersionFromPath();
console.log(`Using pnpm version: ${pnpmVersion}`);
const expectedVersion = getExpectedVersionFromPackageJson();
// assert pnpm V
if (pnpmVersion !== expectedVersion) {
console.error(`Expected pnpm version ${expectedVersion}, got ${pnpmVersion}`);
process.exit(1);
}

let failures = 0;
for (const pkgDir of dirs) {
console.log(`\n[lockfile] ${pkgDir}`);
Expand Down Expand Up @@ -43,3 +52,25 @@ if (failures) {
}

console.log('\nDone.');

function getExpectedVersionFromPackageJson() {
const packageJson = JSON.parse(readFileSync(join(process.cwd(), 'package.json'), 'utf8'));
const packageManager = packageJson.packageManager;
if (!packageManager?.startsWith('pnpm@')) {
throw new Error(`Invalid packageManager: ${packageManager || 'missing'}. Expected format: "[email protected]"`);
}
return packageManager.slice(5); // Remove "pnpm@" prefix
}
Comment on lines +56 to +63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes that we will always have fixed versions and no trailing hash (example here https://github.com/LayerZero-Labs/docs/blob/main/package.json#L102).

I think it's a safe assumption given the timeline of the repo though.


function getPnpmVersionFromPath() {
const { status, stdout, error } = spawnSync('pnpm', ['--version'], { encoding: 'utf8' });
if (status !== 0) {
const hint = error?.code === 'ENOENT' ? 'pnpm is not on PATH.' : 'failed to run "pnpm --version".';
throw new Error(`Could not determine pnpm version: ${hint}`);
}
const version = stdout.trim();
if (!version) {
throw new Error('Empty version output from "pnpm --version".');
}
return version;
}