import { readFileSync } from 'node:fs' import path from 'node:path' import semver from 'semver' import { findPackageDirSync } from '../build/packages/package-dir' import { projectPackages } from './project-packages' import { protobaseVersion } from './version' // Packages every project installs, all released at the same version; the first found names the project's. const installedPackages = ['@protobase/cli', '@protobase/server', '@protobase/ui'] /** * The Protobase version installed for the project in `projectDir`: the version its lockfile installed, read from the * @protobase package Node's resolution finds from there. Undefined outside a project, or before its install. */ export const projectProtobaseVersion = (projectDir: string): string | undefined => { for (const name of installedPackages) { const dir = findPackageDirSync(name, projectDir) if (dir) return (JSON.parse(readFileSync(path.join(dir, 'package.json'), 'utf8')) as { version: string }).version } return undefined } /** * The Protobase version a bundle of the project in `projectDir` runs on, its manifest's `protobase`: the version of the * copies the build uses, the CLI's own in a bundled project and the installed ones in a locked project, which fails * before its install. */ export const bundleProtobaseVersion = (projectDir: string, cliVersion = protobaseVersion) => { if (projectPackages(projectDir).mode === 'bundled') return cliVersion const installed = projectProtobaseVersion(projectDir) if (installed === undefined) throw new Error(`${projectDir} lists its @protobase packages in dependencies and has none installed; install the project's dependencies first`) return installed } // The installed copies must be ones this CLI can run: the CLI's version in the caret range of theirs, so from 1.0 on // the same major and below 1.0, where a minor may break, the same minor, and never older. const checkInstalled = (projectDir: string, cliVersion: string) => { const installed = projectProtobaseVersion(projectDir) if (installed === undefined) return const range = `^${installed}` if (semver.satisfies(cliVersion, range, { includePrerelease: true })) return throw new Error(`This project has Protobase ${installed} installed, and this CLI is Protobase ${cliVersion}: run it with a Protobase ${range} CLI`) } /** * Refuses a project whose Protobase version this CLI does not satisfy, the one version check the CLI makes: the * specifier its package.json lists the @protobase packages at must hold the CLI's version, by semver, and in a locked * project, whose installed copies run, those must be ones the CLI runs. A specifier that is no semver range, such as * `workspace:*`, says no version, so the installed copies are checked instead. A prerelease CLI, such as a build from main, * counts as well, just before its own release: 0.94.4-main.x holds ^0.94.0, 0.95.0-main.x does not. Outside a project, or one that lists no * @protobase package, there is nothing to check. */ export const checkProjectVersion = (projectDir: string, cliVersion = protobaseVersion) => { const { mode, specifier } = projectPackages(projectDir) if (specifier === undefined) return const range = semver.validRange(specifier) !== null if (range && !semver.satisfies(cliVersion, specifier, { includePrerelease: true })) { throw new Error(`This project asks for Protobase ${specifier}, and this CLI is Protobase ${cliVersion}: run it with a CLI whose version ${specifier} holds`) } if (mode === 'locked' || !range) checkInstalled(projectDir, cliVersion) }