Skip to content

Commit 9136d75

Browse files
committed
feat: add remote version check to release
1 parent f85319a commit 9136d75

File tree

5 files changed

+54
-63
lines changed

5 files changed

+54
-63
lines changed

README.md

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,10 @@
1111

1212
## Intro
1313

14-
`Varlet Release` is a tool to release all packages, generate changelogs and lint commit message.
14+
`Varlet Release` is a tool used for publishing all packages, generating change logs, and checking `commit messages`, relying on `pnpm`.
1515

1616
## Installation
1717

18-
### npm
19-
20-
```shell
21-
npm i @varlet/release -D
22-
```
23-
24-
### yarn
25-
26-
```shell
27-
yarn add @varlet/release -D
28-
```
29-
30-
### pnpm
31-
3218
```shell
3319
pnpm add @varlet/release -D
3420
```
@@ -65,13 +51,14 @@ npx vr publish
6551

6652
#### release
6753

68-
| Params | Instructions |
69-
| ---------------------- | ------------------- |
70-
| -r --remote \<remote\> | Specify remote name |
71-
| -s --skip-npm-publish | Skip npm publish |
72-
| -sc --skip-changelog | Skip generate changelog |
73-
| -sgt --skip-git-tag | Skip git tag |
74-
| -nt --npm-tag \<npmTag\> | npm tag |
54+
| Params | Instructions |
55+
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
56+
| -r --remote \<remote\> | Specify remote name |
57+
| -s --skip-npm-publish | Skip npm publish |
58+
| -c --check-remote-version | Check if the remote version of the npm package is the same as the one you want to publish locally, if so, stop execution. |
59+
| -sc --skip-changelog | Skip generate changelog |
60+
| -sgt --skip-git-tag | Skip git tag |
61+
| -nt --npm-tag \<npmTag\> | npm tag |
7562

7663
#### changelog
7764

@@ -91,10 +78,10 @@ npx vr publish
9178

9279
#### publish
9380

94-
| Params | Instructions |
81+
| Params | Instructions |
9582
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
9683
| -c --check-remote-version | Detects whether the remote version of the npm package is the same as the package version to be published locally, and if it is, skip the release |
97-
| -nt --npm-tag \<npmTag\> | npm tag |
84+
| -nt --npm-tag \<npmTag\> | npm tag |
9885

9986
### Custom Handle
10087

README.zh-CN.md

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,10 @@
1111

1212
## 介绍
1313

14-
`Varlet Release` 是一个用于发布所有包、生成变更日志和检测 `commit message` 的工具。
14+
`Varlet Release` 是一个用于发布所有包、生成变更日志和检测 `commit message` 的工具,依赖于 `pnpm`
1515

1616
## 安装
1717

18-
### npm
19-
20-
```shell
21-
npm i @varlet/release -D
22-
```
23-
24-
### yarn
25-
26-
```shell
27-
yarn add @varlet/release -D
28-
```
29-
30-
### pnpm
31-
3218
```shell
3319
pnpm add @varlet/release -D
3420
```
@@ -65,13 +51,14 @@ npx vr publish
6551

6652
#### release
6753

68-
| 参数 | 说明 |
69-
| ---------------------- | ---------------- |
70-
| -r --remote \<remote\> | 指定远程仓库名称 |
71-
| -s --skip-npm-publish | 跳过 npm 发布 |
72-
| -sc --skip-changelog | 跳过生成变更日志 |
73-
| -sgt --skip-git-tag | 跳过 git tag |
74-
| -nt --npm-tag \<npmTag\> | npm tag |
54+
| 参数 | 说明 |
55+
| ------------------------- | ----------------------------------------------------------------------- |
56+
| -r --remote \<remote\> | 指定远程仓库名称 |
57+
| -s --skip-npm-publish | 跳过 npm 发布 |
58+
| -c --check-remote-version | 检测 npm 包的远程版本是否与要在本地发布的包版本相同,如果是,则停止执行 |
59+
| -sc --skip-changelog | 跳过生成变更日志 |
60+
| -sgt --skip-git-tag | 跳过 git tag |
61+
| -nt --npm-tag \<npmTag\> | npm tag |
7562

