-
-
Notifications
You must be signed in to change notification settings - Fork 430
/
Copy pathworkspace.ts
147 lines (116 loc) · 3.4 KB
/
workspace.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import type { CommonPackageJsonContent } from './types'
import { Logger } from './logger'
import { Package, readPackageJson } from './package'
import { ProjectRoot } from './path'
import { pnpmList } from './pnpm'
class CircularDependenciesError extends Error {
constructor(public currentName: string) {
super('Circular dependencies error')
}
}
class ForbiddenPackageRefError extends Error {
constructor(
public currentName: string,
public refName: string
) {
super(
`Public package cannot reference private package. Found '${refName}' in dependencies of '${currentName}'`
)
}
}
export class Workspace {
static PackageNames = pnpmList().map((p) => p.name)
readonly packages: Package[]
readonly packageJson: CommonPackageJsonContent
private readonly logger = new Logger('Milkdown')
readonly path = ProjectRoot
get version() {
return this.packageJson.version
}
get devDependencies() {
return this.packageJson.devDependencies ?? {}
}
get dependencies() {
return this.packageJson.dependencies ?? {}
}
get isTsProject() {
return this.join('tsconfig.json').exists()
}
constructor(list = pnpmList()) {
this.packageJson = readPackageJson(ProjectRoot)
const packages = new Map<string, Package>()
for (const meta of list) {
try {
const pkg = new Package(meta.name, meta)
pkg.workspace = this
packages.set(pkg.name, pkg)
} catch (e) {
this.logger.error(e as Error)
}
}
const building = new Set<string>()
try {
packages.forEach((pkg) => this.buildDeps(pkg, packages, building))
} catch (e) {
if (e instanceof CircularDependenciesError) {
const inProcessPackages = Array.from(building)
// console.log(inProcessPackages, e.currentName);
const circle = inProcessPackages
.slice(inProcessPackages.indexOf(e.currentName))
.concat(e.currentName)
this.logger.error(
`Circular dependencies found: \n ${circle.join(' -> ')}`
)
process.exit(1)
}
throw e
}
this.packages = Array.from(packages.values())
}
tryGetPackage(name: string) {
return this.packages.find((p) => p.name === name)
}
getPackage(name: string) {
const pkg = this.tryGetPackage(name)
if (!pkg) {
throw new Error(`Cannot find package with name '${name}'`)
}
return pkg
}
join(...paths: string[]) {
return this.path.join(...paths)
}
buildDeps(
pkg: Package,
packages: Map<string, Package>,
building: Set<string>
) {
if (pkg.deps.length) {
return
}
building.add(pkg.name)
pkg.deps = pkg.workspaceDependencies
.map((relativeDepPath) => {
const dep = packages.get(relativeDepPath)
if (!dep) {
this.logger.error(
`Cannot find package at ${relativeDepPath}. While build dependencies of ${pkg.name}`
)
return null
}
if (building.has(dep.name)) {
throw new CircularDependenciesError(dep.name)
}
if (!pkg.packageJson.private && dep.packageJson.private) {
throw new ForbiddenPackageRefError(pkg.name, dep.name)
}
this.buildDeps(dep, packages, building)
return dep
})
.filter(Boolean) as Package[]
building.delete(pkg.name)
}
forEach(callback: (pkg: Package) => void) {
this.packages.forEach(callback)
}
}