Skip to content

Commit

Permalink
refactor(package.json): refactor scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
SimpleCodeCX committed Nov 14, 2019
1 parent a3a2cb9 commit 358c9b0
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 17 deletions.
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"name": "koa-ts-full-template",
"version": "1.0.0",
"license": "ISC",
"license": "MIT",
"author": "Simple",
"description": "koa ts template",
"main": "index.js",
"scripts": {
"dev": "nodemon -e ts --exec ts-node -r tsconfig-paths/register src/main.ts",
"dev-server": "nodemon -e ts --exec ts-node -r tsconfig-paths/register src/main.ts",
"prod": "node -r tsconfig-paths/register ./src/main.js",
"test": "jest --runInBand",
"start": "nodemon -e ts --exec ts-node -r tsconfig-paths/register src/main.ts",
"start:dev": "cross-env NODE_ENV=development npm run start",
"start:test": "cross-env NODE_ENV=testing npm run start",
"prod": "cross-env NODE_ENV=production node -r tsconfig-paths/register ./src/main.js",
"test": "cross-env NODE_ENV=development jest --runInBand",
"build": "npm run rm-dist && npm run build-ts && npm run copy-static",
"build-ts": "tsc -b ./ --verbose",
"copy-static": "ts-node copyStatic.ts",
Expand All @@ -24,6 +25,7 @@
"ts"
],
"dependencies": {
"cross-env": "^6.0.3",
"debug": "^4.1.1",
"koa": "^2.8.1",
"koa-body": "^4.1.1",
Expand Down Expand Up @@ -55,7 +57,6 @@
"@typescript-eslint/eslint-plugin": "^2.4.0",
"@typescript-eslint/parser": "^2.4.0",
"apidoc": "^0.17.7",
"cross-env": "^6.0.3",
"eslint": "^6.5.1",
"eslint-config-airbnb": "^18.0.1",
"eslint-plugin-import": "^2.18.2",
Expand Down
4 changes: 3 additions & 1 deletion shell/start-local.cmd → shell/start-dev.cmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
:: 启动开发版

set PORT=8080
:: 数据库密码
set MARIADBPWD=xxxx
npm run dev
npm run start:dev
4 changes: 4 additions & 0 deletions shell/start-prod.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 启动正式版

# MARIADBPWD: 数据库密码
PORT=8080 MARIADBPWD=xxx npm run prod
4 changes: 4 additions & 0 deletions shell/start-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 启动测试版

# MARIADBPWD: 数据库密码
PORT=8080 MARIADBPWD=xxx npm run start:test
3 changes: 2 additions & 1 deletion shell/test.cmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set PORT=8080
:: 运行测试用例

:: 数据库密码
set MARIADBPWD=xxxx
npm run test
17 changes: 11 additions & 6 deletions src/app/config/system.config.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import devConfig from './dev.config';
import devServer from './dev-server.config';
import testConfig from './test.config';
import prodConfig from './prod.config';
import { NODE_ENV } from './variate';
import { normal } from '@lib/logger';
const logger = normal();

class GlobalConfig {
config = {
mariadb: devConfig.mariadb,
isDev: /(^dev.*|^test.*)/.test(process.env.npm_lifecycle_event)
isDev: NODE_ENV === 'development' || NODE_ENV === 'testing'
};

constructor() {
this.getConfig();
}

getConfig() {
switch (process.env.npm_lifecycle_event) {
case 'dev': this.config = Object.assign(this.config, devConfig);
switch (NODE_ENV) {
case 'development': this.config = Object.assign(this.config, devConfig);
break;
case 'dev-server': this.config = Object.assign(this.config, devServer);
case 'testing': this.config = Object.assign(this.config, testConfig);
break;
case 'prod': this.config = Object.assign(this.config, prodConfig);
case 'production': this.config = Object.assign(this.config, prodConfig);
break;
default: this.config = Object.assign(this.config, devConfig);
}
Expand All @@ -29,6 +30,10 @@ class GlobalConfig {
}

const globalConfig = new GlobalConfig();
if (!globalConfig.config.mariadb.password) {
logger.error('process.env.MARIADBPWD is undefined. please see src/app/config/variate.ts.');
throw new Error('process.env.MARIADBPWD is undefined. please see src/app/config/variate.ts.');
}
logger.info(globalConfig.config);

const GLOBAL_CONFIG = globalConfig.config;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MARIADBPWD } from './variate';

// 服务器开发版
// 测试版
export default {
mariadb: {
host: 'xxx.xxx.xxx.xxx', // 数据库地址
Expand Down
10 changes: 9 additions & 1 deletion src/app/config/variate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
/**
* 整个项目的入口变量
*/
export const MARIADBPWD: string = process.env.MARIADBPWD; // 数据库密码

// 开发环境: development || testing || production, 默认 development
export const NODE_ENV: string = process.env.NODE_ENV;

// 数据库密码

export const MARIADBPWD: string = process.env.MARIADBPWD;
// 启动端口号,默认 8080
export const PORT: string = process.env.PORT || '8080';
File renamed without changes.
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { AppServer } from './app/app';
import onerror from 'koa-onerror';
import { PORT } from './app/config/variate';

const appServer = new AppServer();

function normalizePort(val) {
Expand All @@ -13,7 +15,7 @@ function normalizePort(val) {
return false;
}

const port = normalizePort(process.env.PORT || '8080');
const port = normalizePort(PORT || '8080');
/**
* 由于原生的koa的context.onerror不够全面,
* 因此这里重写context.onerror
Expand Down

0 comments on commit 358c9b0

Please sign in to comment.