@youdie006/swapdex 0.1.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/bin/swapdex.js +18 -0
- package/install.js +75 -0
- package/package.json +39 -0
package/bin/swapdex.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Thin shim: exec the prebuilt binary that install.js placed next to this file,
|
|
3
|
+
// forwarding argv, stdio, and the exit code.
|
|
4
|
+
|
|
5
|
+
const path = require("path");
|
|
6
|
+
const { spawnSync } = require("child_process");
|
|
7
|
+
|
|
8
|
+
const bin = path.join(__dirname, "swapdex");
|
|
9
|
+
|
|
10
|
+
const result = spawnSync(bin, process.argv.slice(2), { stdio: "inherit" });
|
|
11
|
+
|
|
12
|
+
if (result.error) {
|
|
13
|
+
console.error(
|
|
14
|
+
"swapdex: prebuilt binary not found. Reinstall the package, or use `cargo install swapdex`."
|
|
15
|
+
);
|
|
16
|
+
process.exit(1);
|
|
17
|
+
}
|
|
18
|
+
process.exit(result.status === null ? 1 : result.status);
|
package/install.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Postinstall: download the prebuilt swapdex binary for this platform from the
|
|
3
|
+
// matching GitHub release and place it next to the JS shim. Never fails the npm
|
|
4
|
+
// install hard - on any problem it prints a hint to use `cargo install swapdex`
|
|
5
|
+
// and exits 0, so the package install does not break.
|
|
6
|
+
|
|
7
|
+
const fs = require("fs");
|
|
8
|
+
const os = require("os");
|
|
9
|
+
const path = require("path");
|
|
10
|
+
const https = require("https");
|
|
11
|
+
const { execFileSync } = require("child_process");
|
|
12
|
+
|
|
13
|
+
const version = require("./package.json").version;
|
|
14
|
+
|
|
15
|
+
// swapdex is a Unix tool (Linux, WSL, macOS) - it manages 0600 credential files.
|
|
16
|
+
const TARGETS = {
|
|
17
|
+
"darwin arm64": "aarch64-apple-darwin",
|
|
18
|
+
"darwin x64": "x86_64-apple-darwin",
|
|
19
|
+
"linux x64": "x86_64-unknown-linux-musl",
|
|
20
|
+
"linux arm64": "aarch64-unknown-linux-musl",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const key = `${process.platform} ${process.arch}`;
|
|
24
|
+
const target = TARGETS[key];
|
|
25
|
+
const binDir = path.join(__dirname, "bin");
|
|
26
|
+
const binName = "swapdex";
|
|
27
|
+
|
|
28
|
+
function bail(msg) {
|
|
29
|
+
console.error(`swapdex: ${msg} Try \`cargo install swapdex\` instead.`);
|
|
30
|
+
process.exit(0); // do not break the whole npm install
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!target) bail(`no prebuilt binary for ${key} (swapdex supports Linux, WSL, and macOS).`);
|
|
34
|
+
|
|
35
|
+
const ext = "tar.gz";
|
|
36
|
+
const url = `https://github.com/youdie006/swapdex/releases/download/v${version}/swapdex-${target}.${ext}`;
|
|
37
|
+
|
|
38
|
+
function download(u, dest, cb, redirects = 0) {
|
|
39
|
+
https
|
|
40
|
+
.get(u, { headers: { "User-Agent": "swapdex-npm" } }, (res) => {
|
|
41
|
+
if (
|
|
42
|
+
[301, 302, 307, 308].includes(res.statusCode) &&
|
|
43
|
+
res.headers.location &&
|
|
44
|
+
redirects < 5
|
|
45
|
+
) {
|
|
46
|
+
res.resume();
|
|
47
|
+
return download(res.headers.location, dest, cb, redirects + 1);
|
|
48
|
+
}
|
|
49
|
+
if (res.statusCode !== 200) {
|
|
50
|
+
return cb(new Error(`HTTP ${res.statusCode}`));
|
|
51
|
+
}
|
|
52
|
+
const f = fs.createWriteStream(dest);
|
|
53
|
+
res.pipe(f);
|
|
54
|
+
f.on("finish", () => f.close(() => cb(null)));
|
|
55
|
+
f.on("error", cb);
|
|
56
|
+
})
|
|
57
|
+
.on("error", cb);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
fs.mkdirSync(binDir, { recursive: true });
|
|
61
|
+
const archive = path.join(os.tmpdir(), `swapdex-${target}.${ext}`);
|
|
62
|
+
|
|
63
|
+
download(url, archive, (err) => {
|
|
64
|
+
if (err) bail(`could not download the prebuilt binary (${err.message}).`);
|
|
65
|
+
try {
|
|
66
|
+
execFileSync("tar", ["-xzf", archive, "-C", binDir], { stdio: "ignore" });
|
|
67
|
+
const bin = path.join(binDir, binName);
|
|
68
|
+
if (!fs.existsSync(bin)) bail("the downloaded archive did not contain the binary.");
|
|
69
|
+
fs.chmodSync(bin, 0o755);
|
|
70
|
+
fs.unlinkSync(archive);
|
|
71
|
+
console.log(`swapdex: installed prebuilt binary (${target}).`);
|
|
72
|
+
} catch (e) {
|
|
73
|
+
bail(`failed to extract the binary (${e.message}).`);
|
|
74
|
+
}
|
|
75
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@youdie006/swapdex",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Switch between multiple Claude Code and Codex login accounts, locally and safely.",
|
|
5
|
+
"bin": {
|
|
6
|
+
"swapdex": "bin/swapdex.js"
|
|
7
|
+
},
|
|
8
|
+
"scripts": {
|
|
9
|
+
"postinstall": "node install.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"bin/swapdex.js",
|
|
13
|
+
"install.js"
|
|
14
|
+
],
|
|
15
|
+
"keywords": [
|
|
16
|
+
"cli",
|
|
17
|
+
"claude",
|
|
18
|
+
"codex",
|
|
19
|
+
"accounts",
|
|
20
|
+
"credentials"
|
|
21
|
+
],
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/youdie006/swapdex.git"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/youdie006/swapdex#readme",
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=16"
|
|
30
|
+
},
|
|
31
|
+
"os": [
|
|
32
|
+
"darwin",
|
|
33
|
+
"linux"
|
|
34
|
+
],
|
|
35
|
+
"cpu": [
|
|
36
|
+
"x64",
|
|
37
|
+
"arm64"
|
|
38
|
+
]
|
|
39
|
+
}
|