Skip to content

Commit a14ab74

Browse files
committed
refactor(core): rewrite ReleaseCreator to GitRepositoryHosting
1 parent d94d4bb commit a14ab74

22 files changed

+760
-329
lines changed

packages/core/README.md

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ A simple tool to release projects with monorepo support.
3636
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
3737
<a href="#usage">Usage</a>
3838
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
39-
<a href="#why">Why</a>
40-
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
4139
<a href="#addons">Addons</a>
4240
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
4341
<a href="#custom-addons">Custom addons</a>
@@ -60,18 +58,19 @@ npm i @simple-release/core
6058
```js
6159
import { Releaser } from '@simple-release/core'
6260
import { PnpmProject } from '@simple-release/pnpm'
63-
import { GithubReleaseCreator } from '@simple-release/github-release'
64-
65-
const project = new PnpmProject()
61+
import { GithubHosting } from '@simple-release/github'
6662

67-
await new Releaser(project)
63+
await new Releaser({
64+
project: new PnpmProject(),
65+
hosting: new GithubHosting({
66+
token: process.env.GITHUB_TOKEN
67+
})
68+
})
6869
.bump()
6970
.commit()
7071
.tag()
7172
.push()
72-
.release(new GithubReleaseCreator({
73-
token: process.env.GITHUB_TOKEN
74-
}))
73+
.release()
7574
.publish()
7675
.run()
7776
```
@@ -81,13 +80,16 @@ Monorepo example:
8180
```js
8281
import { Releaser } from '@simple-release/core'
8382
import { PnpmWorkspacesProject } from '@simple-release/pnpm'
84-
import { GithubReleaseCreator } from '@simple-release/github-release'
83+
import { GithubHosting } from '@simple-release/github'
8584

86-
const project = new PnpmWorkspacesProject({
87-
mode: 'independent'
85+
await new Releaser({
86+
project: new PnpmWorkspacesProject({
87+
mode: 'independent'
88+
}),
89+
hosting: new GithubHosting({
90+
token: process.env.GITHUB_TOKEN
91+
})
8892
})
89-
90-
await new Releaser(project)
9193
.bump()
9294
.commit()
9395
.tag()
@@ -99,9 +101,28 @@ await new Releaser(project)
99101
.run()
100102
```
101103

102-
## Why
103-
104-
There no good tool to release projects with conventional-changelog support and with good monorepo support.
104+
### Options
105+
106+
| Option | Description |
107+
| --- | --- |
108+
| `project` | Project instance. |
109+
| `hosting` | Git repository hosting instance. Optional. |
110+
| `dryRun` | If true, do not write files, just change the version in memory. |
111+
| `verbose` | If true, log more information to the console. |
112+
| `silent` | If true, do not log anything to the console. |
113+
114+
### Available steps
115+
116+
| Step | Description |
117+
| --- | --- |
118+
| checkout | Checkout the desired branch. |
119+
| bump | Bump the version of the project. |
120+
| commit | Commit the changes with the new version. |
121+
| tag | Tag the commit with the new version. |
122+
| push | Push the changes to the remote repository. |
123+
| release | Create a release in the remote repository. |
124+
| publish | Publish the project to the package registry. |
125+
| pullRequest | Create a pull request with the changes. |
105126

106127
## Addons
107128

