-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.ts
More file actions
320 lines (276 loc) · 7.51 KB
/
project.ts
File metadata and controls
320 lines (276 loc) · 7.51 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
import { join } from 'path'
import { ConventionalGitClient } from '@conventional-changelog/git-client'
import { Bumper } from 'conventional-recommended-bump'
import { ConventionalChangelog } from 'conventional-changelog'
import { concatStringStream } from '@simple-libs/stream-utils'
import { parseHostedGitUrl } from '@simple-libs/hosted-git-info'
import semver, { type ReleaseType } from 'semver'
import type { ProjectManifest } from '../manifest/index.js'
import {
addReleaseNotes,
extractLastRelease,
extractLastReleaseFromFile
} from '../change-log.js'
import type { ReleaseData } from '../hosting/index.js'
import { getReleaseType } from '../utils.js'
import type {
ProjectOptions,
ProjectBumpOptions,
ProjectVersionUpdate,
ProjectTagsOptions,
ProjectReleaseOptions,
ProjectPublishOptions
} from './project.types.js'
export * from './project.types.js'
export const bumpDefaultOptions = {
preset: {
name: 'conventionalcommits',
bumpStrict: true
}
}
/**
* A base class that represents a project.
*/
export abstract class Project {
/**
* The manifest of the project.
*/
manifest: ProjectManifest
/**
* The git client used to interact with the repository.
*/
gitClient: ConventionalGitClient
options: ProjectOptions
/**
* Changed files after interacting with the project.
*/
changedFiles: string[] = []
/**
* Version updates after interacting with the project.
*/
versionUpdates: ProjectVersionUpdate[] = []
/**
* Creates a new instance of the project.
* @param options - The options to use for the project.
*/
constructor(options: ProjectOptions) {
const {
manifest,
compose,
gitClient = new ConventionalGitClient(manifest.projectPath)
} = options
this.options = {
changelogFile: 'CHANGELOG.md',
...options
}
this.manifest = compose ? compose(manifest) : manifest
this.gitClient = gitClient
}
/**
* Get the hosted git information for the project.
* @returns The hosted git information.
*/
async getHostedGitInfo() {
const remote = await this.gitClient.getConfig('remote.origin.url')
const info = parseHostedGitUrl(remote)
return info
}
/**
* Get the commit message for the version updates.
* @returns The commit message.
*/
getCommitMessage() {
const { versionUpdates } = this
if (versionUpdates.length === 0) {
throw new Error('Nothing to commit')
}
return `chore(release): ${versionUpdates[0].to}`
}
/**
* Get new git tags for the version updates.
* @param options - The options to use for getting the tags.
* @returns The new git tags.
*/
async getTags(options: ProjectTagsOptions = {}) {
const {
manifest,
gitClient
} = this
const {
tagPrefix = 'v',
verify = true
} = options
const version = await manifest.getVersion()
const tag = `${tagPrefix}${version}`
if (verify) {
const isTagExists = await gitClient.verify(tag, true)
if (isTagExists) {
return []
}
}
return [tag]
}
/**
* Get the release data for the project.
* @param options - The options to use for getting the release data.
* @returns The release data.
*/
async getReleaseData(options: ProjectReleaseOptions = {}): Promise<ReleaseData[]> {
const {
manifest,
versionUpdates
} = this
const { changelogFile } = this.options
const { projectPath } = manifest
const changelogPath = join(projectPath, changelogFile!)
const version = await manifest.getVersion()
const isPrerelease = Boolean(await manifest.getPrereleaseVersion())
const lastRelease = versionUpdates.length
? await extractLastRelease(versionUpdates[0].notes)
: await extractLastReleaseFromFile(changelogPath)
if (!lastRelease || lastRelease.version && lastRelease.version !== version) {
return []
}
if (!lastRelease.nextTag) {
const tags = await this.getTags({
...options,
verify: false
})
if (tags.length) {
[lastRelease.nextTag] = tags
}
}
return [
{
...lastRelease,
title: `v${version}`,
version,
isPrerelease
}
]
}
/**
* Get the next version for the project.
* @param options - The options to use for getting the next version.
* @returns The next version.
*/
async getNextVersion(options: ProjectBumpOptions = {}): Promise<string | null> {
const {
gitClient,
manifest
} = this
if (options.skip || !options.forcePrivate && await manifest.isPrivate()) {
return null
}
const { projectPath } = manifest
const {
version: forcedVersion,
baseVersion,
as,
prerelease,
firstRelease: firstReleaseOption,
tagPrefix,
preset = bumpDefaultOptions.preset
} = options
let firstRelease = firstReleaseOption
if (forcedVersion && semver.valid(forcedVersion)) {
return forcedVersion
}
if (typeof firstRelease === 'undefined') {
firstRelease = !await gitClient.getLastTag({
path: projectPath
})
}
const version = baseVersion || await manifest.getVersion()
if (firstRelease) {
return version
}
let releaseType: ReleaseType | null = null
if (as) {
releaseType = as
} else {
const bump = await new Bumper(gitClient)
.loadPreset(preset, _ => import(_))
.commits({
path: projectPath
})
.tag({
prefix: tagPrefix
})
.bump()
if ('releaseType' in bump) {
releaseType = bump.releaseType
}
}
if (!releaseType) {
return null
}
const nextVersion = semver.inc(
version,
getReleaseType(releaseType, version, prerelease),
prerelease
)
return nextVersion
}
/**
* Bump the version of the project.
* @param options - The options to use for bumping the version.
* @returns Whether the version was bumped.
*/
async bump(options: ProjectBumpOptions = {}) {
const nextVersion = await this.getNextVersion(options)
if (!nextVersion) {
return false
}
const {
gitClient,
manifest
} = this
const { changelogFile } = this.options
const {
tagPrefix,
preset = bumpDefaultOptions.preset,
dryRun,
logger
} = options
const { projectPath } = manifest
const version = await manifest.getVersion()
const versionUpdate = {
...await manifest.writeVersion(nextVersion, dryRun),
notes: ''
}
this.changedFiles.push(...versionUpdate.files)
const changelogPath = join(projectPath, changelogFile!)
const name = await manifest.getName()
if (version === nextVersion) {
logger?.verbose(`${name}: ${nextVersion}`)
} else {
logger?.verbose(`${name}: ${version} -> ${nextVersion}`)
}
const notes = new ConventionalChangelog(gitClient)
.loadPreset(preset, _ => import(_))
.commits({
path: projectPath
})
.tags({
prefix: tagPrefix
})
.readRepository()
.context({
version: nextVersion
})
.write()
versionUpdate.notes = dryRun
? await concatStringStream(notes)
: await addReleaseNotes(changelogPath, notes)
logger?.verbose(`Release notes:\n\n${versionUpdate.notes}`)
this.changedFiles.push(changelogPath)
this.versionUpdates.push(versionUpdate)
return true
}
/**
* Publish the project.
* @param options
*/
abstract publish(options?: ProjectPublishOptions): Promise<void>
}