Philipp Spiess b3d1cbb72f
Upgrade: Error when project is already using Tailwind CSS v4 (#15015)
In some local testing we ran the `@tailwindcss/upgrade` command twice in
a row. It would be great to get some feedback that this is not working,
so this PR now checks if it can resolve the installed version of
`tailwindcss` and if it can, it requires it to be < 4 (you can bypass
this check with `--force`).

---------

Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
2024-11-19 15:53:02 +01:00

19 lines
541 B
TypeScript

import fs from 'node:fs/promises'
import { createRequire } from 'node:module'
const localResolve = createRequire(import.meta.url).resolve
/**
* Resolves the version string of an npm dependency installed in the based
* directory.
*/
export async function getPackageVersion(pkg: string, base: string): Promise<string | null> {
try {
let packageJson = localResolve(`${pkg}/package.json`, { paths: [base] })
let { version } = JSON.parse(await fs.readFile(packageJson, 'utf8'))
return version
} catch {
return null
}
}