Skip to content

Commit 14e7860

Browse files
committed
Auto format
1 parent d9cc76a commit 14e7860

File tree

5 files changed

+30
-32
lines changed

5 files changed

+30
-32
lines changed

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,57 @@
55
[![npm downloads](https://img.shields.io/npm/dm/coa-env.svg?style=flat-square)](http://npm-stat.com/charts.html?package=coa-env)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/coajs/coa-env/pulls)
77

8-
COA框架环境配置包装器,用于统一化通用的环境和配置信息
8+
COA 框架环境配置包装器,用于统一化通用的环境和配置信息
99

1010
## 数据结构
1111

1212
```typescript
13-
1413
// 以下属性均为只读属性,在实例创建时刻就已经固定,实例生成后无法修改
1514

1615
class CoaEnv {
17-
1816
// runEnv 运行环境,一般定义为开发类环境('d0' 'd1' 'd2') 测试类环境('t0' 't1' 't2') 生产类环境('v0' 'v1' 'v2')等
19-
// 由环境变量 process.env.RUN_ENV 控制,如果没有定义,则默认为 'd0'
17+
// 由环境变量 process.env.RUN_ENV 控制,如果没有定义,则默认为 'd0'
2018
public readonly runEnv: string
21-
19+
2220
// runEnvType 运行环境类型,单字母形式,便于判断某一类环境,根据 runEnv 自动判断,如 'd' 't' 'v'
2321
public readonly runEnvType: 'd' | 't' | 'v' | string
24-
22+
2523
// runEnvName 运行环境的名称,便于对外展示环境的名称,根据 runEnvType 自动判断,如 'alpha' 'beta' 'online'
2624
public readonly runEnvName: 'alpha' | 'beta' | 'online' | 'unknown' | string
27-
25+
2826
// cwd 当前运行Node.js进程的工作目录,由 process.cwd() 控制
2927
public readonly cwd: string
30-
28+
3129
// name 当前包名,由 package.json 中的 name 控制
3230
public readonly name: string
33-
31+
3432
// isProd 是否是生产环境,由 process.env.NODE_ENV === 'production' 控制,只要不是 'production' 均为非生产环境
3533
public readonly isProd: boolean
36-
34+
3735
// isOnline 是否是线上环境,由 runEnvType === 'v' 控制,只要不是 'v' 均为非线上环境
3836
public readonly isOnline: boolean
39-
37+
4038
// hostname 当前主机名称,由 process.env.HOSTNAME 控制,默认为 'local'
4139
public readonly hostname: string
42-
40+
4341
// 当前运行的版本号,解耦出来,由外部程序控制,创建实例时必传此参数
4442
public readonly version: string
45-
4643
}
4744
```
48-
_以上为 TypeScript 代码_
4945

46+
_以上为 TypeScript 代码_
5047

5148
## 用法
5249

5350
### 安装
51+
5452
```shell
5553
yarn add coa-env
5654
```
5755

5856
### 示例
59-
```typescript
6057

58+
```typescript
6159
import { CoaEnv } from 'coa-env'
6260

6361
// 创建一个新的环境实例
@@ -81,14 +79,14 @@ const runEnvName = appEnv.runEnvName
8179

8280
// 获取当前工作目录
8381
const cwd = appEnv.cwd
84-
8582
```
8683

8784
### 配置自动选择
8885

8986
定义不同环境的配置表,会自动根据环境类型返回对应环境的配置信息
9087

9188
- 精确返回对应环境的配置
89+
9290
```typescript
9391
// 用户 d1 t1 v1 等精确的环境,精确返回对应环境的配置
9492
const hostConfig1 = appEnv.getConfig({
@@ -103,6 +101,7 @@ const hostConfig1 = appEnv.getConfig({
103101
```
104102

105103
- 返回一类环境的配置
104+
106105
```typescript
107106
// 用 d t v 可以代表一类环境,返回该类环境
108107
const hostConfig2 = appEnv.getConfig({
@@ -117,6 +116,7 @@ const hostConfig2 = appEnv.getConfig({
117116
```
118117

119118
- 返回默认环境配置
119+
120120
```typescript
121121
// 用 $ 可以代表默认配置,当配置表不存在对应环境时,返回默认配置
122122
const hostConfig3 = appEnv.getConfig({
@@ -125,4 +125,4 @@ const hostConfig3 = appEnv.getConfig({
125125
})
126126
// 当环境是t1 t2 t3,返回 { host: '192.168.0.1' }
127127
// 其他环境,一律返回 { host: '127.0.0.1' }
128-
```
128+
```

src/CoaEnv.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { CoaError } from 'coa-error'
22

33
// 以下属性均为只读属性,不可修改,在实例创建时刻就已经固定
44
export class CoaEnv {
5-
65
// runEnv 运行环境,一般定义为开发类环境('d0' 'd1' 'd2') 测试类环境('t0' 't1' 't2') 生产类环境('v0' 'v1' 'v2')等
76
// 由环境变量 process.env.RUN_ENV 控制,如果没有定义,则默认为 'd0'
87
public readonly runEnv: string
@@ -31,21 +30,21 @@ export class CoaEnv {
3130
// 当前运行的版本号,解耦出来,由外部程序控制,创建实例时必传此参数
3231
public readonly version: string
3332

34-
constructor (version: string) {
33+
constructor(version: string) {
3534
const env = process.env || {}
3635
this.cwd = process.cwd()
3736
this.version = version
38-
this.runEnv = env.RUN_ENV || 'd0'
37+
this.runEnv = env.RUN_ENV ?? 'd0'
3938
this.runEnvType = this.runEnv.substr(0, 1)
40-
this.runEnvName = ({ d: 'alpha', t: 'beta', v: 'online' } as { [key: string]: string })[this.runEnvType] || 'unknown'
41-
this.name = env.npm_package_name || ''
42-
this.hostname = env.HOSTNAME || 'local'
39+
this.runEnvName = { d: 'alpha', t: 'beta', v: 'online' }[this.runEnvType] ?? 'unknown'
40+
this.name = env.npm_package_name ?? ''
41+
this.hostname = env.HOSTNAME ?? 'local'
4342
this.isProd = env.NODE_ENV === 'production'
4443
this.isOnline = this.runEnvType === 'v'
4544
}
4645

4746
// 根据环境获取对应配置信息
48-
getConfig<T> (configs: { $?: T, d?: T, t?: T, v?: T, d0?: T, d1?: T, t1?: T, v1?: T }) {
49-
return configs[this.runEnv as '$'] || configs[this.runEnvType as '$'] || configs['$'] || CoaError.throw('Env.ConfigNotFound', '配置信息不存在')
47+
getConfig<T>(configs: { $?: T; d?: T; t?: T; v?: T; d0?: T; d1?: T; t1?: T; v1?: T }) {
48+
return configs[this.runEnv as '$'] ?? configs[this.runEnvType as '$'] ?? configs.$ ?? CoaError.throw('Env.ConfigNotFound', '配置信息不存在')
5049
}
51-
}
50+
}

src/demo.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
12
import { CoaEnv } from './CoaEnv'
23

34
// 创建一个新的环境实例
@@ -50,4 +51,4 @@ const hostConfig3 = appEnv.getConfig({
5051
v: { host: '172.16.0.1' },
5152
})
5253
// 当环境是t1 t2 t3,返回 { host: '192.168.0.1' }
53-
// 其他环境,一律返回 { host: '127.0.0.1' }
54+
// 其他环境,一律返回 { host: '127.0.0.1' }

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { CoaEnv } from './CoaEnv'
1+
export { CoaEnv } from './CoaEnv'

tsconfig.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
"compilerOptions": {
33
"strict": true,
44
"module": "commonjs",
5-
"target": "esnext",
5+
"target": "es2019",
66
"outDir": "dist",
77
"declaration": true
88
},
9-
"include": [
10-
"src"
11-
]
9+
"include": ["src"]
1210
}

0 commit comments

Comments
 (0)