-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.ts
48 lines (40 loc) · 925 Bytes
/
server.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
import fs from 'fs';
import path from 'path';
import express from 'express';
import { green, red } from 'chalk';
import config from './build/webpack/webpack.prod';
import { exec } from 'child_process';
const PORT = 8001;
const distPath = config?.output?.path || path.resolve(__dirname, 'dist');
/**
* 启动测试服务器
*/
function startApp() {
const app = express();
// 将dist作为静态资源入口
app.use(express.static(distPath));
// 监听端口
app.listen(PORT, (err) => {
if (err) {
console.log(err);
return;
}
console.log(
green(
`Project is running at http://localhost:${PORT}. Press Ctrl+C to stop.\n`
)
);
});
}
if (fs.existsSync(distPath)) {
startApp();
} else {
const command = 'npm run build';
exec(command, (err) => {
if (err) {
console.log(red(`build error: \n\n${err}`));
return;
}
startApp();
});
}