workbuddy-switch 0.1.5 → 0.1.6

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.
Files changed (2) hide show
  1. package/package.json +7 -1
  2. package/scripts/install.js +39 -63
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "workbuddy-switch",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "WorkBuddy 账号切换工具:本地 webui(浏览器操作)+ 桌面 App(安装后运行 workbuddy-switch)",
5
5
  "bin": {
6
6
  "workbuddy-switch": "bin/workbuddy-switch.js"
@@ -9,6 +9,12 @@
9
9
  "bin/workbuddy-switch.js",
10
10
  "scripts"
11
11
  ],
12
+ "optionalDependencies": {
13
+ "workbuddy-switch-darwin-arm64": "0.1.6",
14
+ "workbuddy-switch-darwin-x64": "0.1.6",
15
+ "workbuddy-switch-win32-x64": "0.1.6",
16
+ "workbuddy-switch-linux-x64": "0.1.6"
17
+ },
12
18
  "scripts": {
13
19
  "postinstall": "node scripts/install.js"
14
20
  },
@@ -1,18 +1,13 @@
1
- // 下载源:GitHub Releases(changexbc/workbuddy-switch),可用环境变量覆盖:
1
+ // workbuddy-switch postinstall:从「平台包」复制本平台二进制。
2
+ //
3
+ // 平台分包(esbuild 模式):二进制发布在独立 npm 包(workbuddy-switch-<platform>-<arch>),
4
+ // 主包声明为 optionalDependencies,安装时 npm 自动装好平台包,postinstall 只需复制——
5
+ // 不依赖 GitHub,国内镜像(npmmirror)也能稳定安装。
6
+ //
7
+ // 环境变量覆盖:
2
8
  // WB_SWITCH_BINARY=<本地二进制路径> 本地开发/离线安装(直接复制,不联网)
3
- // WB_SWITCH_DOWNLOAD_BASE=<URL 前缀> 自定义下载源(默认 GitHub 带版本号的 release 资产)
4
9
  const fs = require("fs");
5
10
  const path = require("path");
6
- const os = require("os");
7
- const https = require("https");
8
-
9
- const OWNER = "changexbc"; // 发布二进制到 Release 的 GitHub 仓库
10
- const REPO = "workbuddy-switch";
11
-
12
- // 版本号从 package.json 读,下载地址用「带 tag」而非 latest,
13
- // 避免 GitHub CDN 对同名资产 latest 重定向的缓存污染(曾出现下载到旧二进制)。
14
- const PKG = require(path.join(__dirname, "..", "package.json"));
15
- const VERSION = PKG.version;
16
11
 
17
12
  const FILE = {
18
13
  "darwin-arm64": "wb-switch-darwin-arm64",
@@ -22,9 +17,11 @@ const FILE = {
22
17
  "linux-arm64": "wb-switch-linux-arm64",
23
18
  }[`${process.platform}-${process.arch}`];
24
19
 
20
+ const PLATFORM_PKG = `workbuddy-switch-${process.platform}-${process.arch}`;
21
+
25
22
  if (!FILE) {
26
23
  console.warn(
27
- `wb-switch: 跳过平台 ${process.platform}-${process.arch}(当前不支持),` +
24
+ `workbuddy-switch: 跳过平台 ${process.platform}-${process.arch}(当前不支持),` +
28
25
  `可手动下载二进制后放置到 bin/ 目录`,
29
26
  );
30
27
  process.exit(0);
@@ -34,72 +31,51 @@ const binDir = path.join(__dirname, "..", "bin");
34
31
  const target = path.join(binDir, FILE);
35
32
 
36
33
  function fail(msg) {
37
- console.error(`wb-switch install: ${msg}`);
38
- console.error("安装失败,可手动下载二进制放到 npm 包的 bin/ 目录。");
34
+ console.error(`workbuddy-switch install: ${msg}`);
35
+ console.error(
36
+ "安装失败。请确认安装了对应平台包(npm 会自动装),或设置 WB_SWITCH_BINARY 指向本地二进制。",
37
+ );
39
38
  process.exit(1);
40
39
  }
41
40
 
42
- function copyLocal(src) {
41
+ function copyFrom(src) {
43
42
  fs.mkdirSync(binDir, { recursive: true });
44
43
  fs.copyFileSync(src, target);
45
44
  if (process.platform !== "win32") fs.chmodSync(target, 0o755);
46
- console.log(`wb-switch: 已从 ${src} 复制二进制`);
47
- }
48
-
49
- function download(url) {
50
- fs.mkdirSync(binDir, { recursive: true });
51
- console.log(`wb-switch: 正在下载 ${url}`);
52
- const file = fs.createWriteStream(target);
53
- https
54
- .get(url, (res) => {
55
- if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
56
- file.close();
57
- return download(res.headers.location);
58
- }
59
- if (res.statusCode !== 200) {
60
- file.close();
61
- fs.unlinkSync(target);
62
- return fail(`下载失败 (HTTP ${res.statusCode})`);
63
- }
64
- res.pipe(file);
65
- file.on("finish", () => {
66
- file.close();
67
- if (process.platform !== "win32") fs.chmodSync(target, 0o755);
68
- // 校验:可执行文件应至少 1MB(防 CDN 缓存/截断导致下载到损坏或旧文件)
69
- const size = fs.statSync(target).size;
70
- if (size < 1 * 1024 * 1024) {
71
- fs.unlinkSync(target);
72
- return fail(`下载文件异常(仅 ${size} 字节),请重试或设置 WB_SWITCH_BINARY`);
73
- }
74
- console.log(`wb-switch: 二进制就绪 → ${target} (${(size / 1048576).toFixed(1)}MB)`);
75
- });
76
- })
77
- .on("error", (e) => {
78
- file.close();
79
- fs.unlinkSync(target);
80
- fail(`网络错误: ${e.message}`);
81
- });
45
+ const size = fs.statSync(target).size;
46
+ if (size < 1 * 1024 * 1024) {
47
+ fs.unlinkSync(target);
48
+ return fail(`平台包二进制异常(仅 ${size} 字节)`);
49
+ }
50
+ console.log(
51
+ `workbuddy-switch: 二进制就绪 ${target} (${(size / 1048576).toFixed(1)}MB)`,
52
+ );
82
53
  }
83
54
 
84
55
  async function main() {
85
56
  // 1) 本地二进制覆盖(开发/离线)
86
57
  if (process.env.WB_SWITCH_BINARY) {
87
58
  const local = path.resolve(process.env.WB_SWITCH_BINARY);
88
- if (fs.existsSync(local)) return copyLocal(local);
59
+ if (fs.existsSync(local)) return copyFrom(local);
89
60
  return fail(`WB_SWITCH_BINARY 指向的文件不存在: ${local}`);
90
61
  }
91
62
 
92
- // 2) 已存在则跳过
93
- if (fs.existsSync(target)) {
94
- console.log(`wb-switch: 二进制已存在,跳过下载`);
95
- return;
63
+ // 2) 从平台包复制(node_modules/workbuddy-switch-<platform>-<arch>/bin/<file>)
64
+ try {
65
+ const pkgRoot = path.dirname(require.resolve(`${PLATFORM_PKG}/package.json`));
66
+ const src = path.join(pkgRoot, "bin", FILE);
67
+ if (fs.existsSync(src)) return copyFrom(src);
68
+ return fail(`平台包 ${PLATFORM_PKG} 中未找到 ${FILE}`);
69
+ } catch (e) {
70
+ // 平台包缺失:可能是 optionalDependencies 没装上(如手动安装/旧版 npm)
71
+ if (fs.existsSync(target)) {
72
+ console.log("workbuddy-switch: 二进制已存在,跳过");
73
+ return;
74
+ }
75
+ return fail(
76
+ `平台包 ${PLATFORM_PKG} 未安装(${e.message})。请重新执行 npm install。`,
77
+ );
96
78
  }
97
-
98
- // 3) 从 GitHub Releases 下载(带版本号,避免 latest CDN 缓存)
99
- const base =
100
- process.env.WB_SWITCH_DOWNLOAD_BASE ||
101
- `https://github.com/${OWNER}/${REPO}/releases/download/v${VERSION}`;
102
- download(`${base}/${FILE}`);
103
79
  }
104
80
 
105
81
  main();