generated from sonofmagic/monorepo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutils.ts
65 lines (54 loc) · 1.44 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import type { PackageJson } from 'pkg-types'
import type { SyncOpts } from 'resolve'
import fs from 'fs-extra'
import path from 'pathe'
import pkg from 'resolve'
const { sync } = pkg
export function requireResolve(id: string, opts?: SyncOpts) {
return sync(id, opts)
}
function searchPackageJSON(dir: string) {
let packageJsonPath
while (true) {
if (!dir) {
return
}
const newDir = path.dirname(dir)
if (newDir === dir) {
return
}
dir = newDir
packageJsonPath = path.join(dir, 'package.json')
if (fs.existsSync(packageJsonPath)) {
break
}
}
return packageJsonPath
}
function getTailwindcssEntry(name: string = 'tailwindcss', opts?: SyncOpts) {
return requireResolve(name, opts)
}
function getPackageJsonPath(name: string, options: SyncOpts = {}) {
const entry = getTailwindcssEntry(name, options)
if (!entry) {
return
}
return searchPackageJSON(entry)
}
export function getPackageInfoSync(name: string, options: SyncOpts = {}) {
const packageJsonPath = getPackageJsonPath(name, options)
if (!packageJsonPath) {
return
}
const packageJson: PackageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
return {
name,
version: packageJson.version,
rootPath: path.dirname(packageJsonPath),
packageJsonPath,
packageJson,
}
}
export function isObject(val: any) {
return val !== null && typeof val === 'object' && Array.isArray(val) === false
};