diff --git a/ecosystem.config.js b/ecosystem.config.js new file mode 100644 index 00000000..55a7dcd6 --- /dev/null +++ b/ecosystem.config.js @@ -0,0 +1,29 @@ +/* + * @Author: Innei + * @Date: 2020-04-30 18:14:55 + * @LastEditTime: 2020-05-25 21:05:26 + * @LastEditors: Innei + * @FilePath: /mx-server/ecosystem.config.js + * @Copyright + */ + +// eslint-disable-next-line @typescript-eslint/no-var-requires +const env = require('dotenv').config().parsed +module.exports = { + apps: [ + { + name: 'mx-space-server@next', + script: 'dist/main.js', + autorestart: true, + instances: 'max', + exec_mode: 'cluster', + watch: false, + // instances: 1, + // max_memory_restart: env.APP_MAX_MEMORY || '150M', + env: { + NODE_ENV: 'production', + ...env, + }, + }, + ], +} diff --git a/package.json b/package.json index 4ccb30c3..6302cb50 100644 --- a/package.json +++ b/package.json @@ -23,9 +23,13 @@ "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "start": "cross-env NODE_ENV=development nest start -w", "start:dev": "nest start --watch", - "start:debug": "nest start --debug --watch", - "start:prod": "node dist/main", + "start:debug": "cross-env NODE_ENV=development nest start --debug --watch", + "start:prod": "cross-env NODE_ENV=production node dist/main", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", + "prod": "cross-env NODE_ENV=production pm2-runtime start ecosystem.config.js", + "prod:pm2": "cross-env NODE_ENV=production pm2 restart ecosystem.config.js", + "prod:stop": "pm2 stop ecosystem.config.js", + "prod:debug": "cross-env NODE_ENV=production nest start --debug --watch", "test": "jest", "test:watch": "jest --watch", "test:cov": "jest --coverage", @@ -39,6 +43,7 @@ "@nestjs/swagger": "^5.0.8", "@typegoose/auto-increment": "^0.6.0", "@typegoose/typegoose": "^7.6.3", + "chalk": "^4.1.1", "fastify-swagger": "^4.8.3", "husky": "^7.0.1", "lint-staged": "^11.1.0", @@ -63,6 +68,7 @@ "@typescript-eslint/eslint-plugin": "4.28.4", "@typescript-eslint/parser": "4.28.4", "cross-env": "^7.0.3", + "dotenv": "^10.0.0", "eslint": "^7.31.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-prettier": "^3.4.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1811e18a..d00404db 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -16,7 +16,9 @@ specifiers: '@types/supertest': ^2.0.11 '@typescript-eslint/eslint-plugin': 4.28.4 '@typescript-eslint/parser': 4.28.4 + chalk: ^4.1.1 cross-env: ^7.0.3 + dotenv: ^10.0.0 eslint: ^7.31.0 eslint-config-prettier: ^8.3.0 eslint-plugin-prettier: ^3.4.0 @@ -48,6 +50,7 @@ dependencies: '@nestjs/swagger': 5.0.8_8a33bebe899fbeffdca6cb260e5489d3 '@typegoose/auto-increment': 0.6.0_mongoose@5.13.3 '@typegoose/typegoose': 7.6.3_mongoose@5.13.3 + chalk: 4.1.1 fastify-swagger: 4.8.3 husky: 7.0.1 lint-staged: 11.1.0 @@ -72,6 +75,7 @@ devDependencies: '@typescript-eslint/eslint-plugin': 4.28.4_b1648df9f9ba40bdeef3710a5a5af353 '@typescript-eslint/parser': 4.28.4_eslint@7.31.0+typescript@4.3.5 cross-env: 7.0.3 + dotenv: 10.0.0 eslint: 7.31.0 eslint-config-prettier: 8.3.0_eslint@7.31.0 eslint-plugin-prettier: 3.4.0_19f511d6aa08b367b6cb59e8f50291ca @@ -2233,6 +2237,11 @@ packages: tslib: 2.3.0 dev: false + /dotenv/10.0.0: + resolution: {integrity: sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==} + engines: {node: '>=10'} + dev: true + /ecdsa-sig-formatter/1.0.11: resolution: {integrity: sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==} dependencies: diff --git a/src/app.module.ts b/src/app.module.ts index d71dd70a..4784e122 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,9 +1,10 @@ import { Module } from '@nestjs/common' import { AppController } from './app.controller' import { AppService } from './app.service' +import { InitModule } from './modules/init/init.module' @Module({ - imports: [], + imports: [InitModule], controllers: [AppController], providers: [AppService], }) diff --git a/src/constants/path.constant.ts b/src/constants/path.constant.ts new file mode 100644 index 00000000..0fcd1e35 --- /dev/null +++ b/src/constants/path.constant.ts @@ -0,0 +1,19 @@ +/* + * @Author: Innei + * @Date: 2020-08-01 19:49:31 + * @LastEditTime: 2021-03-21 19:36:20 + * @LastEditors: Innei + * @FilePath: /server/shared/constants/index.ts + * @Coding with Love + */ +import { homedir } from 'os' +import { join } from 'path' +import { isDev } from 'src/utils' + +export const HOME = homedir() + +export const TEMP_DIR = isDev ? join(__dirname, '../tmp') : '/tmp/mx-space' + +export const DATA_DIR = isDev + ? join(__dirname, '../tmp') + : join(HOME, '.mx-space') diff --git a/src/modules/init/init.module.ts b/src/modules/init/init.module.ts new file mode 100644 index 00000000..54834e1d --- /dev/null +++ b/src/modules/init/init.module.ts @@ -0,0 +1,8 @@ +import { Module } from '@nestjs/common' +import { InitService } from './init.service' + +@Module({ + providers: [InitService], + exports: [InitService], +}) +export class InitModule {} diff --git a/src/modules/init/init.service.spec.ts b/src/modules/init/init.service.spec.ts new file mode 100644 index 00000000..b9a567bd --- /dev/null +++ b/src/modules/init/init.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { InitService } from './init.service'; + +describe('InitService', () => { + let service: InitService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [InitService], + }).compile(); + + service = module.get(InitService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/src/modules/init/init.service.ts b/src/modules/init/init.service.ts new file mode 100644 index 00000000..5591fd77 --- /dev/null +++ b/src/modules/init/init.service.ts @@ -0,0 +1,27 @@ +import { Injectable, Logger } from '@nestjs/common' +import chalk from 'chalk' +import { mkdirSync } from 'fs' +import { DATA_DIR, TEMP_DIR } from 'src/constants/path.constant' + +@Injectable() +export class InitService { + private logger = new Logger(InitService.name) + constructor() { + this.initDirs() + } + + getTempdir() { + return TEMP_DIR + } + + getDatadir() { + return DATA_DIR + } + + initDirs() { + mkdirSync(DATA_DIR, { recursive: true }) + this.logger.log(chalk.blue('数据目录已经建好: ' + DATA_DIR)) + mkdirSync(TEMP_DIR, { recursive: true }) + this.logger.log(chalk.blue('临时目录已经建好: ' + TEMP_DIR)) + } +} diff --git a/tsconfig.json b/tsconfig.json index d8e88dc3..06a712b9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,7 @@ "removeComments": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, + "esModuleInterop": true, "allowSyntheticDefaultImports": true, "target": "es2017", "sourceMap": true, @@ -13,4 +14,4 @@ "incremental": true, "skipLibCheck": true } -} +} \ No newline at end of file