import { execSync } from 'node:child_process'
import semver from 'semver'
import { DefaultMap } from '../../../tailwindcss/src/utils/default-map'
import { getPackageVersionSync } from './package-version'
export function isMajor(version: number) {
return semver.satisfies(installedTailwindVersion(), `>=${version}.0.0 <${version + 1}.0.0`)
}
export function isGreaterThan(version: number) {
return semver.gte(installedTailwindVersion(), `${version + 1}.0.0`)
}
let cache = new DefaultMap((base) => {
let tailwindVersion = getPackageVersionSync('tailwindcss', base)
if (!tailwindVersion) throw new Error('Tailwind CSS is not installed')
return tailwindVersion
})
export function installedTailwindVersion(base = process.cwd()): string {
return cache.get(base)
}
let expectedCache = new DefaultMap((base) => {
try {
execSync('npm ls tailwindcss --json', { cwd: base, stdio: 'pipe' })
return installedTailwindVersion(base)
} catch (_e) {
try {
let e = _e as { stdout: Buffer }
let data = JSON.parse(e.stdout.toString())
return (
/"(.*?)" from the root project/.exec(data.dependencies.tailwindcss.invalid)?.[1] ??
installedTailwindVersion(base)
)
} catch {
return installedTailwindVersion(base)
}
}
})
export function expectedTailwindVersion(base = process.cwd()): string {
return expectedCache.get(base)
}