@babelx/cli 0.2.2 → 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 +43 -13
- package/package.json +1 -1
package/bin/bx
CHANGED
|
@@ -63,8 +63,9 @@ async function downloadBinary(platform, destPath) {
|
|
|
63
63
|
const version = packageJson.version;
|
|
64
64
|
const url = `https://github.com/${GITHUB_REPO}/releases/download/v${version}/bx-${platform}${process.platform === "win32" ? ".exe" : ""}`;
|
|
65
65
|
|
|
66
|
-
console.log(`📦 First time setup: Downloading BabelX CLI v${version}
|
|
66
|
+
console.log(`📦 First time setup: Downloading BabelX CLI v${version}`);
|
|
67
67
|
console.log(` Platform: ${platform}`);
|
|
68
|
+
console.log("");
|
|
68
69
|
|
|
69
70
|
// Ensure directory exists
|
|
70
71
|
const dir = dirname(destPath);
|
|
@@ -74,21 +75,50 @@ async function downloadBinary(platform, destPath) {
|
|
|
74
75
|
|
|
75
76
|
return new Promise((resolve, reject) => {
|
|
76
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
|
+
|
|
77
117
|
get(url, { followRedirects: true }, (response) => {
|
|
78
118
|
if (response.statusCode === 302 || response.statusCode === 301) {
|
|
79
|
-
get(response.headers.location, (
|
|
80
|
-
res.pipe(file);
|
|
81
|
-
file.on("finish", () => {
|
|
82
|
-
file.close();
|
|
83
|
-
resolve();
|
|
84
|
-
});
|
|
85
|
-
}).on("error", reject);
|
|
119
|
+
get(response.headers.location, handleResponse).on("error", reject);
|
|
86
120
|
} else if (response.statusCode === 200) {
|
|
87
|
-
response
|
|
88
|
-
file.on("finish", () => {
|
|
89
|
-
file.close();
|
|
90
|
-
resolve();
|
|
91
|
-
});
|
|
121
|
+
handleResponse(response);
|
|
92
122
|
} else {
|
|
93
123
|
reject(
|
|
94
124
|
new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`),
|