-
Notifications
You must be signed in to change notification settings - Fork 1
/
dev.ts
128 lines (89 loc) · 2.51 KB
/
dev.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import type { Context } from 'koa'
import { Remote, set_inspect_options, fexists, check, ramdisk, noprint } from 'xshell'
import { Server } from 'xshell/server.js'
import { builder, fpd_root, fpd_out } from './builder.ts'
set_inspect_options()
console.log('项目根目录:', fpd_root)
check(ramdisk || fexists(`${fpd_root}.vscode/settings.json`, noprint), '需要将 .vscode/settings.template.json 复制为 .vscode/settings.json')
class DevServer extends Server {
override async router (ctx: Context) {
let { request, response } = ctx
const { path } = request
if (path === '/api/recompile') {
response.status = 200
await builder.run()
return true
}
return this.try_send(
ctx,
path.fext ? path.slice(1) : 'index.html',
{ fpd_root: fpd_out }
)
}
}
let server = new DevServer({
name: 'web 开发服务器',
http: true,
http_port: 8432,
})
await Promise.all([
server.start(),
builder.build(false)
])
let remote: Remote
// 监听终端快捷键
// https://stackoverflow.com/a/12506613/7609214
let { stdin } = process
stdin.setRawMode(true)
stdin.resume()
stdin.setEncoding('utf-8')
// on any data into stdin
stdin.on('data', function (key: any) {
// ctrl-c ( end of text )
if (key === '\u0003')
process.exit()
// write the key to stdout all normal like
console.log(key)
switch (key) {
case 'r':
builder.run()
break
case 'x':
remote?.disconnect()
process.exit()
case 'i':
console.log(info)
break
}
})
if (ramdisk) {
remote = new Remote({
url: 'ws://localhost',
keeper: {
func: 'register',
args: ['ddb.web'],
},
funcs: {
async recompile () {
await builder.run()
return [ ]
},
async exit () {
await builder.close()
remote.disconnect()
process.exit()
}
}
})
await remote.connect()
}
const info = 'http://localhost:8432/\n'.blue.underline
console.log(
'\n' +
'开发服务器启动成功,请使用浏览器打开:\n'.green +
info +
'终端快捷键:\n' +
'r: 重新编译\n' +
'i: 打印地址信息\n' +
'x: 退出开发服务器\n'
)