Skip to content

Commit

Permalink
feat(release): implement CI pipeline for automated npm package publis…
Browse files Browse the repository at this point in the history
…hing (#3)

* feat(release): implement CI pipeline for automated npm package publishing

* docs: add publish & --skip-npm-publish
  • Loading branch information
a145789 authored Jan 22, 2024
1 parent 9e628c1 commit b038c0f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ npx vr changelog --file changelog.md

# Lint commit message
npx vr lint-commit -p .git/COMMIT_EDITMSG

# Publish to npm, which can be called in the ci environment
npx vr publish
```

### Configuration
Expand All @@ -71,6 +74,7 @@ npx vr lint-commit -p .git/COMMIT_EDITMSG
| Params | Instructions |
| ----------------------------------- | -------------------------- |
| -f --file \<filename\> | Specify changelog filename |
| -s --skip-npm-publish | Skip npm publish |
| -rc --releaseCount \<releaseCount\> | Release count |

#### lint-commit
Expand All @@ -82,6 +86,12 @@ npx vr lint-commit -p .git/COMMIT_EDITMSG
| -e --errorMessage \<message\> | Validation failed to display error messages |
| -w --warningMessage \<message\> | Validation failed to display warning messages |

#### publish

```shell
vr publish
```

### Custom Handle

#### Example
Expand Down Expand Up @@ -109,8 +119,11 @@ release({ task })
#### Types

```ts
function publish(preRelease: boolean | undefined): Promise<void>
function updateVersion(version: string): void
interface ReleaseCommandOptions {
remote?: string
skipNpmPublish?: boolean
task?(): Promise<void>
}
function release(options: ReleaseCommandOptions): Promise<void>
Expand All @@ -121,6 +134,9 @@ interface ChangelogCommandOptions {
}
function changelog({ releaseCount, file }?: ChangelogCommandOptions): Promise<void>

const COMMIT_MESSAGE_RE: RegExp
function isVersionCommitMessage(message: string): string | false | null
function getCommitMessage(commitMessagePath: string): string
interface CommitLintCommandOptions {
commitMessagePath: string
commitMessageRe?: string | RegExp
Expand Down
16 changes: 16 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ npx vr changelog --file changelog.md

# 检测 commit message
npx vr lint-commit -p .git/COMMIT_EDITMSG

# 发布到 npm,可以在 ci 中执行
npx vr publish
```

### 配置
Expand All @@ -71,6 +74,7 @@ npx vr lint-commit -p .git/COMMIT_EDITMSG
| 参数 | 说明 |
| ----------------------------------- | ------------------ |
| -f --file \<filename\> | 指定变更日志文件名 |
| -s --skip-npm-publish | 跳过 npm 发布 |
| -rc --releaseCount \<releaseCount\> | 发布数量 |

#### lint-commit
Expand All @@ -82,6 +86,12 @@ npx vr lint-commit -p .git/COMMIT_EDITMSG
| -e --errorMessage \<message\> | 验证失败展示的错误信息 |
| -w --warningMessage \<message\> | 验证失败展示的提示信息 |

#### publish

```shell
vr publish
```

### 自定义处理

#### 示例
Expand Down Expand Up @@ -109,8 +119,11 @@ release({ task })
#### 类型

```ts
function publish(preRelease: boolean | undefined): Promise<void>
function updateVersion(version: string): void
interface ReleaseCommandOptions {
remote?: string
skipNpmPublish?: boolean
task?(): Promise<void>
}
function release(options: ReleaseCommandOptions): Promise<void>
Expand All @@ -121,6 +134,9 @@ interface ChangelogCommandOptions {
}
function changelog({ releaseCount, file }?: ChangelogCommandOptions): Promise<void>

const COMMIT_MESSAGE_RE: RegExp
function isVersionCommitMessage(message: string): string | false | null
function getCommitMessage(commitMessagePath: string): string
interface CommitLintCommandOptions {
commitMessagePath: string
commitMessageRe?: string | RegExp
Expand Down
8 changes: 7 additions & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#!/usr/bin/env node
import { release, changelog, commitLint } from '../dist/index.js'
import { release, publish, changelog, commitLint } from '../dist/index.js'
import { Command } from 'commander'

const program = new Command()

program
.command('release')
.option('-r --remote <remote>', 'Remote name')
.option('-s --skip-npm-publish', 'Skip npm publish')
.description('Release all packages and generate changelogs')
.action(async (options) => release(options))

program
.command('publish')
.description('Publish to npm')
.action(async () => publish())

program
.command('changelog')
.option('-rc --releaseCount <releaseCount>', 'Release count')
Expand Down
17 changes: 12 additions & 5 deletions src/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function isWorktreeEmpty() {
return !ret.stdout
}

async function publish(preRelease: boolean) {
export async function publish(preRelease: boolean | undefined) {
const s = createSpinner('Publishing all packages').start()
const args = ['-r', 'publish', '--no-git-checks', '--access', 'public']

Expand Down Expand Up @@ -48,7 +48,7 @@ async function pushGit(version: string, remote = 'origin') {
ret.stdout && logger.info(ret.stdout)
}

function updateVersion(version: string) {
export function updateVersion(version: string) {
const packageJsons = glob.sync('packages/*/package.json')
packageJsons.push('package.json')

Expand Down Expand Up @@ -121,6 +121,7 @@ async function getReleaseType() {

export interface ReleaseCommandOptions {
remote?: string
skipNpmPublish?: boolean
task?(): Promise<void>
}

Expand Down Expand Up @@ -161,7 +162,9 @@ export async function release(options: ReleaseCommandOptions) {
await options.task()
}

await publish(isPreRelease)
if (!options.skipNpmPublish) {
await publish(isPreRelease)
}

if (!isPreRelease) {
await changelog()
Expand All @@ -173,11 +176,15 @@ export async function release(options: ReleaseCommandOptions) {
if (isPreRelease) {
try {
await execa('git', ['restore', '**/package.json'])
} catch { /* empty */ }
} catch {
/* empty */
}

try {
await execa('git', ['restore', 'package.json'])
} catch { /* empty */ }
} catch {
/* empty */
}
}
} catch (error: any) {
logger.error(error.toString())
Expand Down

0 comments on commit b038c0f

Please sign in to comment.