Skip to content

Commit bc197f8

Browse files
committed
add version related helpers
1 parent 59045df commit bc197f8

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

packages/@tailwindcss-upgrade/src/utils/package-version.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { readFileSync } from 'node:fs'
12
import fs from 'node:fs/promises'
23
import { resolveJsId } from './resolve'
34

@@ -15,3 +16,14 @@ export async function getPackageVersion(pkg: string, base: string): Promise<stri
1516
return null
1617
}
1718
}
19+
20+
export function getPackageVersionSync(pkg: string, base: string): string | null {
21+
try {
22+
let packageJson = resolveJsId(`${pkg}/package.json`, base)
23+
if (!packageJson) return null
24+
let { version } = JSON.parse(readFileSync(packageJson, 'utf8'))
25+
return version
26+
} catch {
27+
return null
28+
}
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import semver from 'semver'
2+
import { DefaultMap } from '../../../tailwindcss/src/utils/default-map'
3+
import { getPackageVersionSync } from './package-version'
4+
5+
/**
6+
* Must be of major version.
7+
*
8+
* E.g.: `isMajor(3)`
9+
*/
10+
export function isMajor(version: number) {
11+
return semver.satisfies(installedTailwindVersion(), `>=${version}.0.0 <${version + 1}.0.0`)
12+
}
13+
14+
let cache = new DefaultMap((base) => {
15+
let tailwindVersion = getPackageVersionSync('tailwindcss', base)
16+
if (!tailwindVersion) throw new Error('Tailwind CSS is not installed')
17+
return tailwindVersion
18+
})
19+
20+
function installedTailwindVersion(base = process.cwd()): string {
21+
return cache.get(base)
22+
}

0 commit comments

Comments
 (0)