forked from xjh22222228/nav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
53 lines (45 loc) · 1.16 KB
/
server.js
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
// Copyright @ 2018-present xiejiahe. All rights reserved. MIT license.
// See https://github.com/xjh22222228/nav
// 启动端口
const PORT = 7777
const DEPLOY_DIR = process.cwd()
const express = require('express')
const cors = require('cors')
const fs = require('node:fs')
const { execSync } = require('child_process')
// 创建 Express 应用实例
const app = express()
app.use(express.static('dist'))
app.get('/server', (req, res) => {
console.log('正在部署...')
let gitDir = ''
try {
gitDir = execSync('which git').toString()
} catch (error) {
return res.send('找不到 git 命令位置')
}
if (!fs.existsSync(DEPLOY_DIR)) {
return res.send(`仓库目录不存在: ${DEPLOY_DIR}`)
}
try {
const res = execSync(
`cd ${DEPLOY_DIR} && npm run pull && npm run build`
).toString()
} catch (error) {
console.log('Failed:', error.message)
return res?.send?.(error.message)
}
console.log('OK')
res?.send?.('OK')
})
app.use(
cors({
origin: '*',
methods: '*',
allowedHeaders: '*',
})
)
app.listen(PORT, () => {
console.log(`Server is running on port :${PORT}`)
console.log(`Directory: ${DEPLOY_DIR}`)
})