@chahuadev/framework 5.0.2 → 5.0.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/install.js +51 -14
- package/package.json +1 -1
package/install.js
CHANGED
|
@@ -9,7 +9,7 @@ const arch = os.arch();
|
|
|
9
9
|
let downloadUrl = '';
|
|
10
10
|
let fileName = '';
|
|
11
11
|
|
|
12
|
-
// เช็ก OS เพื่อเลือกลิงก์ดาวน์โหลด
|
|
12
|
+
// 1. เช็ก OS เพื่อเลือกลิงก์ดาวน์โหลด
|
|
13
13
|
if (platform === 'win32') {
|
|
14
14
|
if (arch === 'ia32') {
|
|
15
15
|
downloadUrl = 'https://huggingface.co/datasets/chahuadev/chahuadev-framework-binaries/resolve/main/Chahuadev%20Framework-win-ia32.exe?download=true';
|
|
@@ -28,7 +28,7 @@ if (!downloadUrl) {
|
|
|
28
28
|
process.exit(1);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
// สร้างโฟลเดอร์ bin เพื่อเก็บไฟล์ที่โหลดมา
|
|
31
|
+
// 2. สร้างโฟลเดอร์ bin เพื่อเก็บไฟล์ที่โหลดมา
|
|
32
32
|
const binDir = path.join(__dirname, 'bin');
|
|
33
33
|
if (!fs.existsSync(binDir)) {
|
|
34
34
|
fs.mkdirSync(binDir);
|
|
@@ -36,26 +36,63 @@ if (!fs.existsSync(binDir)) {
|
|
|
36
36
|
|
|
37
37
|
const filePath = path.join(binDir, fileName);
|
|
38
38
|
console.log(`Downloading Chahuadev Framework for ${platform} (${arch})...`);
|
|
39
|
+
console.log(`Please wait, this may take a few minutes depending on your connection.\n`);
|
|
39
40
|
|
|
40
|
-
//
|
|
41
|
+
// 3. ฟังก์ชันดาวน์โหลดพร้อมระบบโชว์ Progress Bar
|
|
41
42
|
function downloadFile(url, dest) {
|
|
42
|
-
const file = fs.createWriteStream(dest);
|
|
43
43
|
https.get(url, (response) => {
|
|
44
|
+
// จัดการกรณีลิงก์ถูก Redirect
|
|
44
45
|
if (response.statusCode === 301 || response.statusCode === 302) {
|
|
45
|
-
downloadFile(response.headers.location, dest);
|
|
46
|
-
} else {
|
|
47
|
-
response.pipe(file);
|
|
48
|
-
file.on('finish', () => {
|
|
49
|
-
file.close();
|
|
50
|
-
if (platform !== 'win32') fs.chmodSync(dest, 0o755); // ให้สิทธิ์รันบน Linux
|
|
51
|
-
console.log('Download completed successfully!');
|
|
52
|
-
});
|
|
46
|
+
return downloadFile(response.headers.location, dest);
|
|
53
47
|
}
|
|
48
|
+
|
|
49
|
+
// ดึงขนาดไฟล์ทั้งหมดมาเพื่อคำนวณ %
|
|
50
|
+
const totalBytes = parseInt(response.headers['content-length'], 10);
|
|
51
|
+
let downloadedBytes = 0;
|
|
52
|
+
|
|
53
|
+
const file = fs.createWriteStream(dest);
|
|
54
|
+
|
|
55
|
+
// ดักจับข้อมูลตอนที่กำลังโหลดทีละก้อน (chunk)
|
|
56
|
+
response.on('data', (chunk) => {
|
|
57
|
+
downloadedBytes += chunk.length;
|
|
58
|
+
|
|
59
|
+
if (!isNaN(totalBytes)) {
|
|
60
|
+
// คำนวณเปอร์เซ็นต์
|
|
61
|
+
const percent = ((downloadedBytes / totalBytes) * 100).toFixed(2);
|
|
62
|
+
const downloadedMB = (downloadedBytes / (1024 * 1024)).toFixed(2);
|
|
63
|
+
const totalMB = (totalBytes / (1024 * 1024)).toFixed(2);
|
|
64
|
+
|
|
65
|
+
// วาดหลอด Progress Bar
|
|
66
|
+
const barLength = 30;
|
|
67
|
+
const filledLength = Math.round((barLength * downloadedBytes) / totalBytes);
|
|
68
|
+
const bar = '█'.repeat(filledLength) + '-'.repeat(barLength - filledLength);
|
|
69
|
+
|
|
70
|
+
process.stdout.write(`\r[${bar}] ${percent}% (${downloadedMB} MB / ${totalMB} MB)`);
|
|
71
|
+
} else {
|
|
72
|
+
const downloadedMB = (downloadedBytes / (1024 * 1024)).toFixed(2);
|
|
73
|
+
process.stdout.write(`\rDownloading... ${downloadedMB} MB`);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
response.pipe(file);
|
|
78
|
+
|
|
79
|
+
file.on('finish', () => {
|
|
80
|
+
file.close();
|
|
81
|
+
console.log('\n\n✅ Download completed successfully!');
|
|
82
|
+
|
|
83
|
+
// *** ชี้เป้าหมายที่อยู่ไฟล์แบบชัดเจน ***
|
|
84
|
+
console.log(`📂 File saved to: ${dest}\n`);
|
|
85
|
+
|
|
86
|
+
// ให้สิทธิ์รันบน Linux
|
|
87
|
+
if (platform !== 'win32') fs.chmodSync(dest, 0o755);
|
|
88
|
+
});
|
|
89
|
+
|
|
54
90
|
}).on('error', (err) => {
|
|
55
|
-
fs.unlink(dest, () => {});
|
|
56
|
-
console.error('Download failed:', err.message);
|
|
91
|
+
fs.unlink(dest, () => {}); // ถ้า error ให้ลบไฟล์ที่โหลดค้างทิ้ง
|
|
92
|
+
console.error('\n❌ Download failed:', err.message);
|
|
57
93
|
process.exit(1);
|
|
58
94
|
});
|
|
59
95
|
}
|
|
60
96
|
|
|
97
|
+
// เริ่มดาวน์โหลด
|
|
61
98
|
downloadFile(downloadUrl, filePath);
|