-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonorepo.ts
More file actions
337 lines (281 loc) · 7.67 KB
/
monorepo.ts
File metadata and controls
337 lines (281 loc) · 7.67 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import semver from 'semver'
import { packagePrefix } from '@conventional-changelog/git-client'
import type { ReleaseData } from '../hosting/hosting.js'
import type { ProjectManifest } from '../manifest/index.js'
import {
type ProjectOptions,
bumpDefaultOptions,
Project
} from './project.js'
import type {
MonorepoMode,
MonorepoProjectOptions,
MonorepoProjectBumpOptions
} from './monorepo.types.js'
export * from './monorepo.types.js'
export abstract class MonorepoProject extends Project {
/**
* The mode of the monorepo.
* If mode is 'fixed', all projects will be bumped to the same version.
* If mode is 'independent', each project will be bumped to its own version.
*/
mode: MonorepoMode
declare options: MonorepoProjectOptions & ProjectOptions
private projectsMutex: Promise<Project[]> | undefined
/**
* Creates a new instance of the monorepo project.
* @param options - The options to use for the monorepo project.
*/
constructor(options: MonorepoProjectOptions) {
const {
mode,
compose
} = options
super({
...options,
compose: compose
? (manifest, isRoot = true) => compose(manifest, isRoot)
: manifest => manifest
})
this.mode = mode
}
/**
* Get the projects in the monorepo.
* @yields The projects in the monorepo.
*/
async* getProjects() {
if (!this.projectsMutex) {
const {
compose,
getProjects
} = this.options
const options = {
compose: (manifest: ProjectManifest) => compose!(manifest, false),
gitClient: this.gitClient,
manifest: this.manifest
}
const projects: Project[] = []
let resolve: (projects: Project[]) => void
this.projectsMutex = new Promise<Project[]>((r) => {
resolve = r
})
for await (const project of getProjects(options)) {
const isPrivate = await project.manifest.isPrivate()
if (!isPrivate) {
projects.push(project)
yield project
}
}
resolve!(projects)
return
}
yield* await this.projectsMutex
}
/**
* Get scope from project name.
* @param name - The name of the project.
* @returns The scope of the project.
*/
async getScope(name: string) {
if (this.options.scope) {
return await this.options.scope(name, await this.manifest.getName())
}
return name.replace(/@[^/]+\//, '')
}
/**
* Get tag prefix from scope.
* @param scope - The scope of the project.
* @returns The tag prefix for the project.
*/
async getTagPrefix(scope: string) {
if (this.options.tagPrefix) {
return await this.options.tagPrefix(scope)
}
if (this.mode === 'fixed') {
return ''
}
return packagePrefix(scope) as string
}
private getIndependentCommitMessage() {
const { versionUpdates } = this
if (versionUpdates.length === 0) {
throw new Error('Nothing to commit')
}
return `chore(release): monorepo release\n\n${
versionUpdates
.map(({ name, to }) => `- ${name}@${to}`)
.join('\n')
}`
}
override getCommitMessage() {
const { mode } = this
if (mode === 'fixed') {
return super.getCommitMessage()
}
return this.getIndependentCommitMessage()
}
private async getIndependentTags() {
const tags: string[] = []
for await (const project of this.getProjects()) {
const { manifest } = project
const name = await manifest.getName()
const scope = await this.getScope(name)
const tagPrefix = await this.getTagPrefix(scope)
tags.push(...await project.getTags({
tagPrefix
}))
}
return tags
}
override async getTags() {
const { mode } = this
if (mode === 'fixed') {
return super.getTags()
}
return this.getIndependentTags()
}
private async getIndependentReleaseData() {
const data: ReleaseData[] = []
for await (const project of this.getProjects()) {
const { manifest } = project
const name = await manifest.getName()
const scope = await this.getScope(name)
const tagPrefix = await this.getTagPrefix(scope)
const releaseData = await project.getReleaseData({
tagPrefix
})
data.push(...releaseData.map(data => ({
...data,
title: `${name}: ${data.title}`
})))
}
return data
}
override async getReleaseData() {
const { mode } = this
if (mode === 'fixed') {
return super.getReleaseData()
}
return this.getIndependentReleaseData()
}
private async getBumpOptions(
project: Project,
options: MonorepoProjectBumpOptions,
baseVersion?: string
) {
const {
preset = bumpDefaultOptions.preset,
extraScopes = [],
byProject,
...bumpOptions
} = options
const { manifest } = project
const name = await manifest.getName()
const scope = await this.getScope(name)
const tagPrefix = await this.getTagPrefix(scope)
const projectPreset = {
...typeof preset === 'string'
? {
name: preset
}
: preset,
scope: [scope, ...extraScopes]
}
const projectBumpOptions = {
...bumpOptions,
...byProject?.[name],
preset: projectPreset,
tagPrefix,
baseVersion
}
return projectBumpOptions
}
override getNextVersion(
options: MonorepoProjectBumpOptions = {}
): Promise<string | null> {
if (this.mode === 'fixed' && options.version) {
return super.getNextVersion(options)
}
throw new Error(
'Monorepo project does not support getNextVersion'
)
}
private async independentBump(
options: MonorepoProjectBumpOptions = {}
) {
let hasBump = false
for await (const project of this.getProjects()) {
const projectBumpOptions = await this.getBumpOptions(
project,
options
)
hasBump = await project.bump(projectBumpOptions) || hasBump
this.changedFiles.push(
...project.changedFiles
)
this.versionUpdates.push(
...project.versionUpdates
)
}
return hasBump
}
private async fixedBump(
options: MonorepoProjectBumpOptions = {}
) {
const { force } = options
const updatedProjects: {
project: Project
options: MonorepoProjectBumpOptions
}[] = []
const baseVersion = await this.manifest.getVersion()
let hasBump = false
let fixedVersion: string | undefined
for await (const project of this.getProjects()) {
const projectBumpOptions = await this.getBumpOptions(
project,
options,
baseVersion
)
const version = await project.getNextVersion(projectBumpOptions)
if (version || force) {
updatedProjects.push({
project,
options: projectBumpOptions
})
if (version && (!fixedVersion || semver.gt(version, fixedVersion))) {
fixedVersion = version
}
}
}
if (fixedVersion) {
hasBump = await super.bump({
...options,
forcePrivate: true,
version: fixedVersion,
tagPrefix: await this.getTagPrefix('')
})
for (const { project, options } of updatedProjects) {
hasBump = await project.bump({
...options,
version: fixedVersion
}) || hasBump
this.changedFiles.push(
...project.changedFiles
)
this.versionUpdates.push(
...project.versionUpdates
)
}
}
return hasBump
}
override async bump(
options: MonorepoProjectBumpOptions = {}
) {
const { mode } = this
if (mode === 'fixed') {
return this.fixedBump(options)
}
return this.independentBump(options)
}
}