-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate and upgrade version middleware
- Loading branch information
longgui.wjb
committed
Apr 14, 2022
1 parent
9513170
commit 60dd2ff
Showing
7 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Change Log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# @dawnjs/dn-middleware-version | ||
|
||
[![npm](https://img.shields.io/npm/v/@dawnjs/dn-middleware-version)](https://www.npmjs.com/package/@dawnjs/dn-middleware-version) | ||
[![npm](https://img.shields.io/npm/dw/@dawnjs/dn-middleware-version)](https://www.npmjs.com/package/@dawnjs/dn-middleware-version) | ||
|
||
## Usage | ||
|
||
```yml | ||
version: | ||
- name: '@dawnjs/dn-middleware-version' | ||
``` | ||
|
||
## Options | ||
|
||
| Name | Type | Default | Description | | ||
| --------------- | --------- | ------- | ------------------------- | | ||
| noGitTagVersion | `boolean` | | 是否不提交 Git | | ||
| skitCommitHooks | `boolean` | | 是否运行 git commit hooks | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "@dawnjs/dn-middleware-version", | ||
"version": "1.0.4", | ||
"description": "A middleware to bump package version", | ||
"main": "./lib/index.js", | ||
"scripts": { | ||
"clean": "rimraf lib", | ||
"prebuild": "npm run clean", | ||
"build": "tsc && ts-add-module-exports", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
"files": [ | ||
"lib" | ||
], | ||
"author": "", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"homepage": "http://dawnjs.com", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/alibaba/dawn.git" | ||
}, | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@dawnjs/types": "^2.0.1", | ||
"@types/semver": "^7.3.9" | ||
}, | ||
"dependencies": { | ||
"semver": "^7.3.7" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import semver from "semver"; | ||
import type { Handler } from "@dawnjs/types"; | ||
import type { IOpts } from "./types"; | ||
|
||
const handler: Handler<IOpts> = opts => { | ||
return async (next, ctx) => { | ||
const envOpts = JSON.parse(decodeURIComponent(process.env.DN_ARGV || "{}")); | ||
const versionEnv = envOpts.version || {}; | ||
const silenceMode = !!versionEnv.silence; | ||
|
||
const currentVersion = ctx.project.version; | ||
|
||
const questions = [ | ||
{ | ||
name: "version", | ||
type: "list", | ||
message: "请确认将要发布的版本", | ||
default: currentVersion, | ||
choices: [ | ||
{ name: `使用当前版本(${currentVersion})`, value: currentVersion }, | ||
{ name: `新的修订版本(${semver.inc(currentVersion, "patch")})`, value: "patch" }, | ||
{ name: `新的次要版本(${semver.inc(currentVersion, "minor")})`, value: "minor" }, | ||
{ name: `新的主要版本(${semver.inc(currentVersion, "major")})`, value: "major" }, | ||
{ name: `新的预发行版本(${semver.inc(currentVersion, "prerelease")})`, value: "prerelease" }, | ||
{ name: `新的预发行修订版本(${semver.inc(currentVersion, "prepatch")})`, value: "prepatch" }, | ||
{ name: `新的预发行次要版本(${semver.inc(currentVersion, "preminor")})`, value: "preminor" }, | ||
{ name: `新的预发行主要版本(${semver.inc(currentVersion, "premajor")})`, value: "premajor" }, | ||
new ctx.inquirer.Separator(), | ||
{ name: "手动输入版本", value: "input" }, | ||
], | ||
}, | ||
{ | ||
name: "inputVersion", | ||
type: "input", | ||
message: "请输入将要发布的版本", | ||
when({ version }) { | ||
return version === "input"; | ||
}, | ||
filter(input) { | ||
return input?.trim(); | ||
}, | ||
validate(input) { | ||
if (!semver.valid(input)) { | ||
return "新版本号不合法"; | ||
} | ||
if (semver.lt(input, currentVersion)) { | ||
return "新版本号不能小于当前版本号"; | ||
} | ||
return true; | ||
}, | ||
}, | ||
{ | ||
name: "preid", | ||
type: "input", | ||
message: "请输入预发行版本前缀", | ||
default: "alpha", | ||
validate(input) { | ||
if (!/^[a-z0-9]*$/i.test(input)) { | ||
return "预发行版本前缀只能包含英文字母和数字"; | ||
} | ||
return true; | ||
}, | ||
}, | ||
]; | ||
|
||
let answers: { version?: string; inputVersion?: string; preid?: string } = {}; | ||
if (silenceMode) { | ||
ctx.console.info("静默模式...", JSON.stringify(versionEnv)); | ||
questions.forEach(({ name, default: defaultValue }) => { | ||
answers[name] = versionEnv[name] || defaultValue; | ||
}); | ||
} else { | ||
answers = await ctx.inquirer.prompt(questions); | ||
} | ||
const command = [`npm version ${answers.inputVersion || answers.version}`, "--allow-same-version"]; | ||
if (opts.noGitTagVersion) { | ||
command.push("--no-git-tag-version"); | ||
} | ||
if (opts.skitCommitHooks) { | ||
command.push("--no-commit-hooks"); | ||
} | ||
if (answers.preid) { | ||
command.push(`--preid=${answers.preid}`); | ||
} | ||
await ctx.utils.exec(command.join(" ")); | ||
ctx.console.warn("版本已更新为:", ctx.project.version); | ||
next(); | ||
}; | ||
}; | ||
|
||
export default handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface IOpts { | ||
noGitTagVersion?: boolean; | ||
skitCommitHooks?: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"baseUrl": "./", | ||
"rootDir": "src", | ||
"outDir": "lib" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters