@vincentbanana/bananacli 0.3.0 → 0.4.0
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/dist/server/ui-server.js +23 -5
- package/dist/ui/app.js +1253 -0
- package/dist/ui/index.html +108 -0
- package/dist/ui/style.css +885 -0
- package/package.json +3 -3
package/dist/server/ui-server.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createServer } from 'node:http';
|
|
2
2
|
import { randomBytes } from 'node:crypto';
|
|
3
3
|
import { mkdir, readdir, readFile, stat, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { existsSync } from 'node:fs';
|
|
4
5
|
import { extname, join, resolve, dirname } from 'node:path';
|
|
5
6
|
import { spawn } from 'node:child_process';
|
|
6
7
|
import { fileURLToPath } from 'node:url';
|
|
@@ -41,13 +42,30 @@ const SKIP_DIRS = new Set([
|
|
|
41
42
|
'node_modules', '.git', 'dist', 'build', 'out', '.next', '.nuxt', 'coverage', '__pycache__',
|
|
42
43
|
'.venv', 'venv', 'target', '.cache', '.idea', '.gradle',
|
|
43
44
|
]);
|
|
44
|
-
/**
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
/**
|
|
46
|
+
* 静态资源目录。同一个包在两种形态下路径不同,所以按候选顺序找第一个真实存在的:
|
|
47
|
+
* - 源码运行:src/server/ -> src/ui
|
|
48
|
+
* - 编译产物:dist/server/ -> dist/ui(构建时由 scripts/copy-ui.mjs 复制)
|
|
49
|
+
* - 兜底:包根 ui/ 或环境变量指定的目录(GUI 独立包用这个)
|
|
50
|
+
*/
|
|
51
|
+
export function resolveAssetsDir() {
|
|
52
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
53
|
+
const candidates = [
|
|
54
|
+
process.env.BANANA_UI_DIR,
|
|
55
|
+
join(here, '..', 'ui'),
|
|
56
|
+
join(here, '..', '..', 'ui'),
|
|
57
|
+
join(here, '..', '..', 'src', 'ui'),
|
|
58
|
+
].filter((item) => Boolean(item));
|
|
59
|
+
for (const candidate of candidates) {
|
|
60
|
+
if (existsSync(join(candidate, 'index.html')))
|
|
61
|
+
return candidate;
|
|
62
|
+
}
|
|
63
|
+
// 都找不到时返回默认位置,让 404 报错能指向一个具体路径
|
|
64
|
+
return join(here, '..', 'ui');
|
|
47
65
|
}
|
|
48
66
|
let activeAssetsDir = null;
|
|
49
67
|
function assetRoot() {
|
|
50
|
-
return activeAssetsDir ??
|
|
68
|
+
return activeAssetsDir ?? resolveAssetsDir();
|
|
51
69
|
}
|
|
52
70
|
/** 启动只监听回环地址的本地 GUI 服务;token 保护所有接口。 */
|
|
53
71
|
export async function startUiServer(options) {
|
|
@@ -55,7 +73,7 @@ export async function startUiServer(options) {
|
|
|
55
73
|
const token = randomBytes(24).toString('hex');
|
|
56
74
|
const host = options.host ?? '127.0.0.1';
|
|
57
75
|
const prefix = `/t/${token}`;
|
|
58
|
-
const assets = options.assetsDir ??
|
|
76
|
+
const assets = options.assetsDir ?? resolveAssetsDir();
|
|
59
77
|
activeAssetsDir = assets;
|
|
60
78
|
let turn = null;
|
|
61
79
|
const provider = () => createProvider(config.current, config);
|