Skip to content

Commit 88eab2d

Browse files
authored
feat: Check if the npm package has a remote version (#4)
* feat: Check if the npm package has a remote version * refactor: Only detect one remote package * refactor: optimize tips
1 parent c0bb136 commit 88eab2d

File tree

4 files changed

+65
-24
lines changed

4 files changed

+65
-24
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ npx vr publish
6868
| Params | Instructions |
6969
| ---------------------- | ------------------- |
7070
| -r --remote \<remote\> | Specify remote name |
71-
| -s --skip-npm-publish | Skip npm publish |
72-
| -sgt --skip-git-tag | Skip git tag |
71+
| -s --skip-npm-publish | Skip npm publish |
72+
| -sgt --skip-git-tag | Skip git tag |
7373

7474
#### changelog
7575

@@ -89,9 +89,9 @@ npx vr publish
8989

9090
#### publish
9191

92-
```shell
93-
vr publish
94-
```
92+
| 参数 | 说明 |
93+
| ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
94+
| -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 |
9595

9696
### Custom Handle
9797

@@ -120,7 +120,11 @@ release({ task })
120120
#### Types
121121

122122
```ts
123-
function publish(preRelease: boolean | undefined): Promise<void>
123+
interface PublishCommandOptions {
124+
preRelease?: boolean
125+
checkRemoteVersion?: boolean
126+
}
127+
function publish({ preRelease, checkRemoteVersion }: PublishCommandOptions): Promise<void>
124128
function updateVersion(version: string): void
125129
interface ReleaseCommandOptions {
126130
remote?: string

README.zh-CN.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ npx vr publish
6868
| 参数 | 说明 |
6969
| ---------------------- | ---------------- |
7070
| -r --remote \<remote\> | 指定远程仓库名称 |
71-
| -s --skip-npm-publish | 跳过 npm 发布 |
72-
| -sgt --skip-git-tag | 跳过 git tag |
71+
| -s --skip-npm-publish | 跳过 npm 发布 |
72+
| -sgt --skip-git-tag | 跳过 git tag |
7373

7474
#### changelog
7575

@@ -89,9 +89,9 @@ npx vr publish
8989

9090
#### publish
9191

92-
```shell
93-
vr publish
94-
```
92+
| 参数 | 说明 |
93+
| ------------------------- | --------------------------------------------------------------------- |
94+
| -c --check-remote-version | 检测npm包的远程版本是否与要在本地发布的包版本相同,如果是,则跳过发布 |
9595

9696
### 自定义处理
9797

@@ -120,7 +120,11 @@ release({ task })
120120
#### 类型
121121

122122
```ts
123-
function publish(preRelease: boolean | undefined): Promise<void>
123+
interface PublishCommandOptions {
124+
preRelease?: boolean
125+
checkRemoteVersion?: boolean
126+
}
127+
function publish({ preRelease, checkRemoteVersion }: PublishCommandOptions): Promise<void>
124128
function updateVersion(version: string): void
125129
interface ReleaseCommandOptions {
126130
remote?: string

bin/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ program
1414

1515
program
1616
.command('publish')
17+
.option('-c --check-remote-version', 'Check remote version')
1718
.description('Publish to npm')
18-
.action(async () => publish())
19+
.action(async (options) => publish(options))
1920

2021
program
2122
.command('changelog')

src/release.ts

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fse from 'fs-extra'
22
import logger from './logger'
3-
import semver from 'semver'
3+
import semver, { type ReleaseType } from 'semver'
44
import inquirer from 'inquirer'
55
import { execa } from 'execa'
66
import { createSpinner } from 'nanospinner'
@@ -19,10 +19,30 @@ async function isWorktreeEmpty() {
1919
return !ret.stdout
2020
}
2121

22-
export async function publish(preRelease: boolean | undefined) {
22+
interface PublishCommandOptions {
23+
preRelease?: boolean
24+
checkRemoteVersion?: boolean
25+
}
26+
export async function publish({ preRelease, checkRemoteVersion }: PublishCommandOptions) {
2327
const s = createSpinner('Publishing all packages').start()
2428
const args = ['-r', 'publish', '--no-git-checks', '--access', 'public']
2529

30+
if (checkRemoteVersion) {
31+
const packageJson = getPackageJsons().find((packageJson) => !packageJson.config.private)
32+
if (packageJson) {
33+
const { config } = packageJson
34+
try {
35+
await execa('npm', ['view', `${config.name}@${config.version}`, 'version'])
36+
s.warn({
37+
text: `The npm package has a same remote version ${config.version}, publishing automatically skipped.`,
38+
})
39+
return
40+
} catch {
41+
/* empty */
42+
}
43+
}
44+
}
45+
2646
if (preRelease) {
2747
args.push('--tag', 'alpha')
2848
}
@@ -51,16 +71,28 @@ async function pushGit(version: string, remote = 'origin', skipGitTag = false) {
5171
ret.stdout && logger.info(ret.stdout)
5272
}
5373

54-
export function updateVersion(version: string) {
55-
const packageJsons = glob.sync('packages/*/package.json')
56-
packageJsons.push('package.json')
74+
function getPackageJsons() {
75+
const packageJsons = ['package.json', ...glob.sync('packages/*/package.json')]
76+
77+
return packageJsons.map((path) => {
78+
const filePath = resolve(cwd, path)
79+
return {
80+
config: readJSONSync(filePath) as {
81+
name: string
82+
version: string
83+
private: boolean
84+
},
85+
filePath,
86+
}
87+
})
88+
}
5789

58-
packageJsons.forEach((path: string) => {
59-
const file = resolve(cwd, path)
60-
const config = readJSONSync(file)
90+
export function updateVersion(version: string) {
91+
const packageJsons = getPackageJsons()
6192

93+
packageJsons.forEach(({ config, filePath }) => {
6294
config.version = version
63-
writeFileSync(file, JSON.stringify(config, null, 2))
95+
writeFileSync(filePath, JSON.stringify(config, null, 2))
6496
})
6597
}
6698

@@ -109,7 +141,7 @@ async function confirmRefs(remote = 'origin') {
109141
return ret[name]
110142
}
111143

112-
async function getReleaseType() {
144+
async function getReleaseType(): Promise<ReleaseType> {
113145
const name = 'Please select release type'
114146
const ret = await prompt([
115147
{
@@ -167,7 +199,7 @@ export async function release(options: ReleaseCommandOptions) {
167199
}
168200

169201
if (!options.skipNpmPublish) {
170-
await publish(isPreRelease)
202+
await publish({ preRelease: isPreRelease })
171203
}
172204

173205
if (!isPreRelease) {

0 commit comments

Comments
 (0)