2023-12-04 03:02:39 +00:00
|
|
|
import { MongoMemoryServer } from 'mongodb-memory-server';
|
2023-12-15 04:10:16 +00:00
|
|
|
import { spawn } from 'child_process';
|
2023-12-04 03:02:39 +00:00
|
|
|
|
|
|
|
async function startServerAndRunScript() {
|
|
|
|
// 启动 MongoDB 内存服务器
|
|
|
|
const mongod = await MongoMemoryServer.create();
|
|
|
|
const mongoUri = mongod.getUri();
|
|
|
|
|
|
|
|
console.log('MongoDB Memory Server started:', mongoUri);
|
|
|
|
|
2023-12-15 04:10:16 +00:00
|
|
|
// 通过 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());
|
2023-12-04 03:02:39 +00:00
|
|
|
});
|
|
|
|
|
2023-12-15 04:10:16 +00:00
|
|
|
tsnode.stderr?.on('data', (data) => {
|
2023-12-04 03:02:39 +00:00
|
|
|
console.error(data);
|
|
|
|
});
|
|
|
|
|
2023-12-15 04:10:16 +00:00
|
|
|
tsnode.on('close', (code) => {
|
2023-12-04 03:02:39 +00:00
|
|
|
console.log(`Nodemon process exited with code ${code}`);
|
|
|
|
mongod.stop(); // 停止 MongoDB 内存服务器
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
startServerAndRunScript().catch((err) => {
|
|
|
|
console.error('Error starting server and script:', err);
|
|
|
|
});
|