Skip to content

Commit

Permalink
feat: update the confusing attributes ifShortSHA and ifGitSHA (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongxuYang authored Mar 13, 2024
1 parent 7d98356 commit 1a84089
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# CHANGELOG


## v0.0.12

* Update the confusing attributes `ifShortSHA` and `ifGitSHA`. If you want to enable `ifShortSHA`, you do not need to configure `ifGitSHA` to be `true`. (by [@littlecxm](https://github.com/ZhongxuYang/vite-plugin-version-mark/issues/8))
* The default value of `ifShortSHA` is changed to `false`.

## v0.0.11

* Support export version field in the entry file.
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default defineConfig({
// name: 'test-app',
// version: '0.0.1',
// command: 'git describe --tags',
ifGitSHA: true,
// ifGitSHA: true,
ifShortSHA: true,
ifMeta: true,
ifLog: true,
Expand All @@ -69,7 +69,7 @@ export default defineNuxtConfig({
// name: 'test-app',
// version: '0.0.1',
// command: 'git describe --tags',
ifGitSHA: true,
// ifGitSHA: true,
ifShortSHA: true,
ifMeta: true,
ifLog: true,
Expand All @@ -85,18 +85,19 @@ Then you can use `vite-plugin-version-mark` ! 🎉

> `vite-plugin-version-mark` can be print application version in the `Console`, defined `global` or add `<meta>` in HTML element.
| name | description | type | default | supported |
| Name | Description | Type | Default | Supported |
| --- | --- | --- | --- | --- |
| name | application name | `string` | `name` in package.json | `0.0.1+` |
| version | application version | `string` | `version` in package.json | `0.0.1+` |
| ifGitSHA | use git commit SHA as the version | `boolean` | false | `0.0.1+` |
| ifShortSHA | use git commit short SHA as the version | `boolean` | true | `0.0.1+` |
| ifShortSHA | use git commit short SHA as the version | `boolean` | false | `0.0.1+` |
| command | provide a custom command to retrieve the version <br/>For example: `git describe --tags` | `string` | git rev-parse --short HEAD | `0.0.8+` |
| ifLog | print info in the Console | `boolean` | true | `0.0.1+` |
| ifGlobal | set a variable named *\`\_\_${APPNAME}\_VERSION\_\_\`* in the window<br/>[For TypeScript users, make sure to add the type declarations in the env.d.ts or vite-env.d.ts file to get type checks and Intellisense.](https://vitejs.dev/config/shared-options.html#define) | `boolean` | true | `0.0.4+` |
| ifMeta | add \<meta name="application-name" content="{APPNAME_VERSION}: {version}"> in the \<head> | `boolean` | true | `0.0.1+` |
| ifExport | export the version field in the entry file. This may be used when you use vite to build a `library mode`.<br/>Through `import { {APPNAME}_VERSION } from <your_library_name>` | `boolean` | false | `0.0.11+` |

> The **version field** takes precedence: `command` > `ifShortSHA` > `ifGitSHA` > `version`

## Other
Expand Down
4 changes: 3 additions & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,16 @@ export default defineNuxtConfig({
| name | 应用名 | `string` |`package.json` 中定义的 `name` 属性 | `0.0.1+` |
| version | 应用版本 | `string` |`package.json` 中定义的 `version` 属性 | `0.0.1+` |
| ifGitSHA | 使用git commitSHA作为版本号 | `boolean` | false | `0.0.1+` |
| ifShortSHA | 使用git的短commitSHA作为版本号 | `boolean` | true | `0.0.1+` |
| ifShortSHA | 使用git的短commitSHA作为版本号 | `boolean` | false | `0.0.1+` |
| command | 提供自定义指令,以便自定义版本号的获取方式 <br/>例如使用git tag作为版本号: `git describe --tags` | `string` | git rev-parse --short HEAD | `0.0.8+` |
| ifLog | 在控制台打印版本信息 | `boolean` | true | `0.0.1+` |
| ifGlobal | 在window上定义变量 *\`\_\_${APPNAME}\_VERSION\_\_\`* <br/>[对于TypeScript使用者, 请确保您在 env.d.ts 或者 vite-env.d.ts 文件中定义该变量,以便通过类型检查。](https://vitejs.dev/config/shared-options.html#define) | `boolean` | true | `0.0.4+` |
| ifMeta |`<head>` 中添加 `<meta name="application-name" content="{APPNAME_VERSION}: {version}">` | `boolean` | true | `0.0.1+` |
| ifExport | 在入口文件导出版本字段。这在您使用vite构建 `library mode`时或许会用到。<br />通过 `import { {APPNAME}_VERSION} from <your_library_name>` | `boolean` | false | `0.0.11+` |


> **版本字段**的取值优先级为: `command` > `ifShortSHA` > `ifGitSHA` > `version`
## 其它

### 如何在您的vite插件中获取版本号?
Expand Down
8 changes: 4 additions & 4 deletions playground/vite-webapp/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ const testPlugin: () => Plugin = () => ({
export default defineConfig({
plugins: [
vue(),
vitePluginVersionMark({
ifGitSHA: true,
ifShortSHA: true,
vitePluginVersionMark({
// ifGitSHA: true,
ifShortSHA: true,
// command: 'git describe --tags',
ifMeta: true,
ifLog: true,
ifGlobal: true,
ifExport: true,
// command: 'git describe --tags'
}),
testPlugin(),
],
Expand Down
20 changes: 6 additions & 14 deletions src/plugins/core/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,11 @@ export type VitePluginVersionMarkConfig = {
printInfo: string
}

const getGitSHA = (ifShortSHA: boolean, command: string | undefined) => {
const execCommand = (command: string) => {
const {exec} = childProcess
let sh: string
if (command) {
sh = command
} else if (ifShortSHA) {
sh = 'git rev-parse --short HEAD'
} else {
sh = 'git rev-parse HEAD'
}

return new Promise((resolve, reject) => {
exec(sh, (error, stdout) => {
exec(command, (error, stdout) => {
if (error) {
reject(error)
} else {
Expand All @@ -55,16 +47,16 @@ export const analyticOptions: (options: VitePluginVersionMarkInput) => Promise<V
const {
name = process.env['npm_package_name'],
version = process.env['npm_package_version'],
command = undefined,
ifShortSHA = false,
ifGitSHA = false,
ifShortSHA = true,
ifMeta = true,
ifLog = true,
ifGlobal = true,
ifExport = false,
command = undefined,
} = options

const printVersion = (ifGitSHA ? await getGitSHA(ifShortSHA, command) : version) as string
const finalCommand = command ?? (ifShortSHA ? 'git rev-parse --short HEAD' : ifGitSHA ? 'git rev-parse HEAD' : undefined)
const printVersion = (finalCommand ? await execCommand(finalCommand) : version) as string
const printName = `${name?.replace(/((?!\w).)/g, '_')?.toLocaleUpperCase?.()}_VERSION`
const printInfo = `${printName}: ${printVersion}`

Expand Down

0 comments on commit 1a84089

Please sign in to comment.