-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandbox.ts
55 lines (43 loc) · 1.47 KB
/
sandbox.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import express, { Request, Response } from 'express';
import fs from 'fs';
import { spawn } from 'child_process';
const app = express();
const port = 3000;
app.use(express.json());
app.post('/update-jsx', (req: Request, res: Response) => {
const jsxCode = req.body.jsxCode;
// 写入JSX代码到./next-container/src/App.jsx
fs.writeFileSync('./next-container/src/App.jsx', jsxCode);
// 在./next-container目录下执行npm run build
const buildProcess = spawn('npm', ['run', 'build'], { cwd: './next-container' });
buildProcess.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
buildProcess.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
buildProcess.on('close', (code) => {
if (code === 0) {
// build成功后,执行npm run start
const startProcess = spawn('npm', ['run', 'start'], { cwd: './next-container' });
startProcess.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
});
startProcess.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
});
startProcess.on('close', (code) => {
if (code === 0) {
res.status(200).send('Next.js已经执行成功');
} else {
res.status(500).send('执行npm run start失败');
}
});
} else {
res.status(500).send('执行npm run build失败');
}
});
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});