api-switch-cc 0.3.0 → 0.3.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/api-switch.js +70 -46
- package/install.js +1 -1
- package/package.json +1 -1
package/api-switch.js
CHANGED
|
@@ -3,11 +3,10 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* API-Switch — lazy-install launcher
|
|
5
5
|
*
|
|
6
|
-
* On first run, installs the api-switch binary
|
|
7
|
-
* then execs it with the original arguments.
|
|
6
|
+
* On first run, installs the api-switch binary, then execs it.
|
|
8
7
|
* Subsequent runs use the cached binary directly.
|
|
9
8
|
*
|
|
10
|
-
*
|
|
9
|
+
* Install order: GitHub release → Gitee clone+build → go install
|
|
11
10
|
*/
|
|
12
11
|
|
|
13
12
|
"use strict";
|
|
@@ -16,9 +15,10 @@ const { existsSync, mkdirSync, chmodSync, unlinkSync, copyFileSync } = require("
|
|
|
16
15
|
const { join } = require("path");
|
|
17
16
|
const { execSync, spawnSync } = require("child_process");
|
|
18
17
|
|
|
19
|
-
const VERSION = "v0.
|
|
18
|
+
const VERSION = "v0.3.0";
|
|
20
19
|
const BIN_DIR = join(__dirname, ".bin");
|
|
21
|
-
const
|
|
20
|
+
const IS_WIN = process.platform === "win32";
|
|
21
|
+
const BIN_NAME = IS_WIN ? "api-switch.exe" : "api-switch";
|
|
22
22
|
const BIN_PATH = join(BIN_DIR, BIN_NAME);
|
|
23
23
|
|
|
24
24
|
const GITEE_REPO = "https://gitee.com/776311606/API-Switch.git";
|
|
@@ -33,71 +33,95 @@ function platform() {
|
|
|
33
33
|
return map[`${process.platform}-${process.arch}`];
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
function showManualSteps() {
|
|
37
|
+
console.error();
|
|
38
|
+
console.error("Installation failed. Try manually:");
|
|
39
|
+
console.error();
|
|
40
|
+
|
|
41
|
+
if (IS_WIN) {
|
|
42
|
+
console.error(" # Download the .exe and add to PATH");
|
|
43
|
+
console.error(` # URL: ${GH_RELEASE}/api-switch-windows-amd64.exe`);
|
|
44
|
+
console.error(" # Save to: C:\\Users\\" + process.env.USERNAME + "\\AppData\\Roaming\\npm\\api-switch.cmd");
|
|
45
|
+
console.error();
|
|
46
|
+
console.error(" # Or write a .cmd wrapper:");
|
|
47
|
+
console.error(' echo @node "%~dp0node_modules\\api-switch-cc\\api-switch.js" %%* > %APPDATA%\\npm\\api-switch.cmd');
|
|
48
|
+
} else {
|
|
49
|
+
console.error(" git clone --depth=1 " + GITEE_REPO);
|
|
50
|
+
console.error(" cd API-Switch && GOTOOLCHAIN=local GOPROXY=https://goproxy.cn,direct go build -o api-switch ./cmd/api-switch/");
|
|
51
|
+
console.error(" cp api-switch /usr/local/bin/");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
37
55
|
function ensureInstalled() {
|
|
38
56
|
if (existsSync(BIN_PATH)) return;
|
|
39
57
|
|
|
40
58
|
mkdirSync(BIN_DIR, { recursive: true });
|
|
41
59
|
console.log("First run — installing api-switch...");
|
|
42
60
|
|
|
43
|
-
// 1.
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
61
|
+
// 1. GitHub release download (fastest, works on all platforms)
|
|
62
|
+
const plat = platform();
|
|
63
|
+
if (plat) {
|
|
64
|
+
try {
|
|
65
|
+
const archive = `api-switch-${plat}${IS_WIN ? ".exe" : ".tar.gz"}`;
|
|
66
|
+
const url = `${GH_RELEASE}/${archive}`;
|
|
67
|
+
const tmp = join(__dirname, IS_WIN ? "_.tmp.exe" : "_.tmp.tar.gz");
|
|
68
|
+
execSync(
|
|
69
|
+
IS_WIN
|
|
70
|
+
? `powershell -Command "Invoke-WebRequest '${url}' -OutFile '${tmp}'"`
|
|
71
|
+
: `curl -sSL --connect-timeout 10 --max-time 120 "${url}" -o "${tmp}"`,
|
|
72
|
+
{ stdio: "pipe", timeout: 150000 }
|
|
73
|
+
);
|
|
74
|
+
if (IS_WIN) {
|
|
75
|
+
execSync(`move /Y "${tmp}" "${BIN_PATH}"`, { stdio: "pipe", shell: true });
|
|
76
|
+
} else {
|
|
77
|
+
execSync(`tar xzf "${tmp}" -C "${BIN_DIR}" "${archive}"`, { stdio: "pipe" });
|
|
78
|
+
execSync(`mv "${join(BIN_DIR, archive)}" "${BIN_PATH}"`, { stdio: "pipe" });
|
|
79
|
+
unlinkSync(tmp);
|
|
80
|
+
}
|
|
50
81
|
chmodSync(BIN_PATH, 0o755);
|
|
51
|
-
console.log("Installed via
|
|
82
|
+
console.log("Installed via GitHub.");
|
|
52
83
|
return;
|
|
84
|
+
} catch (e) {
|
|
85
|
+
console.error(" GitHub download failed: " + e.message.split("\n")[0].slice(0, 80));
|
|
53
86
|
}
|
|
54
|
-
}
|
|
87
|
+
}
|
|
55
88
|
|
|
56
|
-
// 2.
|
|
89
|
+
// 2. Gitee clone + build (requires Go + Git)
|
|
57
90
|
try {
|
|
58
|
-
console.log("Trying Gitee mirror...");
|
|
91
|
+
console.log("Trying Gitee mirror (needs Go + Git)...");
|
|
59
92
|
const tmp = join(__dirname, ".gitee-build");
|
|
60
93
|
execSync(`rm -rf ${tmp} && git clone --depth=1 ${GITEE_REPO} ${tmp}`, { stdio: "pipe", timeout: 60000 });
|
|
61
|
-
execSync(
|
|
94
|
+
execSync(
|
|
95
|
+
`cd ${tmp} && GOTOOLCHAIN=local GOPROXY=https://goproxy.cn,direct go build -ldflags="-s -w -X main.Version=${VERSION}" -o "${BIN_PATH}" ./cmd/api-switch/`,
|
|
96
|
+
{ stdio: "pipe", timeout: 120000 }
|
|
97
|
+
);
|
|
62
98
|
execSync(`rm -rf ${tmp}`, { stdio: "pipe" });
|
|
63
99
|
chmodSync(BIN_PATH, 0o755);
|
|
64
|
-
console.log("Installed via Gitee
|
|
100
|
+
console.log("Installed via Gitee.");
|
|
65
101
|
return;
|
|
66
102
|
} catch (_) {}
|
|
67
103
|
|
|
68
|
-
// 3.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
execSync(`unzip -o "${tmp}" "api-switch-${plat}.exe"`, { stdio: "pipe" });
|
|
80
|
-
renameSync(`api-switch-${plat}.exe`, BIN_PATH);
|
|
81
|
-
} else {
|
|
82
|
-
execSync(`tar xzf "${tmp}" -C "${BIN_DIR}" "api-switch-${plat}"`, { stdio: "pipe" });
|
|
83
|
-
execSync(`mv "${join(BIN_DIR, 'api-switch-' + plat)}" "${BIN_PATH}"`, { stdio: "pipe" });
|
|
84
|
-
}
|
|
85
|
-
unlinkSync(tmp);
|
|
104
|
+
// 3. go install (requires Go)
|
|
105
|
+
try {
|
|
106
|
+
console.log("Trying go install (needs Go)...");
|
|
107
|
+
const gopath = (execSync("go env GOPATH", { encoding: "utf8", stdio: "pipe" }) || "~/go").trim();
|
|
108
|
+
const goBin = join(gopath, "bin", BIN_NAME);
|
|
109
|
+
execSync(
|
|
110
|
+
`GOPROXY=https://goproxy.cn,direct GOTOOLCHAIN=local go install github.com/hxz0727/API-Switch/cmd/api-switch@${VERSION}`,
|
|
111
|
+
{ stdio: "pipe", timeout: 120000 }
|
|
112
|
+
);
|
|
113
|
+
if (existsSync(goBin)) {
|
|
114
|
+
copyFileSync(goBin, BIN_PATH);
|
|
86
115
|
chmodSync(BIN_PATH, 0o755);
|
|
87
|
-
console.log("Installed via
|
|
116
|
+
console.log("Installed via go install.");
|
|
88
117
|
return;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
118
|
+
}
|
|
119
|
+
} catch (_) {}
|
|
91
120
|
|
|
92
|
-
|
|
93
|
-
console.error("Installation failed. Run manually:");
|
|
94
|
-
console.error(` git clone --depth=1 ${GITEE_REPO}`);
|
|
95
|
-
console.error(" cd API-Switch && GOTOOLCHAIN=local GOPROXY=https://goproxy.cn,direct go build -o api-switch ./cmd/api-switch/");
|
|
96
|
-
console.error(" cp api-switch /usr/local/bin/");
|
|
121
|
+
showManualSteps();
|
|
97
122
|
process.exit(1);
|
|
98
123
|
}
|
|
99
124
|
|
|
100
|
-
// ── Entry ──
|
|
101
125
|
ensureInstalled();
|
|
102
126
|
const r = spawnSync(BIN_PATH, process.argv.slice(2), { stdio: "inherit" });
|
|
103
127
|
process.exit(r.status ?? 1);
|
package/install.js
CHANGED
|
@@ -20,7 +20,7 @@ const { join } = require("path");
|
|
|
20
20
|
const { execSync } = require("child_process");
|
|
21
21
|
|
|
22
22
|
const PKG = require("./package.json");
|
|
23
|
-
const DOWNLOAD_VERSION = "v0.
|
|
23
|
+
const DOWNLOAD_VERSION = "v0.3.0";
|
|
24
24
|
const GH_RELEASE = `https://github.com/hxz0727/API-Switch/releases/download/${DOWNLOAD_VERSION}`;
|
|
25
25
|
const GITEE_REPO = "https://gitee.com/776311606/API-Switch.git";
|
|
26
26
|
const BIN_DIR = join(__dirname, "bin");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "api-switch-cc",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Claude Code 多模型代理 — 一键切换 DeepSeek、Qwen、GLM、Moonshot 等国产大模型,协议自动转换,零配置即用。LLM API proxy for Claude Code with DeepSeek, Qwen, Anthropic, OpenAI support.",
|
|
5
5
|
"bin": {
|
|
6
6
|
"api-switch": "api-switch.js"
|