Automatically detect biome version from package manager lockfile
nhedger opened this issue · 4 comments
The setup action currently defaults to downloading the latest version of Biome when the version is not specified.
We'd like to explore the possibility of extracting the biome version from the package manager's lock file to setup the same version of Biome as specified in the project's dependencies.
Resolution order
Here's the desired resolution order.
graph LR;
A[specified version]-->B;
B[version from lockfile]-->C;
C[fallback to latest];
Package managers
We'd like to support the following package managers.
- npm (package-lock.json)
- pnpm (package-lock.yaml)
- yarn (yarn.lock)
- bun (bun.lockb)
References
Reading bun.lockb
Running bun bun.lockb
outputs a yarn compatible human readable lockfile (yaml)
Ideally, we'd want to be able to decode the file without having to install bun. Maybe we could ask users to generate the yarn lockfile to be able to use the feature.
bunfig.toml
[install.lockfile]
# whether to save a non-Bun lockfile alongside bun.lockb
# only "yarn" is supported
print = "yarn"
Reading package-lock.json
The currently installed version of biome can be found at following JSON path.
.packages."node_modules/@biomejs/biome".version
Here's an example to retrieve it from js/ts:
import { readFileSync } from 'fs';
const packageLock = JSON.parse(readFileSync('./package-lock.json', 'utf8'));
const version = packageLock.packages['node_modules/@biomejs/biome'].version;
console.log(`Biome version: ${version}`);
Reading pnpm-lock.yaml
The currently installed version of biome can be found at the following YAML path.
.dependencies."@biomejs/biome".version
Here's an example to retrieve in from js/ts:
import { parse } from 'yaml';
import { readFileSync } from 'fs';
const pnpmLock = parse(readFileSync('./pnpm-lock.yaml', 'utf8'));
const version = pnpmLock.dependencies['@biomejs/biome'].version;
Reading yarn.lock
The current installed version of biome can be found at the following YAML path:
"@biomejs/biome@[version-specifier]".version
Here's an example to retrieve it using yarn's own lockfile parser:
import { readFileSync } from 'fs';
import lockfile from '@yarnpkg/lockfile';
const yarnLock = lockfile.parse(readFileSync('./yarn.lock', 'utf8')).object;
const biome = Object.keys(yarnLock).filter(name => name.startsWith('@biomejs/biome@'))[0];
const version = yarnLock[biome].version;
console.log(version);