minecraft-dev-starter 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +53 -9
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* @module minecraft-dev-starter
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
+
import { readFileSync } from 'node:fs'
|
|
9
10
|
import { createApi } from './lib/api.js'
|
|
10
11
|
import { createLaunchService } from './lib/launches.js'
|
|
11
12
|
import { applyTools } from './tools.js'
|
|
@@ -14,10 +15,31 @@ import { applyTools } from './tools.js'
|
|
|
14
15
|
export const name = 'mcdev-host'
|
|
15
16
|
|
|
16
17
|
/** 依赖的宿主服务。 */
|
|
17
|
-
|
|
18
|
+
/**
|
|
19
|
+
* 必需服务:会话(取工作区)与工具注册表。
|
|
20
|
+
*
|
|
21
|
+
* `webServer` 不在这里——它是可选的(纯 CLI profile 没有 web 层),
|
|
22
|
+
* 由 `apply` 里的 `ctx.inject(['webServer'], …)` 单独等。
|
|
23
|
+
*/
|
|
24
|
+
export const inject = ['sessions', 'tools']
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 插件版本;客户端会显示,用来确认两边同一份代码。
|
|
28
|
+
*
|
|
29
|
+
* 从 package.json 读,不再手写——手写的常量在发版后必然忘记同步,
|
|
30
|
+
* 面板上就会出现"装了 0.1.1、显示 0.1.0"的假版本。
|
|
31
|
+
*/
|
|
32
|
+
const VERSION = readPackageVersion()
|
|
18
33
|
|
|
19
|
-
/**
|
|
20
|
-
|
|
34
|
+
/** @returns {string} */
|
|
35
|
+
function readPackageVersion() {
|
|
36
|
+
try {
|
|
37
|
+
const url = new URL('../package.json', import.meta.url)
|
|
38
|
+
return String(JSON.parse(readFileSync(url, 'utf8')).version)
|
|
39
|
+
} catch {
|
|
40
|
+
return '0.0.0-unknown'
|
|
41
|
+
}
|
|
42
|
+
}
|
|
21
43
|
|
|
22
44
|
/**
|
|
23
45
|
* @param {import('node:http').IncomingMessage} req
|
|
@@ -119,9 +141,14 @@ export function apply(ctx, config = {}) {
|
|
|
119
141
|
// 面板与模型共用同一套服务;工具只是薄封装(M5)
|
|
120
142
|
applyTools(ctx, { api })
|
|
121
143
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
144
|
+
// webServer 是**可选**的:纯 CLI / headless profile 里没有它,
|
|
145
|
+
// 这时插件应当照常激活(工具可用),只是没有 HTTP 路由。
|
|
146
|
+
// 早期把它写进 inject 会让整个 profile 加载失败("pending: waiting for service")。
|
|
147
|
+
ctx.effect(() => {
|
|
148
|
+
const mount = (scoped) => {
|
|
149
|
+
const webServer = scoped?.webServer
|
|
150
|
+
if (webServer === undefined || typeof webServer.register !== 'function') return undefined
|
|
151
|
+
const dispose = webServer.register({
|
|
125
152
|
kind: 'prefix',
|
|
126
153
|
path: '/mcdev/api',
|
|
127
154
|
handler: async (req, res) => {
|
|
@@ -157,9 +184,26 @@ export function apply(ctx, config = {}) {
|
|
|
157
184
|
writeJson(res, 500, { ok: false, error: { code: 'internal', message } })
|
|
158
185
|
}
|
|
159
186
|
},
|
|
160
|
-
})
|
|
161
|
-
|
|
162
|
-
|
|
187
|
+
})
|
|
188
|
+
console.log('[mcdev] http routes mounted at /mcdev/api')
|
|
189
|
+
return () => {
|
|
190
|
+
try {
|
|
191
|
+
dispose?.()
|
|
192
|
+
} catch {
|
|
193
|
+
/* 已经在卸载 */
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (typeof ctx.inject === 'function') {
|
|
199
|
+
return ctx.inject(['webServer'], (scoped) => mount(scoped))
|
|
200
|
+
}
|
|
201
|
+
try {
|
|
202
|
+
return mount(ctx)
|
|
203
|
+
} catch {
|
|
204
|
+
return undefined
|
|
205
|
+
}
|
|
206
|
+
}, 'mcdev: /mcdev/api routes')
|
|
163
207
|
|
|
164
208
|
console.log(`[mcdev] host ready (${VERSION}) — /mcdev/api/{projects.scan,projects.detail,state.info}`)
|
|
165
209
|
}
|