7663
#### changelog
7764

@@ -94,7 +81,7 @@ npx vr publish
9481
| 参数 | 说明 |
9582
| ------------------------- | --------------------------------------------------------------------- |
9683
| -c --check-remote-version | 检测npm包的远程版本是否与要在本地发布的包版本相同,如果是,则跳过发布 |
97-
| -nt --npm-tag \<npmTag\> | npm tag |
84+
| -nt --npm-tag \<npmTag\> | npm tag |
9885

9986
### 自定义处理
10087

bin/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ program
1111
.option('-sc --skip-changelog', 'Skip generate changelog')
1212
.option('-sgt --skip-git-tag', 'Skip git tag')
1313
.option('-nt --npm-tag <npmTag>', 'Npm tag')
14+
.option('-c --check-remote-version', 'Check remote version')
1415
.description('Release all packages and generate changelogs')
1516
.action(async (options) => release(options))
1617

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"scripts": {
3535
"dev": "tsup --watch",
3636
"build": "tsup",
37-
"release": "pnpm build && node bin/index.js release",
37+
"release": "pnpm build && node bin/index.js release -c",
3838
"lint": "eslint --ext .ts,.js",
3939
"commit-lint": "node bin/index.js commit-lint"
4040
},
@@ -76,4 +76,4 @@
7676
"picocolors": "^1.0.0",
7777
"semver": "^7.5.4"
7878
}
79-
}
79+
}

src/release.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,27 @@ async function isWorktreeEmpty() {
2121
return !ret.stdout
2222
}
2323

24+
export async function isSameVersion(version?: string) {
25+
const s = createSpinner('Check remote version...').start()
26+
27+
const packageJsones = getPackageJsons()
28+
const packageJson = packageJsones.find((packageJson) => !packageJson.config.private) || packageJsones[0]
29+
if (packageJson) {
30+
const { config } = packageJson
31+
try {
32+
await execa('npm', ['view', `${config.name}@${version ?? config.version}`, 'version'])
33+
34+
s.warn({
35+
text: `The npm package has a same remote version ${config.version}.`,
36+
})
37+
return true
38+
} catch {
39+
s.success()
40+
return false
41+
}
42+
}
43+
}
44+
2445
export interface PublishCommandOptions {
2546
preRelease?: boolean
2647
checkRemoteVersion?: boolean
@@ -31,20 +52,9 @@ export async function publish({ preRelease, checkRemoteVersion, npmTag }: Publis
3152
const s = createSpinner('Publishing all packages').start()
3253
const args = ['-r', 'publish', '--no-git-checks', '--access', 'public']
3354

34-
if (checkRemoteVersion) {
35-
const packageJson = getPackageJsons().find((packageJson) => !packageJson.config.private)
36-
if (packageJson) {
37-
const { config } = packageJson
38-
try {
39-
await execa('npm', ['view', `${config.name}@${config.version}`, 'version'])
40-
s.warn({
41-
text: `The npm package has a same remote version ${config.version}, publishing automatically skipped.`,
42-
})
43-
return
44-
} catch {
45-
/* empty */
46-
}
47-
}
55+
if (checkRemoteVersion && (await isSameVersion())) {
56+
logger.error('publishing automatically skipped.')
57+
return
4858
}
4959

5060
if (preRelease) {
@@ -181,6 +191,7 @@ export interface ReleaseCommandOptions {
181191
skipNpmPublish?: boolean
182192
skipChangelog?: boolean
183193
skipGitTag?: boolean
194+
checkRemoteVersion?: boolean
184195
task?(): Promise<void>
185196
}
186197

@@ -208,6 +219,11 @@ export async function release(options: ReleaseCommandOptions) {
208219

209220
const { isPreRelease, expectVersion } = await getReleaseVersion(currentVersion)
210221

222+
if (options.checkRemoteVersion && (await isSameVersion(expectVersion))) {
223+
logger.error('Please check remote version.')
224+
return
225+
}
226+
211227
updateVersion(expectVersion)
212228

213229
if (options.task) {

0 commit comments

Comments
 (0)