@@ -178,20 +199,25 @@ export class CustomProject extends Project {
178199

179200
There also is a base class for monorepo projects - [MonorepoProject](./src/project/monorepo.ts). It provides methods to work with monorepo projects and you can extend it to create your own monorepo project class (alos see [PackageJsonMonorepoProject](./src/project/packageJsonMonorepo.ts)).
180201

181-
### Release creator
202+
### GitRepositoryHosting
182203

183-
Release creator is a class that creates a release in the remote repository (like GitHub, GitLab, etc.) or wherever you want. It is used in the `release` step of the releaser.
184-
185-
Signature of the class is very simple, you just need to implement `create` method:
204+
GitRepositoryHosting is a class that represents a git repository hosting service (like GitHub, GitLab, etc.) or whatever you want. It is used to create a release in the remote repository and create a pull request with the changes.
186205

187206
```js
188-
import { ReleaseCreator } from '@simple-release/core'
207+
import { GitRepositoryHosting } from '@simple-release/core'
189208

190-
export class CustomReleaseCreator extends ReleaseCreator {
191-
async create({ project, dryRun, logger }) {
209+
export class MyRepositoryHosting extends GitRepositoryHosting {
210+
async createRelease({ project, dryRun, logger }) {
192211
// Create the release in the remote repository
193212
// You can use `project` to get information about the project
194-
// or more precisely you can use `project.getgetReleaseData()` to get the data for the release
213+
// or more precisely you can use `project.getReleaseData()` to get the data for the release
214+
}
215+
216+
async createPullRequest({ from, to, project, dryRun, logger }) {
217+
// Create a pull request with the changes
218+
// You can use `project` to get information about the project
195219
}
196220
}
197221
```
222+
223+
For more detailed example you can look at the [GithubHosting](../github/src/index.ts) implementation.

packages/core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@
5858
"test": "run -p lint test:unit test:types"
5959
},
6060
"dependencies": {
61-
"@conventional-changelog/git-client": "^2.4.0",
61+
"@conventional-changelog/git-client": "^2.5.1",
6262
"@simple-libs/child-process-utils": "^1.0.0",
63+
"@simple-libs/hosted-git-info": "^1.0.0",
6364
"@simple-libs/stream-utils": "^1.0.0",
6465
"conventional-changelog": "^7.0.2",
6566
"conventional-changelog-conventionalcommits": "^9.0.0",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type {
2+
CreateReleaseOptions,
3+
CreatePullRequestOptions
4+
} from './hosting.types.js'
5+
6+
export * from './hosting.types.js'
7+
8+
/**
9+
* A base class that represents a git repository hosting service.
10+
*/
11+
export abstract class GitRepositoryHosting {
12+
/**
13+
* Creates a new release for the given project.
14+
* @param options
15+
*/
16+
abstract createRelease(options: CreateReleaseOptions): Promise<void>
17+
18+
/**
19+
* Creates a pull request for the given project.
20+
* @param options
21+
*/
22+
abstract createPullRequest(options: CreatePullRequestOptions): Promise<void>
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { Project } from '../project/project.js'
2+
import type { ChildLogger } from '../logger.js'
3+
4+
export interface ReleaseData {
5+
version: string
6+
title: string
7+
notes: string
8+
previousTag: string
9+
nextTag: string
10+
isPrerelease: boolean
11+
}
12+
13+
export interface CreateReleaseOptions {
14+
project: Project
15+
dryRun?: boolean
16+
logger?: ChildLogger
17+
}
18+
19+
export interface CreatePullRequestOptions {
20+
project: Project
21+
/**
22+
* Base branch for the pull request.
23+
*/
24+
to?: string
25+
/**
26+
* Head branch for the pull request.
27+
*/
28+
from?: string
29+
dryRun?: boolean
30+
logger?: ChildLogger
31+
}

packages/core/src/hosting/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './hosting.js'

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export * from './manifest/index.js'
22
export * from './project/index.js'
3-
export * from './release/index.js'
3+
export * from './hosting/index.js'
44
export * from './logger.js'
55
export * from './releaser.js'

packages/core/src/logger.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface LoggerOptions {
1212
export interface LoggerMessage {
1313
dryRun?: boolean
1414
stage: string
15-
message: string
15+
message: unknown
1616
}
1717

1818
function defaultPrinter({
@@ -21,7 +21,7 @@ function defaultPrinter({
2121
message
2222
}: LoggerMessage) {
2323
// eslint-disable-next-line no-console
24-
console.log(`${dryRun ? `[dry-run]` : ''}[${stage}]: ${message}`)
24+
console.log(`${dryRun ? `[dry-run]` : ''}[${stage}]:`, message)
2525
}
2626

2727
/**
@@ -42,15 +42,15 @@ export class ChildLogger {
4242
* Logs an info message.
4343
* @param message - The message to log.
4444
*/
45-
info(message: string) {
45+
info(message: unknown) {
4646
this.parent.info(this.stage, message)
4747
}
4848

4949
/**
5050
* Logs a verbose message.
5151
* @param message - The message to log.
5252
*/
53-
verbose(message: string) {
53+
verbose(message: unknown) {
5454
this.parent.verbose(this.stage, message)
5555
}
5656
}
@@ -78,7 +78,7 @@ export class Logger {
7878
* @param stage - The stage of the process.
7979
* @param message - The message to log.
8080
*/
81-
info(stage: string, message: string) {
81+
info(stage: string, message: unknown) {
8282
const {
8383
silent,
8484
dryRun
@@ -98,7 +98,7 @@ export class Logger {
9898
* @param stage - The stage of the process.
9999
* @param message - The message to log.
100100
*/
101-
verbose(stage: string, message: string) {
101+
verbose(stage: string, message: unknown) {
102102
const {
103103
silent,
104104
verbose,

packages/core/src/manifest/manifest.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import { dirname } from 'path'
22
import semver from 'semver'
3+
import type { ProjectManifestVersionUpdate } from './manifest.types.js'
34

4-
export interface ProjectManifestVersionUpdate {
5-
name: string
6-
from: string
7-
to: string
8-
files: string[]
9-
}
5+
export * from './manifest.types.js'
106

117
/**
128
* A base class for project manifests.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface ProjectManifestVersionUpdate {
2+
name: string
3+
from: string
4+
to: string
5+
files: string[]
6+
}

packages/core/src/manifest/packageJson.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@ import {
33
type ProjectManifestVersionUpdate,
44
ProjectManifest
55
} from './manifest.js'
6+
import type { PackageJsonProps } from './packageJson.types.js'
67

7-
export interface PackageJsonKnownProps {
8-
name: string
9-
version: string
10-
private?: boolean
11-
}
12-
13-
export type PackageJsonProps = PackageJsonKnownProps & Record<string, unknown>
8+
export * from './packageJson.types.js'
149

1510
/**
1611
* A class that represents a package.json manifest.

0 commit comments

Comments
 (0)