@babelx/cli 0.2.1 → 0.2.3
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/bx +110 -15
- package/package.json +1 -1
- package/scripts/postinstall.js +21 -69
package/bin/bx
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
* BabelX CLI - Binary wrapper
|
|
4
|
-
*
|
|
3
|
+
* BabelX CLI - Binary wrapper with lazy download
|
|
4
|
+
* Downloads the appropriate binary on first run
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { spawn } from "node:child_process";
|
|
8
|
-
import { existsSync } from "node:fs";
|
|
8
|
+
import { createWriteStream, existsSync, mkdirSync } from "node:fs";
|
|
9
9
|
import { join, dirname } from "node:path";
|
|
10
10
|
import { fileURLToPath } from "node:url";
|
|
11
11
|
import { platform as getPlatformName, arch as getArch } from "node:os";
|
|
12
|
+
import { get } from "node:https";
|
|
12
13
|
|
|
13
14
|
const __filename = fileURLToPath(import.meta.url);
|
|
14
15
|
const __dirname = dirname(__filename);
|
|
15
16
|
|
|
17
|
+
const GITHUB_REPO = "babelxdev/bx-cli";
|
|
18
|
+
|
|
16
19
|
function getPlatform() {
|
|
17
20
|
const platform = getPlatformName();
|
|
18
21
|
const arch = getArch();
|
|
@@ -32,33 +35,125 @@ function getPlatform() {
|
|
|
32
35
|
const a = archMap[arch];
|
|
33
36
|
|
|
34
37
|
if (!p || !a) {
|
|
35
|
-
console.error(
|
|
38
|
+
console.error(`❌ Unsupported platform: ${platform} ${arch}`);
|
|
36
39
|
process.exit(1);
|
|
37
40
|
}
|
|
38
41
|
|
|
39
42
|
return `${p}-${a}`;
|
|
40
43
|
}
|
|
41
44
|
|
|
45
|
+
function getBinaryName() {
|
|
46
|
+
return process.platform === "win32" ? "bx.exe" : "bx";
|
|
47
|
+
}
|
|
48
|
+
|
|
42
49
|
function getBinaryPath() {
|
|
43
50
|
const platform = getPlatform();
|
|
44
51
|
const binDir = join(__dirname, "..", "vendor");
|
|
45
|
-
const binName =
|
|
46
|
-
|
|
52
|
+
const binName = getBinaryName();
|
|
53
|
+
return join(binDir, platform, binName);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function downloadBinary(platform, destPath) {
|
|
57
|
+
const binaryName = getBinaryName();
|
|
58
|
+
const packageJson = JSON.parse(
|
|
59
|
+
await import("node:fs").then((fs) =>
|
|
60
|
+
fs.readFileSync(join(__dirname, "..", "package.json"), "utf-8"),
|
|
61
|
+
),
|
|
62
|
+
);
|
|
63
|
+
const version = packageJson.version;
|
|
64
|
+
const url = `https://github.com/${GITHUB_REPO}/releases/download/v${version}/bx-${platform}${process.platform === "win32" ? ".exe" : ""}`;
|
|
65
|
+
|
|
66
|
+
console.log(`📦 First time setup: Downloading BabelX CLI v${version}`);
|
|
67
|
+
console.log(` Platform: ${platform}`);
|
|
68
|
+
console.log("");
|
|
69
|
+
|
|
70
|
+
// Ensure directory exists
|
|
71
|
+
const dir = dirname(destPath);
|
|
72
|
+
if (!existsSync(dir)) {
|
|
73
|
+
mkdirSync(dir, { recursive: true });
|
|
74
|
+
}
|
|
47
75
|
|
|
48
|
-
return
|
|
76
|
+
return new Promise((resolve, reject) => {
|
|
77
|
+
const file = createWriteStream(destPath);
|
|
78
|
+
let downloaded = 0;
|
|
79
|
+
let total = 0;
|
|
80
|
+
let lastPercent = -1;
|
|
81
|
+
|
|
82
|
+
function formatBytes(bytes) {
|
|
83
|
+
if (bytes === 0) return "0 B";
|
|
84
|
+
const k = 1024;
|
|
85
|
+
const sizes = ["B", "KB", "MB", "GB"];
|
|
86
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
87
|
+
return `${(bytes / k ** i).toFixed(1)} ${sizes[i]}`;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function updateProgress() {
|
|
91
|
+
if (total === 0) return;
|
|
92
|
+
const percent = Math.floor((downloaded / total) * 100);
|
|
93
|
+
if (percent !== lastPercent && percent % 5 === 0) {
|
|
94
|
+
const bar = "█".repeat(Math.floor(percent / 5)) + "░".repeat(20 - Math.floor(percent / 5));
|
|
95
|
+
process.stdout.write(`\r ${bar} ${percent}% (${formatBytes(downloaded)} / ${formatBytes(total)})`);
|
|
96
|
+
lastPercent = percent;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function handleResponse(response) {
|
|
101
|
+
total = parseInt(response.headers["content-length"] || "0", 10);
|
|
102
|
+
|
|
103
|
+
response.on("data", (chunk) => {
|
|
104
|
+
downloaded += chunk.length;
|
|
105
|
+
updateProgress();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
response.pipe(file);
|
|
109
|
+
|
|
110
|
+
file.on("finish", () => {
|
|
111
|
+
process.stdout.write("\n");
|
|
112
|
+
file.close();
|
|
113
|
+
resolve();
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
get(url, { followRedirects: true }, (response) => {
|
|
118
|
+
if (response.statusCode === 302 || response.statusCode === 301) {
|
|
119
|
+
get(response.headers.location, handleResponse).on("error", reject);
|
|
120
|
+
} else if (response.statusCode === 200) {
|
|
121
|
+
handleResponse(response);
|
|
122
|
+
} else {
|
|
123
|
+
reject(
|
|
124
|
+
new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`),
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}).on("error", reject);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function makeExecutable(filePath) {
|
|
132
|
+
if (process.platform !== "win32") {
|
|
133
|
+
import("node:fs").then((fs) => fs.chmodSync(filePath, 0o755));
|
|
134
|
+
}
|
|
49
135
|
}
|
|
50
136
|
|
|
51
|
-
function main() {
|
|
137
|
+
async function main() {
|
|
52
138
|
const binaryPath = getBinaryPath();
|
|
139
|
+
const platform = getPlatform();
|
|
53
140
|
|
|
54
|
-
//
|
|
141
|
+
// Download binary if it doesn't exist (lazy loading)
|
|
55
142
|
if (!existsSync(binaryPath)) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
143
|
+
try {
|
|
144
|
+
await downloadBinary(platform, binaryPath);
|
|
145
|
+
makeExecutable(binaryPath);
|
|
146
|
+
console.log(`✅ BabelX CLI installed successfully!\n`);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
console.error("❌ Failed to download BabelX CLI binary.");
|
|
149
|
+
console.error(`Error: ${error.message}`);
|
|
150
|
+
console.error("");
|
|
151
|
+
console.error("Please try:");
|
|
152
|
+
console.error("1. Check your internet connection");
|
|
153
|
+
console.error("2. Download manually from:");
|
|
154
|
+
console.error(` https://github.com/${GITHUB_REPO}/releases`);
|
|
155
|
+
process.exit(1);
|
|
156
|
+
}
|
|
62
157
|
}
|
|
63
158
|
|
|
64
159
|
// Run the binary with all arguments
|
package/package.json
CHANGED
package/scripts/postinstall.js
CHANGED
|
@@ -1,23 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
* Post-install script for @babelx/cli
|
|
4
|
-
*
|
|
3
|
+
* Post-install script for @babelx/cli (optional)
|
|
4
|
+
* Pre-downloads the binary during installation (if allowed)
|
|
5
|
+
* If this fails, the CLI will download lazily on first run
|
|
5
6
|
*/
|
|
6
7
|
|
|
7
|
-
import {
|
|
8
|
-
chmodSync,
|
|
9
|
-
createWriteStream,
|
|
10
|
-
existsSync,
|
|
11
|
-
mkdirSync,
|
|
12
|
-
readFileSync,
|
|
13
|
-
} from "node:fs";
|
|
8
|
+
import { createWriteStream, existsSync, mkdirSync } from "node:fs";
|
|
14
9
|
import { get } from "node:https";
|
|
15
10
|
import { dirname, join } from "node:path";
|
|
16
11
|
import { fileURLToPath } from "node:url";
|
|
17
12
|
|
|
18
13
|
const GITHUB_REPO = "babelxdev/bx-cli";
|
|
19
14
|
|
|
20
|
-
// ES modules don't have __dirname
|
|
21
15
|
const __filename = fileURLToPath(import.meta.url);
|
|
22
16
|
const __dirname = dirname(__filename);
|
|
23
17
|
|
|
@@ -39,14 +33,7 @@ function getPlatform() {
|
|
|
39
33
|
const p = platformMap[platform];
|
|
40
34
|
const a = archMap[arch];
|
|
41
35
|
|
|
42
|
-
if (!p || !a)
|
|
43
|
-
console.error(`❌ Unsupported platform: ${platform} ${arch}`);
|
|
44
|
-
console.error(
|
|
45
|
-
"Supported platforms: darwin-x64, darwin-arm64, linux-x64, linux-arm64, windows-x64",
|
|
46
|
-
);
|
|
47
|
-
process.exit(1);
|
|
48
|
-
}
|
|
49
|
-
|
|
36
|
+
if (!p || !a) return null;
|
|
50
37
|
return `${p}-${a}`;
|
|
51
38
|
}
|
|
52
39
|
|
|
@@ -58,14 +45,10 @@ async function downloadBinary(version, platform, destPath) {
|
|
|
58
45
|
const binaryExt = process.platform === "win32" ? ".exe" : "";
|
|
59
46
|
const url = `https://github.com/${GITHUB_REPO}/releases/download/v${version}/bx-${platform}${binaryExt}`;
|
|
60
47
|
|
|
61
|
-
console.log(`📦 Downloading BabelX CLI v${version} for ${platform}...`);
|
|
62
|
-
console.log(` URL: ${url}`);
|
|
63
|
-
|
|
64
48
|
return new Promise((resolve, reject) => {
|
|
65
49
|
const file = createWriteStream(destPath);
|
|
66
50
|
get(url, { followRedirects: true }, (response) => {
|
|
67
51
|
if (response.statusCode === 302 || response.statusCode === 301) {
|
|
68
|
-
// Handle redirect
|
|
69
52
|
get(response.headers.location, (res) => {
|
|
70
53
|
res.pipe(file);
|
|
71
54
|
file.on("finish", () => {
|
|
@@ -80,78 +63,47 @@ async function downloadBinary(version, platform, destPath) {
|
|
|
80
63
|
resolve();
|
|
81
64
|
});
|
|
82
65
|
} else {
|
|
83
|
-
reject(
|
|
84
|
-
new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`),
|
|
85
|
-
);
|
|
66
|
+
reject(new Error(`HTTP ${response.statusCode}`));
|
|
86
67
|
}
|
|
87
68
|
}).on("error", reject);
|
|
88
69
|
});
|
|
89
70
|
}
|
|
90
71
|
|
|
91
|
-
function makeExecutable(filePath) {
|
|
92
|
-
if (process.platform !== "win32") {
|
|
93
|
-
chmodSync(filePath, 0o755);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
72
|
async function main() {
|
|
98
|
-
// Skip
|
|
73
|
+
// Skip if CI, disabled, or not interactive
|
|
99
74
|
if (process.env.CI || process.env.SKIP_BABELX_BINARY) {
|
|
100
|
-
console.log("⏭️ Skipping binary download (CI environment)");
|
|
101
75
|
return;
|
|
102
76
|
}
|
|
103
77
|
|
|
104
78
|
try {
|
|
105
|
-
|
|
79
|
+
const platform = getPlatform();
|
|
80
|
+
if (!platform) return;
|
|
81
|
+
|
|
82
|
+
const { readFileSync } = await import("node:fs");
|
|
106
83
|
const packageJson = JSON.parse(
|
|
107
84
|
readFileSync(join(__dirname, "..", "package.json"), "utf-8"),
|
|
108
85
|
);
|
|
109
86
|
const version = packageJson.version;
|
|
110
|
-
|
|
111
|
-
const platform = getPlatform();
|
|
112
87
|
const binaryName = getBinaryName();
|
|
113
|
-
|
|
114
|
-
// Create vendor directory
|
|
115
88
|
const vendorDir = join(__dirname, "..", "vendor", platform);
|
|
116
|
-
if (!existsSync(vendorDir)) {
|
|
117
|
-
mkdirSync(vendorDir, { recursive: true });
|
|
118
|
-
}
|
|
119
|
-
|
|
120
89
|
const binaryPath = join(vendorDir, binaryName);
|
|
121
90
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
91
|
+
if (existsSync(binaryPath)) return;
|
|
92
|
+
|
|
93
|
+
if (!existsSync(vendorDir)) {
|
|
94
|
+
mkdirSync(vendorDir, { recursive: true });
|
|
126
95
|
}
|
|
127
96
|
|
|
128
|
-
// Download binary
|
|
129
97
|
await downloadBinary(version, platform, binaryPath);
|
|
130
98
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
console.log(`✅ BabelX CLI v${version} installed successfully!`);
|
|
135
|
-
console.log(` Binary: ${binaryPath}`);
|
|
136
|
-
} catch (error) {
|
|
137
|
-
// Don't fail installation if binary download fails (e.g., first release)
|
|
138
|
-
if (error.message.includes("404")) {
|
|
139
|
-
console.warn("⚠️ Binary not found for this version yet.");
|
|
140
|
-
console.warn(" The CLI will use the bundled JavaScript version.");
|
|
141
|
-
console.warn(
|
|
142
|
-
` Check https://github.com/${GITHUB_REPO}/releases for updates.`,
|
|
143
|
-
);
|
|
144
|
-
return; // Don't fail installation
|
|
99
|
+
if (process.platform !== "win32") {
|
|
100
|
+
const { chmodSync } = await import("node:fs");
|
|
101
|
+
chmodSync(binaryPath, 0o755);
|
|
145
102
|
}
|
|
146
103
|
|
|
147
|
-
console.
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
console.error("1. Check your internet connection");
|
|
151
|
-
console.error("2. Set SKIP_BABELX_BINARY=1 to skip binary download");
|
|
152
|
-
console.error("3. Install manually from GitHub releases:");
|
|
153
|
-
console.error(` https://github.com/${GITHUB_REPO}/releases`);
|
|
154
|
-
// Don't exit with error - allow npm install to continue
|
|
104
|
+
console.log(`✅ BabelX CLI binary pre-downloaded for ${platform}`);
|
|
105
|
+
} catch {
|
|
106
|
+
// Silent fail - CLI will download on first run
|
|
155
107
|
}
|
|
156
108
|
}
|
|
157
109
|
|