-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.ts
88 lines (71 loc) · 3.17 KB
/
index.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { promises as fsp } from 'fs'
import { dirname, resolve } from 'path'
import { ResolveOptions as _ResolveOptions, resolvePath } from 'mlly'
import { isAbsolute } from 'pathe'
import { findFile, FindFileOptions, findNearestFile } from './utils'
import type { PackageJson, TSConfig } from './types'
export * from './types'
export * from './utils'
export type ResolveOptions = _ResolveOptions & FindFileOptions
export function definePackageJSON (pkg: PackageJson): PackageJson {
return pkg
}
export function defineTSConfig (tsconfig: TSConfig): TSConfig {
return tsconfig
}
export async function readPackageJSON (id?: string, opts: ResolveOptions = {}): Promise<PackageJson> {
const resolvedPath = await resolvePackageJSON(id, opts)
const blob = await fsp.readFile(resolvedPath, 'utf-8')
return JSON.parse(blob) as PackageJson
}
export async function writePackageJSON (path: string, pkg: PackageJson): Promise<void> {
await fsp.writeFile(path, JSON.stringify(pkg, null, 2))
}
export async function readTSConfig (id?: string, opts: ResolveOptions = {}): Promise<TSConfig> {
const resolvedPath = await resolveTSConfig(id, opts)
const blob = await fsp.readFile(resolvedPath, 'utf-8')
const jsonc = await import('jsonc-parser')
return jsonc.parse(blob) as TSConfig
}
export async function writeTSConfig (path: string, tsconfig: TSConfig): Promise<void> {
await fsp.writeFile(path, JSON.stringify(tsconfig, null, 2))
}
export async function resolvePackageJSON (id: string = process.cwd(), opts: ResolveOptions = {}): Promise<string> {
const resolvedPath = isAbsolute(id) ? id : await resolvePath(id, opts)
return findNearestFile('package.json', { startingFrom: resolvedPath, ...opts })
}
export async function resolveTSConfig (id: string = process.cwd(), opts: ResolveOptions = {}): Promise<string> {
const resolvedPath = isAbsolute(id) ? id : await resolvePath(id, opts)
return findNearestFile('tsconfig.json', { startingFrom: resolvedPath, ...opts })
}
const lockFiles = ['yarn.lock', 'package-lock.json', 'pnpm-lock.yaml', 'npm-shrinkwrap.json', 'bun.lockb']
export async function resolveLockfile (id: string = process.cwd(), opts: ResolveOptions = {}): Promise<string> {
const resolvedPath = isAbsolute(id) ? id : await resolvePath(id, opts)
const _opts = { startingFrom: resolvedPath, ...opts }
for (const lockFile of lockFiles) {
try {
return await findNearestFile(lockFile, _opts)
} catch { }
}
throw new Error('No lockfile found from ' + id)
}
export async function findWorkspaceDir (id: string = process.cwd(), opts: ResolveOptions = {}): Promise<string> {
const resolvedPath = isAbsolute(id) ? id : await resolvePath(id, opts)
const _opts = { startingFrom: resolvedPath, ...opts }
// Lookup for .git/config
try {
const r = await findNearestFile('.git/config', _opts)
return resolve(r, '../..')
} catch { }
// Lookdown for lockfile
try {
const r = await resolveLockfile(resolvedPath, { ..._opts, reverse: true })
return dirname(r)
} catch { }
// Lookdown for package.json
try {
const r = await findFile(resolvedPath, _opts)
return dirname(r)
} catch { }
throw new Error('Cannot detect workspace root from ' + id)
}