feat: 支持windows启动服务端 (#24)

This commit is contained in:
luch 2023-12-15 12:10:16 +08:00 committed by sudoooooo
parent 783675d186
commit 594cdde0cf
2 changed files with 11 additions and 11 deletions

View File

@ -6,22 +6,23 @@
"scripts": {
"copy": "mkdir -p ./build/ && cp -rf ./src/* ./build/",
"build": "tsc",
"launch:local": "SERVER_ENV=local npx ts-node ./src/index.ts",
"launch:dev": "SERVER_ENV=dev npx ts-node ./src/index.ts",
"start:stable": "npm run copy && npm run build && SERVER_ENV=stable node ./build/index.js",
"start:preonline": "npm run copy && npm run build && SERVER_ENV=preonline node ./build/index.js",
"start:online": "npm run copy && npm run build && SERVER_ENV=online node ./build/index.js",
"start": "npm run start:online",
"local": "npx ts-node scripts/run-local.ts",
"dev": "nodemon -e js,mjs,json,ts --exec 'npm run launch:dev' --watch ./src"
"dev": "npx ts-node-dev ./src/index.ts"
},
"devDependencies": {
"@types/crypto-js": "^4.2.1",
"@types/koa": "^2.13.8",
"@types/koa-bodyparser": "^4.3.10",
"@types/koa-router": "^7.4.4",
"cross-env": "^7.0.3",
"mongodb-memory-server": "^9.0.1",
"nodemon": "^2.0.20",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
"typescript": "^4.8.4"
},
"dependencies": {

View File

@ -1,5 +1,5 @@
import { MongoMemoryServer } from 'mongodb-memory-server';
import { exec } from 'child_process';
import { spawn } from 'child_process';
async function startServerAndRunScript() {
// 启动 MongoDB 内存服务器
@ -8,18 +8,17 @@ async function startServerAndRunScript() {
console.log('MongoDB Memory Server started:', mongoUri);
// 通过 nodemon 运行另一个脚本,并传递 MongoDB 连接 URL 作为环境变量
const nodemon = exec(`nodemon -e js,mjs,json,ts --exec 'xiaojuSurveyMongoUrl=${mongoUri} npm run launch:local' --watch ./src`);
nodemon.stdout?.on('data', (data) => {
console.log(data);
// 通过 spawn 运行另一个脚本,并传递 MongoDB 连接 URL 作为环境变量
const tsnode = spawn('cross-env', [`xiaojuSurveyMongoUrl="${mongoUri}"`, 'npx', 'ts-node-dev', './src/index.ts'])
tsnode.stdout?.on('data', (data) => {
console.log(data.toString());
});
nodemon.stderr?.on('data', (data) => {
tsnode.stderr?.on('data', (data) => {
console.error(data);
});
nodemon.on('close', (code) => {
tsnode.on('close', (code) => {
console.log(`Nodemon process exited with code ${code}`);
mongod.stop(); // 停止 MongoDB 内存服务器
});