@vincentbanana/banana-gui 0.4.0 → 0.4.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/README.md +5 -4
- package/bin/banana-gui-desktop.mjs +46 -40
- package/bin/banana-gui.mjs +86 -18
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -18,13 +18,14 @@ banana-gui-desktop # 同上,等价的独立命令
|
|
|
18
18
|
|
|
19
19
|
electron 是可选依赖(`optionalDependencies`),npm 安装时会自动带上;万一某个平台装不上,`banana-gui` 仍然照常工作,只是 `--desktop` 会提示你用浏览器版。
|
|
20
20
|
|
|
21
|
-
>
|
|
21
|
+
> **关于 electron 的下载**:新版 electron 的二进制不在 `npm install` 时下载,而是**首次运行时**才拉取(一百多 MB),默认源是 GitHub,国内基本连不上。`banana-gui --desktop` 会自动改用国内镜像下载,你不用加任何参数。
|
|
22
|
+
>
|
|
23
|
+
> 想换源就设环境变量,例如:
|
|
22
24
|
>
|
|
23
25
|
> ```bash
|
|
24
|
-
>
|
|
26
|
+
> set ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/
|
|
27
|
+
> banana-gui --desktop
|
|
25
28
|
> ```
|
|
26
|
-
>
|
|
27
|
-
> 装完 `banana-gui --desktop` 就能开窗口了。
|
|
28
29
|
|
|
29
30
|
首次启动会进配置向导(选 provider、贴 API key、选权限、测连接),配置与 CLI 共用 `~/.banana/config.json`。
|
|
30
31
|
|
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
//
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
// banana-gui-desktop 开窗口
|
|
6
|
-
// banana-gui-desktop --port 8080
|
|
7
|
-
//
|
|
8
|
-
// electron 是 optionalDependencies:装得上就能用,装不上会提示改用浏览器版
|
|
9
|
-
// (banana-gui)。本文件不直接依赖 electron 的 API,只负责把它拉起来。
|
|
10
|
-
import { spawn } from 'node:child_process';
|
|
2
|
+
// 桌面版启动器:等价于 `banana-gui --desktop`,保留独立命令方便记忆。
|
|
3
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
4
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
11
5
|
import { createRequire } from 'node:module';
|
|
12
6
|
import { dirname, join } from 'node:path';
|
|
13
7
|
import { fileURLToPath } from 'node:url';
|
|
@@ -30,48 +24,60 @@ if (args.includes('--help') || args.includes('-h')) {
|
|
|
30
24
|
process.exit(0);
|
|
31
25
|
}
|
|
32
26
|
|
|
33
|
-
|
|
34
|
-
function electronBinary() {
|
|
35
|
-
for (const specifier of ['electron', '@vincentbanana/banana-gui/node_modules/electron']) {
|
|
36
|
-
try {
|
|
37
|
-
const resolved = require_.resolve(specifier);
|
|
38
|
-
// electron 主入口导出的是 exe 路径;解析失败时退回到包目录里的 dist
|
|
39
|
-
const binary = require_(resolved);
|
|
40
|
-
if (typeof binary === 'string') return binary;
|
|
41
|
-
return join(dirname(resolved), 'dist', process.platform === 'win32' ? 'electron.exe' : 'electron');
|
|
42
|
-
} catch {
|
|
43
|
-
/* 换下一个候选 */
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
return null;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const binary = electronBinary();
|
|
27
|
+
const binary = resolveElectron();
|
|
50
28
|
if (!binary) {
|
|
51
|
-
console.error('没找到 electron
|
|
52
|
-
console.error('');
|
|
53
|
-
console.error('可以先继续用浏览器版,功能完全一样:');
|
|
54
|
-
console.error(' banana-gui');
|
|
55
|
-
console.error('');
|
|
56
|
-
console.error('想用桌面版,试着补装一次:');
|
|
57
|
-
console.error(' npm install -g electron');
|
|
29
|
+
console.error('没找到 electron,桌面版用不了。装一次即可:npm install -g electron');
|
|
30
|
+
console.error('不装也没关系:banana-gui 用浏览器版,功能完全一样。');
|
|
58
31
|
process.exit(1);
|
|
59
32
|
}
|
|
60
33
|
|
|
61
34
|
const main = join(here, '..', 'desktop', 'main.cjs');
|
|
62
35
|
const child = spawn(binary, [main, ...args], {
|
|
63
36
|
stdio: 'inherit',
|
|
64
|
-
env: {
|
|
65
|
-
...process.env,
|
|
66
|
-
BANANA_UI_DIR: join(here, '..', 'ui'),
|
|
67
|
-
},
|
|
37
|
+
env: { ...process.env, BANANA_UI_DIR: join(here, '..', 'ui') },
|
|
68
38
|
});
|
|
69
39
|
child.on('error', (error) => {
|
|
70
|
-
console.error('
|
|
71
|
-
console.error('可以先用浏览器版:banana-gui');
|
|
40
|
+
console.error('启动失败:' + error.message);
|
|
72
41
|
process.exit(1);
|
|
73
42
|
});
|
|
74
43
|
child.on('exit', (code) => process.exit(code ?? 0));
|
|
75
44
|
for (const signal of ['SIGINT', 'SIGTERM']) {
|
|
76
45
|
process.on(signal, () => child.kill(signal));
|
|
77
|
-
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 找 electron 可执行文件;二进制缺失时用镜像补下载一次。
|
|
50
|
+
* 新版 electron 只在首次运行时下载一百多 MB 的二进制,默认源是 GitHub。
|
|
51
|
+
*/
|
|
52
|
+
function resolveElectron() {
|
|
53
|
+
let dir;
|
|
54
|
+
try {
|
|
55
|
+
dir = dirname(require_.resolve('electron'));
|
|
56
|
+
} catch {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
const pathTxt = join(dir, 'path.txt');
|
|
60
|
+
const distDir = join(dir, 'dist');
|
|
61
|
+
const current = () => {
|
|
62
|
+
try {
|
|
63
|
+
const name = readFileSync(pathTxt, 'utf8').trim();
|
|
64
|
+
const full = join(distDir, name || (process.platform === 'win32' ? 'electron.exe' : 'electron'));
|
|
65
|
+
return existsSync(full) ? full : null;
|
|
66
|
+
} catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const ready = current();
|
|
71
|
+
if (ready) return ready;
|
|
72
|
+
|
|
73
|
+
const installScript = join(dir, 'install.js');
|
|
74
|
+
if (!existsSync(installScript)) return null;
|
|
75
|
+
const mirror = process.env.ELECTRON_MIRROR ?? 'https://npmmirror.com/mirrors/electron/';
|
|
76
|
+
console.log('electron 的二进制还没下载,正在获取(一百多 MB,只需一次)...');
|
|
77
|
+
console.log(`下载源:${mirror}`);
|
|
78
|
+
const result = spawnSync(process.execPath, [installScript], {
|
|
79
|
+
stdio: 'inherit',
|
|
80
|
+
env: { ...process.env, ELECTRON_MIRROR: mirror },
|
|
81
|
+
});
|
|
82
|
+
return result.status === 0 ? current() : null;
|
|
83
|
+
}
|
package/bin/banana-gui.mjs
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
// Banana GUI 启动器:复用 CLI 包的 agent 内核,用本包自带的界面资源。
|
|
3
3
|
// 安装:npm install -g @vincentbanana/banana-gui
|
|
4
4
|
// 启动:banana-gui(首次会进配置向导)
|
|
5
|
+
import { spawn, spawnSync } from 'node:child_process';
|
|
6
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
5
7
|
import { createRequire } from 'node:module';
|
|
6
|
-
import { spawn } from 'node:child_process';
|
|
7
8
|
import { dirname, join } from 'node:path';
|
|
8
9
|
import { fileURLToPath } from 'node:url';
|
|
9
10
|
|
|
@@ -13,25 +14,94 @@ const here = dirname(fileURLToPath(import.meta.url));
|
|
|
13
14
|
|
|
14
15
|
/** CLI 内核入口。依赖可能被 npm 提升到上层 node_modules,用 require 解析更稳。 */
|
|
15
16
|
function resolveCli() {
|
|
17
|
+
const candidates = [];
|
|
18
|
+
for (const specifier of ['@vincentbanana/bananacli']) {
|
|
19
|
+
// 先在本包的依赖树里找(版本对得上),再退回到被提升到上层的副本
|
|
20
|
+
for (const paths of [[join(here, '..')], undefined]) {
|
|
21
|
+
try {
|
|
22
|
+
const entry = paths ? require.resolve(specifier, { paths }) : require.resolve(specifier);
|
|
23
|
+
candidates.push(entry);
|
|
24
|
+
} catch {
|
|
25
|
+
/* 换下一个候选 */
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (!candidates.length) return null;
|
|
30
|
+
|
|
31
|
+
/** 读内核版本:entry 是 dist/index.js,同目录上一级就是包根 */
|
|
32
|
+
const versionOf = (entry) => {
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(readFileSync(join(dirname(entry), '..', 'package.json'), 'utf8')).version;
|
|
35
|
+
} catch {
|
|
36
|
+
return '0.0.0';
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const required = String(pkg.dependencies?.['@vincentbanana/bananacli'] ?? '').replace(/^[^\d]*/, '') || '0.0.0';
|
|
40
|
+
const newer = (a, b) => {
|
|
41
|
+
const pa = a.split('.').map(Number);
|
|
42
|
+
const pb = b.split('.').map(Number);
|
|
43
|
+
for (let i = 0; i < 3; i += 1) {
|
|
44
|
+
if ((pa[i] ?? 0) !== (pb[i] ?? 0)) return (pa[i] ?? 0) > (pb[i] ?? 0);
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
};
|
|
48
|
+
const best = candidates.sort((a, b) => (newer(versionOf(a), versionOf(b)) ? -1 : 1))[0];
|
|
49
|
+
const found = versionOf(best);
|
|
50
|
+
if (newer(required, found)) {
|
|
51
|
+
// 旧内核没有内置界面资源,界面会 404。与其让用户看白屏,不如直接说清。
|
|
52
|
+
console.error(`内核 @vincentbanana/bananacli 版本过旧(当前 ${found},需要 ${required} 以上)。`);
|
|
53
|
+
console.error('旧版本不含界面资源,打开会是空白页。先更新内核:');
|
|
54
|
+
console.error(' npm install -g @vincentbanana/bananacli');
|
|
55
|
+
console.error('然后再运行 banana-gui。');
|
|
56
|
+
process.exit(1);
|
|
57
|
+
}
|
|
58
|
+
return best;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 找到 electron 可执行文件。electron 是个可选依赖,而且新版只在**首次运行**才下载
|
|
63
|
+
* 一百多 MB 的二进制(npm install 时只装壳)。默认从 GitHub 下,国内基本连不上,
|
|
64
|
+
* 所以这里主动用镜像补下载一次,用户不用记任何参数。
|
|
65
|
+
* 想换源:设 ELECTRON_MIRROR 环境变量即可。
|
|
66
|
+
*/
|
|
67
|
+
function resolveElectron() {
|
|
68
|
+
let dir;
|
|
16
69
|
try {
|
|
17
|
-
|
|
70
|
+
dir = dirname(require.resolve('electron'));
|
|
18
71
|
} catch {
|
|
19
72
|
return null;
|
|
20
73
|
}
|
|
21
|
-
}
|
|
22
74
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
75
|
+
const pathTxt = join(dir, 'path.txt');
|
|
76
|
+
const distDir = join(dir, 'dist');
|
|
77
|
+
const current = () => {
|
|
26
78
|
try {
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
if (typeof binary === 'string') return binary;
|
|
79
|
+
const name = readFileSync(pathTxt, 'utf8').trim();
|
|
80
|
+
const full = join(distDir, name || (process.platform === 'win32' ? 'electron.exe' : 'electron'));
|
|
81
|
+
return existsSync(full) ? full : null;
|
|
31
82
|
} catch {
|
|
32
|
-
|
|
83
|
+
return null;
|
|
33
84
|
}
|
|
34
|
-
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const ready = current();
|
|
88
|
+
if (ready) return ready;
|
|
89
|
+
|
|
90
|
+
const installScript = join(dir, 'install.js');
|
|
91
|
+
if (!existsSync(installScript)) return null;
|
|
92
|
+
|
|
93
|
+
const mirror = process.env.ELECTRON_MIRROR ?? 'https://npmmirror.com/mirrors/electron/';
|
|
94
|
+
console.log('electron 的二进制还没下载,正在获取(一百多 MB,只需一次)...');
|
|
95
|
+
console.log(`下载源:${mirror}`);
|
|
96
|
+
const result = spawnSync(process.execPath, [installScript], {
|
|
97
|
+
stdio: 'inherit',
|
|
98
|
+
env: { ...process.env, ELECTRON_MIRROR: mirror },
|
|
99
|
+
});
|
|
100
|
+
if (result.status === 0) return current();
|
|
101
|
+
console.error('');
|
|
102
|
+
console.error('下载失败。可以换个网络或换个源再试,比如:');
|
|
103
|
+
console.error(' set ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/');
|
|
104
|
+
console.error(' banana-gui --desktop');
|
|
35
105
|
return null;
|
|
36
106
|
}
|
|
37
107
|
|
|
@@ -46,7 +116,7 @@ if (args.includes('--help') || args.includes('-h')) {
|
|
|
46
116
|
console.log('banana-gui — Banana 的图形界面');
|
|
47
117
|
console.log('');
|
|
48
118
|
console.log('用法: banana-gui [选项]');
|
|
49
|
-
console.log(' --desktop
|
|
119
|
+
console.log(' --desktop 用独立窗口打开(首次会下载 electron 运行时)');
|
|
50
120
|
console.log(' --port <端口> 指定端口(默认随机)');
|
|
51
121
|
console.log(' --cwd <目录> 指定工作区');
|
|
52
122
|
console.log(' --provider <名> 指定 provider');
|
|
@@ -80,12 +150,10 @@ function kernelArgs() {
|
|
|
80
150
|
}
|
|
81
151
|
|
|
82
152
|
if (args.includes('--desktop')) {
|
|
83
|
-
|
|
84
|
-
const binary = electronBinary();
|
|
153
|
+
const binary = resolveElectron();
|
|
85
154
|
if (!binary) {
|
|
86
|
-
console.error('
|
|
87
|
-
console.error(' npm install -g electron
|
|
88
|
-
console.error('(国内网络必须加镜像参数,GitHub 连不上会装不了)');
|
|
155
|
+
console.error('没找到 electron,用不了独立窗口。装一次即可:');
|
|
156
|
+
console.error(' npm install -g electron');
|
|
89
157
|
console.error('不装也没关系:直接运行 banana-gui 用浏览器版,功能完全一样。');
|
|
90
158
|
process.exit(1);
|
|
91
159
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vincentbanana/banana-gui",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "Banana 的图形界面:本地服务 + 浏览器。黑白主题,自绘图标,无第三方 UI 依赖",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -71,7 +71,7 @@
|
|
|
71
71
|
"electron-builder": "^25.0.0"
|
|
72
72
|
},
|
|
73
73
|
"dependencies": {
|
|
74
|
-
"@vincentbanana/bananacli": "^0.4.
|
|
74
|
+
"@vincentbanana/bananacli": "^0.4.2"
|
|
75
75
|
},
|
|
76
76
|
"license": "MIT",
|
|
77
77
|
"publishConfig": {
|