-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (73 loc) · 2 KB
/
index.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env node
/**
* @doc true
* @fileDoc true
* @author xupengfei
* @name index.js
* @type main
* @desc project main file, create api server and run vue app with vite
*/
const fs = require('fs')
const path = require('path')
const http = require('http')
const Router = require('./src/core/Route.js')
const Docs = require('./src/core/Docs.js')
const compressing = require('compressing')
const argv = process.argv
if (!argv[2] || argv[2].trim() === '--build') {
argv.splice(2, 0, path.join(__dirname, 'src'))
}
const src = argv[2]
process.env.DOC_SRC = src
const isBuild = argv[3] && argv[3].trim() === '--build'
if (isBuild) {
const outDir = argv[4] ? argv[4] : path.join(__dirname, 'release')
const zipfile = path.join(__dirname, 'pre-build.zip')
compressing.zip
.uncompress(zipfile, outDir)
.then(() => {
const doc = new Docs(src).getDoc()
fs.writeFileSync(path.join(outDir, 'docs.json'), JSON.stringify(doc))
console.log('Build Success: ', outDir)
})
.catch()
return
}
let base = ''
/**
* @doc true
* @name api server
* @desc project api server
*/
const server = http.createServer(async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Headers', 'content-type')
res.setHeader('Access-Control-Allow-Methods', 'POST,GET,OPTIONS')
if (req.method.toLowerCase() === 'options') {
res.statusCode = 200
res.end()
return
}
let post = ''
req.on('data', function (chunk) {
post += chunk
})
req.on('end', function () {
post = JSON.parse(post || '{}')
const get = {}
const url = new URL(req.url, base)
url.searchParams.forEach((value, name) => {
get[name] = value
})
const params = { ...get, ...post }
req.params = params
req._url = url
const router = new Router(req, res)
})
})
server.on('listening', () => {
const port = server.address().port
console.log('Doc Serve: ', `http://localhost:${port}/`)
base = `http://localhost:${port}/`
})
server.listen(0)