-
-
Notifications
You must be signed in to change notification settings - Fork 430
/
Copy pathpackage.ts
85 lines (69 loc) · 2.2 KB
/
package.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
import { readFileSync } from 'node:fs'
import { parse } from 'node:path'
import type { PackageItem } from './pnpm'
import type { CommonPackageJsonContent } from './types'
import type { Workspace } from './workspace'
import { Path } from './path'
import { pnpmList } from './pnpm'
export function readPackageJson(path: Path): CommonPackageJsonContent {
const content = readFileSync(path.join('package.json').toString(), 'utf-8')
return JSON.parse(content)
}
export class Package {
readonly name: string
readonly packageJson: CommonPackageJsonContent
readonly dirname: string
readonly path: Path
readonly srcPath: Path
readonly nodeModulesPath: Path
readonly libPath: Path
readonly distPath: Path
readonly version: string
readonly isTsProject: boolean
readonly workspaceDependencies: string[]
deps: Package[] = []
private _workspace: Workspace | null = null
get entry() {
return this.packageJson.main || this.packageJson.exports?.['.']
}
get dependencies() {
return this.packageJson.dependencies || {}
}
get devDependencies() {
return this.packageJson.devDependencies || {}
}
get workspace() {
if (!this._workspace) {
throw new Error('Workspace is not initialized')
}
return this._workspace
}
set workspace(workspace: Workspace) {
this._workspace = workspace
}
constructor(name: string, meta?: PackageItem) {
this.name = name
meta ??= pnpmList().find((item) => item.name === name)!
// parse paths
this.path = new Path(meta.path)
this.dirname = parse(meta.path).name
this.srcPath = this.path.join('src')
this.libPath = this.path.join('lib')
this.distPath = this.path.join('dist')
this.nodeModulesPath = this.path.join('node_modules')
// parse workspace
const packageJson = readPackageJson(this.path)
this.packageJson = packageJson
this.version = packageJson.version
this.workspaceDependencies = Object.keys(
packageJson.dependencies ?? {}
).filter((dep) => dep.startsWith('@milkdown/'))
this.isTsProject = this.path.join('tsconfig.json').isFile()
}
get scripts() {
return this.packageJson.scripts || {}
}
join(...paths: string[]) {
return this.path.join(...paths)
}
}