Skip to content

Commit c66bce8

Browse files
authored
feat: opt config load and add husky (#12)
* feat: opt config load and add husky * fix: alter code comment
1 parent b640358 commit c66bce8

15 files changed

Lines changed: 505 additions & 201 deletions

.env.default

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
NODE_ENV=
21

32
REDIS_HOST=
43
# parseInt(process.env.REDIS_PORT, 10)
@@ -20,3 +19,6 @@ REDIS_HOST=
2019

2120
LOKALISE_TOKEN=
2221
LOKALISE_PROJECT_ID=
22+
23+
PORT=
24+
GLOBAL_PREFIX=

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ jest-stare/
1919
.dist/
2020
_dist/
2121
.vscode
22-
.env
2322
.history
2423
.yarn/install-state.gz
2524

26-
.yarn/cache
25+
.yarn/cache
26+
.config*.json

.husky/pre-commit

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
export NVM_DIR="$HOME/.nvm"
4+
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
5+
6+
echo "Running lint-staged..."
7+
npx lint-staged

package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
"@midwayjs/redis": "3",
2020
"@midwayjs/validate": "3",
2121
"@typegoose/typegoose": "^11.7.1",
22-
"class-transformer": "^0.5.1",
23-
"class-validator": "^0.14.0",
2422
"dotenv": "^16.3.1",
2523
"glob": "^10.3.10",
24+
"lint-staged": "^15.2.2",
2625
"lodash": "^4.17.21",
2726
"mongoose": "^7.0.0",
2827
"mongoose-delete": "^1.0.1",
@@ -39,6 +38,7 @@
3938
"@types/node": "14",
4039
"cross-env": "^7.0.3",
4140
"eslint-plugin-import": "^2.29.0",
41+
"husky": "^9.0.11",
4242
"jest": "^29.7.0",
4343
"lokalise-client": "^1.1.9",
4444
"mwts": "^1.3.0",
@@ -58,8 +58,8 @@
5858
"lint:fix": "mwts fix",
5959
"ci": "npm run cov",
6060
"build": "midway-bin build -c",
61-
"prepare-env": "yarn dlx ts-node ./src/scripts/prepare-env.ts",
62-
"fetch:locale": "rimraf src/locales/*.json & npx lokalise-client fetch"
61+
"fetch:locale": "rimraf src/locales/*.json & npx lokalise-client fetch",
62+
"prepare": "husky"
6363
},
6464
"midway-bin-clean": [
6565
".vscode/.tsbuildinfo",
@@ -71,5 +71,10 @@
7171
},
7272
"author": "anonymous",
7373
"license": "MIT",
74-
"packageManager": "yarn@4.0.2"
74+
"packageManager": "yarn@4.0.2",
75+
"lint-staged": {
76+
"*{.ts,.tsx}": [
77+
"yarn run lint:fix"
78+
]
79+
}
7580
}

src/config/base.ts

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/config/config.default.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* eslint-disable no-process-env */
2+
3+
import localeConfig from '../locales';
4+
5+
const getRedisConfig = (db = 0, options: Record<string, unknown> = {}) => ({
6+
host: process.env.REDIS_HOST,
7+
port: parseInt(process.env.REDIS_PORT, 10),
8+
password: process.env.REDIS_PWD,
9+
db,
10+
...options,
11+
});
12+
13+
/*
14+
default中的配置项,会被config.xxxx.ts中相同配置项覆盖
15+
*/
16+
export default {
17+
koa: {
18+
port: Number(process.env.PORT),
19+
globalPrefix: process.env.GLOBAL_PREFIX,
20+
},
21+
midwayLogger: {
22+
default: {
23+
enableFile: false,
24+
enableConsole: false,
25+
},
26+
},
27+
redis: getRedisConfig(parseInt(process.env.REDIS_DB, 10)),
28+
mongoose: {
29+
dataSource: {
30+
default: {
31+
uri: process.env.MONGODB_URI,
32+
options: {
33+
user: process.env.MONGODB_USER,
34+
pass: process.env.MONGODB_PASSWORD,
35+
},
36+
},
37+
},
38+
},
39+
NODE_ENV: process.env.NODE_ENV,
40+
aws: {
41+
awsAccessKeyId: process.env.AWS_ACCESS_KEY_ID,
42+
awsSecretKey: process.env.AWS_SECRET_KEY,
43+
awsRegion: process.env.AWS_REGION,
44+
},
45+
keys: process.env.COOKIE_SIGN_KEY,
46+
i18n: {
47+
defaultLocale: 'en-us',
48+
localeTable: localeConfig,
49+
fallbacks: {
50+
'*': 'en-us',
51+
},
52+
writeCookie: false,
53+
resolver: false,
54+
},
55+
};

src/config/config.local.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
/* eslint-disable no-console */
2-
/* eslint-disable no-process-env */
3-
41
import { MidwayConfig } from '@midwayjs/core';
5-
import { merge } from 'lodash';
6-
7-
import { baseConfig } from './base';
82

9-
export default (): MidwayConfig => {
10-
return merge(baseConfig, {
11-
koa: {
12-
port: 7001,
13-
},
14-
midwayLogger: {
15-
default: {
16-
console: false,
17-
file: false,
18-
},
19-
},
20-
}) as MidwayConfig;
21-
};
3+
export default {} as MidwayConfig;

src/config/config.production.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { baseConfig } from './base';
1+
import { MidwayConfig } from '@midwayjs/core';
22

3-
import type { MidwayConfig } from '@midwayjs/core';
4-
5-
export default baseConfig as MidwayConfig;
3+
export default {} as MidwayConfig;

src/config/config.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { baseConfig } from './base';
1+
import { MidwayConfig } from '@midwayjs/core';
22

3-
import type { MidwayConfig } from '@midwayjs/core';
4-
5-
export default baseConfig as MidwayConfig;
3+
export default {} as MidwayConfig;

src/configuration.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import './init';
2+
13
import { hostname } from 'os';
24
import path, { join } from 'path';
35

@@ -21,6 +23,8 @@ import { sync } from 'read-pkg';
2123
import { DefaultErrorFilter } from './filter/default.filter';
2224
import { LocaleMiddleware } from './middleware/locale.middleware';
2325
import { ResponseWrapperMiddleware } from './middleware/response-wrapper.middleware';
26+
import { RUNTIME_ENV_MAP, ServerEnv } from './types/config/config.dto';
27+
import { validateBy } from './utils/common';
2428
import { CloudwatchTransport } from './utils/logger';
2529
import { registerModel } from './utils/register-model';
2630
@Configuration({
@@ -57,11 +61,19 @@ export class MainConfiguration {
5761
@Inject()
5862
dataSourceManager: mongoose.MongooseDataSourceManager;
5963

64+
async onConfigLoad() {
65+
const config = this.app.getConfig();
66+
67+
return validateBy(config, ServerEnv, {
68+
allowUnknown: true,
69+
});
70+
}
71+
6072
async onReady(applicationContext: IMidwayContainer) {
6173
this.app.useMiddleware([LocaleMiddleware, ResponseWrapperMiddleware]);
6274
this.app.useFilter([DefaultErrorFilter]);
6375

64-
if (this.envConfig === 'production') {
76+
if (this.envConfig === RUNTIME_ENV_MAP.PRODUCTION) {
6577
const cloudwatchTransport = new CloudwatchTransport({
6678
app: this.app.getProjectName(),
6779
hostname: hostname(),

0 commit comments

Comments
 (0)