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

This commit is contained in:
luch 2023-12-15 12:10:16 +08:00 committed by GitHub
parent dd2846bcb1
commit 59e9981914
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 11 deletions

View File

@ -6,22 +6,23 @@
"scripts": { "scripts": {
"copy": "mkdir -p ./build/ && cp -rf ./src/* ./build/", "copy": "mkdir -p ./build/ && cp -rf ./src/* ./build/",
"build": "tsc", "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: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: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:online": "npm run copy && npm run build && SERVER_ENV=online node ./build/index.js",
"start": "npm run start:online", "start": "npm run start:online",
"local": "npx ts-node scripts/run-local.ts", "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": { "devDependencies": {
"@types/crypto-js": "^4.2.1", "@types/crypto-js": "^4.2.1",
"@types/koa": "^2.13.8", "@types/koa": "^2.13.8",
"@types/koa-bodyparser": "^4.3.10", "@types/koa-bodyparser": "^4.3.10",
"@types/koa-router": "^7.4.4", "@types/koa-router": "^7.4.4",
"cross-env": "^7.0.3",
"mongodb-memory-server": "^9.0.1", "mongodb-memory-server": "^9.0.1",
"nodemon": "^2.0.20", "nodemon": "^2.0.20",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
"typescript": "^4.8.4" "typescript": "^4.8.4"
}, },
"dependencies": { "dependencies": {

View File

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