Skip to content

Commit c96ab4e

Browse files
committed
feat: bin subcommand for binary management
1 parent 3939c23 commit c96ab4e

File tree

2 files changed

+73
-32
lines changed

2 files changed

+73
-32
lines changed

src/index.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,47 @@ import { bin, install } from "./lib.js";
55
main();
66

77
async function main() {
8-
if (!fs.existsSync(bin)) {
9-
console.log("Installed cloudflared to " + (await install(bin)));
10-
}
11-
128
const args = process.argv.slice(2);
139

14-
if (args[0] === "remove-bin") {
15-
fs.unlinkSync(bin);
16-
console.log("Removed cloudflared");
17-
process.exit(0);
10+
if (args[0] === "bin") {
11+
if (!args[1]) {
12+
console.log(bin);
13+
return;
14+
}
15+
if (args[1] === "remove") {
16+
fs.unlinkSync(bin);
17+
console.log("Removed cloudflared");
18+
return;
19+
}
20+
if (args[1] === "install") {
21+
if (args[2]) {
22+
console.log(`Installing cloudflared ${args[2]}`);
23+
console.log(await install(bin, args[2]));
24+
} else {
25+
console.log("Installing latest version of cloudflared");
26+
await install(bin);
27+
}
28+
return;
29+
}
30+
if (args[1] === "help" || args[1] === "--help" || args[1] === "-h") {
31+
console.log(`cloudflared bin : Prints the path to the binary`);
32+
console.log(`cloudflared bin remove : Removes the binary`);
33+
console.log(`cloudflared bin install [version] : Installs the binary`);
34+
console.log(`cloudflared bin help : Prints this help message`);
35+
console.log(`Examples:`);
36+
console.log(
37+
`cloudflared bin install : Installs the latest version of cloudflared`,
38+
);
39+
console.log(`cloudflared bin install 2022.7.1 : Installs cloudflared 2022.7.1`);
40+
console.log(
41+
`You can find releases at https://github.com/cloudflare/cloudflared/releases`,
42+
);
43+
return;
44+
}
45+
}
46+
47+
if (!fs.existsSync(bin)) {
48+
console.log("Installed cloudflared to " + (await install(bin)));
1849
}
1950

2051
const sub = spawn(bin, args, { shell: true, stdio: "inherit" });

src/install.ts

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,89 @@ import path from "node:path";
33
import https from "node:https";
44
import { execSync } from "node:child_process";
55

6+
const RELEASE_BASE = "https://github.com/cloudflare/cloudflared/releases/";
7+
68
const LINUX_URL: Partial<Record<typeof process.arch, string>> = {
7-
arm64: "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-arm64",
8-
arm: "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-arm",
9-
x64: "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64",
10-
ia32: "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-386",
9+
arm64: "cloudflared-linux-arm64",
10+
arm: "cloudflared-linux-arm",
11+
x64: "cloudflared-linux-amd64",
12+
ia32: "cloudflared-linux-386",
1113
};
1214

1315
const MACOS_URL: Partial<Record<typeof process.arch, string>> = {
14-
arm64: "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-darwin-amd64.tgz",
15-
x64: "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-darwin-amd64.tgz",
16+
arm64: "cloudflared-darwin-amd64.tgz",
17+
x64: "cloudflared-darwin-amd64.tgz",
1618
};
1719

1820
const WINDOWS_URL: Partial<Record<typeof process.arch, string>> = {
19-
x64: "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-windows-amd64.exe",
20-
ia32: "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-windows-386.exe",
21+
x64: "cloudflared-windows-amd64.exe",
22+
ia32: "cloudflared-windows-386.exe",
2123
};
2224

25+
function resolve_base(version: string): string {
26+
if (version === "latest") {
27+
return `${RELEASE_BASE}latest/download/`;
28+
}
29+
return `${RELEASE_BASE}download/${version}/`;
30+
}
31+
2332
/**
2433
* Install cloudflared to the given path.
2534
* @param to The path to the binary to install.
35+
* @param version The version of cloudflared to install.
2636
* @returns The path to the binary that was installed.
2737
*/
28-
export async function install(to: string): Promise<string> {
38+
export async function install(to: string, version = "latest"): Promise<string> {
2939
if (process.platform === "linux") {
30-
return install_linux(to);
40+
return install_linux(to, version);
3141
} else if (process.platform === "darwin") {
32-
return install_macos(to);
42+
return install_macos(to, version);
3343
} else if (process.platform === "win32") {
34-
return install_windows(to);
44+
return install_windows(to, version);
3545
} else {
3646
console.error("Unsupported platform: " + process.platform);
3747
process.exit(1);
3848
}
3949
}
4050

41-
export async function install_linux(to: string): Promise<string> {
42-
const url = LINUX_URL[process.arch];
51+
export async function install_linux(to: string, version = "latest"): Promise<string> {
52+
const file = LINUX_URL[process.arch];
4353

44-
if (url === undefined) {
54+
if (file === undefined) {
4555
console.error("Unsupported architecture: " + process.arch);
4656
process.exit(1);
4757
}
4858

49-
await download(url, to);
59+
await download(resolve_base(version) + file, to);
5060
await new Promise((r) => setTimeout(r, 100));
5161
fs.chmodSync(to, "755");
5262
return to;
5363
}
5464

55-
export async function install_macos(to: string): Promise<string> {
56-
const url = MACOS_URL[process.arch];
65+
export async function install_macos(to: string, version = "latest"): Promise<string> {
66+
const file = MACOS_URL[process.arch];
5767

58-
if (url === undefined) {
68+
if (file === undefined) {
5969
console.error("Unsupported architecture: " + process.arch);
6070
process.exit(1);
6171
}
6272

63-
await download(url, `${to}.tgz`);
73+
await download(resolve_base(version) + file, `${to}.tgz`);
6474
process.env.VERBOSE && console.log(`Extracting to ${to}`);
6575
execSync(`tar -xzf ${path.basename(`${to}.tgz`)}`, { cwd: path.dirname(to) });
6676
fs.unlinkSync(`${to}.tgz`);
6777
fs.renameSync(`${path.dirname(to)}/cloudflared`, to);
6878
return to;
6979
}
70-
export async function install_windows(to: string): Promise<string> {
71-
const url = WINDOWS_URL[process.arch];
80+
export async function install_windows(to: string, version = "latest"): Promise<string> {
81+
const file = WINDOWS_URL[process.arch];
7282

73-
if (url === undefined) {
83+
if (file === undefined) {
7484
console.error("Unsupported architecture: " + process.arch);
7585
process.exit(1);
7686
}
7787

78-
await download(url, to);
88+
await download(resolve_base(version) + file, to);
7989
return to;
8090
}
8191

0 commit comments

Comments
 (0)