Skip to content

Commit

Permalink
feat: cli support changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
3lang3 committed Nov 3, 2021
1 parent 242c446 commit d281cf5
Show file tree
Hide file tree
Showing 16 changed files with 321 additions and 17 deletions.
1 change: 0 additions & 1 deletion .github/workflows/deploy-site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ on:
branches: [main]
paths:
- 'packages/react-vant/docs/**'
- 'packages/react-vant/src/**'

jobs:
build-and-deploy:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Create Release Tag
on:
push:
tags:
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
- 'react-vant@*' # Push events to matching v*, i.e. v1.0, v20.15.10

jobs:
build:
Expand All @@ -20,4 +20,4 @@ jobs:
with:
tag_name: ${{ github.ref }}
body: |
更新内容参见 [CHANGELOG](https://3lang3.github.io/react-vant/#/zh-CN/)。
更新内容参见 [CHANGELOG](https://3lang3.github.io/react-vant/#/zh-CN/changelog)。
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ es
lib
site
dist
changelog.generated.md

# log file
.changelog
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*hbs
1 change: 1 addition & 0 deletions packages/react-vant-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"clean-css": "^4.2.3",
"commander": "^6.1.0",
"consola": "^2.15.0",
"conventional-changelog": "^3.1.24",
"css-loader": "^4.3.0",
"enzyme": "^3.11.0",
"enzyme-adapter-react-16": "^1.15.5",
Expand Down
3 changes: 3 additions & 0 deletions packages/react-vant-cli/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
clean,
build,
release,
changelog,
buildSite,
commitLint,
cliVersion,
Expand Down Expand Up @@ -55,6 +56,8 @@ program

program.command('build-site').description('Compile site in production mode').action(buildSite);

program.command('changelog').description('Generate changelog').action(changelog);

program.command('commit-lint').description('Lint commit message').action(commitLint);

program
Expand Down
70 changes: 70 additions & 0 deletions packages/react-vant-cli/src/commands/changelog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { join } from 'path';
import conventionalChangelog from 'conventional-changelog';
import { createWriteStream, readFileSync } from 'fs-extra';
import { ROOT } from '../common/constant';
import { ora, slimPath } from '../common/logger';

const DIST_FILE = join(ROOT, './changelog.generated.md');
const MAIN_TEMPLATE = join(__dirname, '../../template/changelog-main.hbs');
const HEADER_TEMPLATE = join(__dirname, '../../template/changelog-header.hbs');
const COMMIT_TEMPLATE = join(__dirname, '../../template/changelog-commit.hbs');

const mainTemplate = readFileSync(MAIN_TEMPLATE, 'utf-8');
const headerPartial = readFileSync(HEADER_TEMPLATE, 'utf-8');
const commitPartial = readFileSync(COMMIT_TEMPLATE, 'utf-8');

function formatType(type: string) {
const MAP: Record<string, string> = {
fix: 'Bug Fixes',
feat: 'Feature',
docs: 'Document',
types: 'Types',
};

return MAP[type] || type;
}

function transform(item: any) {
if (item.type === 'chore' || item.type === 'test') {
return null;
}

item.type = formatType(item.type);

if (item.hash) {
item.shortHash = item.hash.slice(0, 6);
}

if (item.references.length) {
item.references.forEach((ref: any) => {
if (ref.issue && item.subject) {
item.subject = item.subject.replace(` (#${ref.issue})`, '');
}
});
}
return item;
}

export async function changelog(): Promise<void> {
const spinner = ora('Generating changelog...').start();

return new Promise((resolve) => {
conventionalChangelog(
{
preset: 'angular',
releaseCount: 2,
},
{
mainTemplate,
headerPartial,
commitPartial,
transform,
},
)
.pipe(createWriteStream(DIST_FILE))
.on('close', () => {
spinner.succeed(`Changelog generated at ${slimPath(DIST_FILE)}`);
resolve();
});
});
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/* eslint-disable class-methods-use-this */
import releaseIt from 'release-it';
import { build } from '../commands/build';
import { changelog } from '../commands/changelog';

class VantCliReleasePlugin extends releaseIt.Plugin {
async beforeRelease() {
// log an empty line
console.log('');

await build();
await changelog();
}
}

Expand Down
15 changes: 3 additions & 12 deletions packages/react-vant-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,13 @@ import { test } from './commands/jest';
import { lint } from './commands/lint';
import { clean } from './commands/clean';
import { release } from './commands/release';
import { changelog } from './commands/changelog';
import { buildSite } from './commands/build-site';
import { commitLint } from './commands/commit-lint';
import { create } from "./commands/create";
import { create } from './commands/create';

export const cliVersion: string = packageJson.version;

process.env.REACT_VANT_CLI_VERSION = cliVersion;

export {
dev,
create,
lint,
test,
clean,
build,
release,
buildSite,
commitLint,
};
export { dev, create, lint, test, clean, build, release, changelog, buildSite, commitLint };
14 changes: 14 additions & 0 deletions packages/react-vant-cli/template/changelog-commit.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-{{#if scope}}
{{scope}}:
{{~/if}}
{{#if subject}}
{{~subject}}
{{~else}}
{{~header}}
{{~/if}}
{{#if references~}}
{{~#each references}}
[{{~this.repository}}#{{this.issue}}]({{~@root.repoUrl}}/{{~@root.issue}}/{{this.issue}}){{/each}}
{{~else}}
[{{shortHash}}]({{~@root.repoUrl}}/{{~@root.commit}}/{{hash}})
{{~/if}}
1 change: 1 addition & 0 deletions packages/react-vant-cli/template/changelog-header.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
### [v{{version}}]({{~@root.repoUrl}}/compare/{{previousTag}}...{{currentTag}}) `{{date}}`
12 changes: 12 additions & 0 deletions packages/react-vant-cli/template/changelog-main.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{> header}}
{{#each commitGroups}}

{{#if title}}
**{{title}}**

{{/if}}
{{#each commits}}
{{> commit root=@root}}
{{/each}}
{{/each}}
{{> footer}}
Loading

0 comments on commit d281cf5

Please sign in to comment.