@dbcube/core 1.0.28 → 1.0.32

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/dist/index.cjs CHANGED
@@ -173,7 +173,11 @@ var path = __toESM(require("path"));
173
173
  var os2 = __toESM(require("os"));
174
174
  var import_follow_redirects = require("follow-redirects");
175
175
  var unzipper = __toESM(require("unzipper"));
176
+ var import_ora = __toESM(require("ora"));
177
+ var import_chalk = __toESM(require("chalk"));
176
178
  var Downloader = class {
179
+ static mainSpinner = null;
180
+ static currentSpinner = null;
177
181
  static get(prefix) {
178
182
  const arch2 = new Arquitecture();
179
183
  const platform2 = arch2.getPlatform();
@@ -211,12 +215,16 @@ var Downloader = class {
211
215
  const binaries = ["schema", "query", "sqlite"];
212
216
  const binDir = targetDir || this.getDefaultBinDir();
213
217
  fs.mkdirSync(binDir, { recursive: true });
214
- for (const prefix of binaries) {
218
+ this.mainSpinner = (0, import_ora.default)({
219
+ text: import_chalk.default.blue("Descargando binarios necesarios..."),
220
+ spinner: "dots12"
221
+ }).start();
222
+ for (let i = 0; i < binaries.length; i++) {
223
+ const prefix = binaries[i];
215
224
  const maxRetries = 3;
216
225
  let attempt = 0;
217
226
  while (attempt <= maxRetries) {
218
227
  try {
219
- console.log(`\u{1F4E5} Descargando ${prefix} (intento ${attempt + 1}/${maxRetries + 1})...`);
220
228
  const binaryInfo = this.get(prefix);
221
229
  if (!binaryInfo.name || !binaryInfo.url) {
222
230
  throw new Error("Plataforma o arquitectura no soportada");
@@ -224,35 +232,65 @@ var Downloader = class {
224
232
  const tempZipPath = path.join(os2.tmpdir(), `dbcube-${prefix}-${Date.now()}.zip`);
225
233
  const finalBinaryPath = path.join(binDir, binaryInfo.name);
226
234
  if (fs.existsSync(finalBinaryPath)) {
227
- console.log(`\u2705 ${prefix} ya existe, omitiendo`);
235
+ this.updateMainProgress(prefix, i + 1, binaries.length, "exists");
228
236
  break;
229
237
  }
230
- await this.downloadFile(binaryInfo.url, tempZipPath, prefix);
238
+ this.updateMainProgress(prefix, i + 1, binaries.length, "downloading");
239
+ await this.downloadFileWithProgress(binaryInfo.url, tempZipPath, prefix);
240
+ this.updateMainProgress(prefix, i + 1, binaries.length, "extracting");
231
241
  await this.extractBinary(tempZipPath, finalBinaryPath, prefix);
232
- console.log(`\u2705 ${prefix} descargado correctamente`);
242
+ this.updateMainProgress(prefix, i + 1, binaries.length, "completed");
233
243
  break;
234
244
  } catch (error) {
235
245
  const errorMessage = error instanceof Error ? error.message : "Error desconocido";
236
246
  if (attempt < maxRetries && (errorMessage.includes("ECONNRESET") || errorMessage.includes("timeout") || errorMessage.includes("ETIMEDOUT") || errorMessage.includes("ENOTFOUND"))) {
237
- console.warn(`\u26A0\uFE0F Error de red para ${prefix}: ${errorMessage}`);
238
- console.log(`\u{1F504} Reintentando en 3 segundos...`);
239
247
  attempt++;
240
- await new Promise((resolve5) => setTimeout(resolve5, 3e3));
248
+ this.updateMainProgress(prefix, i + 1, binaries.length, "retrying", attempt);
249
+ await new Promise((resolve5) => setTimeout(resolve5, 2e3));
241
250
  } else {
242
- console.error(`\u274C Error final descargando ${prefix}:`, errorMessage);
251
+ this.mainSpinner.fail(import_chalk.default.red(`Error descargando ${prefix}: ${errorMessage}`));
243
252
  throw error;
244
253
  }
245
254
  }
246
255
  }
247
256
  }
248
- }
249
- static downloadFile(url, outputPath, prefix) {
257
+ this.mainSpinner.succeed(import_chalk.default.green("Binarios descargados correctamente"));
258
+ }
259
+ static updateMainProgress(binary, current, total, status, attempt) {
260
+ const progressBar = this.createProgressBar(current, total);
261
+ const statusEmojis = {
262
+ downloading: "\u{1F4E5}",
263
+ extracting: "\u{1F4E6}",
264
+ completed: "\u2705",
265
+ exists: "\u2705",
266
+ retrying: "\u{1F504}"
267
+ };
268
+ const statusMessages = {
269
+ downloading: import_chalk.default.blue("descargando"),
270
+ extracting: import_chalk.default.yellow("extrayendo"),
271
+ completed: import_chalk.default.green("completado"),
272
+ exists: import_chalk.default.gray("existe"),
273
+ retrying: import_chalk.default.orange(`reintentando (${attempt}/${3})`)
274
+ };
275
+ const emoji = statusEmojis[status] || "\u{1F4E5}";
276
+ const message = statusMessages[status] || status;
277
+ this.mainSpinner.text = `${progressBar} ${emoji} ${import_chalk.default.bold(binary)} - ${message}`;
278
+ }
279
+ static createProgressBar(current, total, width = 20) {
280
+ const filled = Math.round(current / total * width);
281
+ const empty = width - filled;
282
+ const filledBar = import_chalk.default.green("\u2588".repeat(filled));
283
+ const emptyBar = import_chalk.default.gray("\u2591".repeat(empty));
284
+ const percentage = import_chalk.default.bold(`${current}/${total}`);
285
+ return `[${filledBar}${emptyBar}] ${percentage}`;
286
+ }
287
+ static downloadFileWithProgress(url, outputPath, prefix) {
250
288
  return new Promise((resolve5, reject) => {
251
289
  const request = import_follow_redirects.https.get(url, { timeout: 6e4 }, (response) => {
252
290
  if (response.statusCode === 302 || response.statusCode === 301) {
253
291
  const redirectUrl = response.headers.location;
254
292
  if (redirectUrl) {
255
- return this.downloadFile(redirectUrl, outputPath, prefix).then(resolve5).catch(reject);
293
+ return this.downloadFileWithProgress(redirectUrl, outputPath, prefix).then(resolve5).catch(reject);
256
294
  }
257
295
  }
258
296
  if (response.statusCode !== 200) {
@@ -262,21 +300,20 @@ var Downloader = class {
262
300
  const file = fs.createWriteStream(outputPath);
263
301
  const totalBytes = parseInt(response.headers["content-length"] || "0", 10);
264
302
  let downloadedBytes = 0;
265
- if (totalBytes > 0) {
266
- console.log(`\u{1F4E6} Descargando ${(totalBytes / 1024 / 1024).toFixed(1)}MB...`);
267
- }
268
303
  response.on("data", (chunk) => {
269
304
  downloadedBytes += chunk.length;
270
305
  file.write(chunk);
271
306
  if (totalBytes > 0) {
272
- const progress = (downloadedBytes / totalBytes * 100).toFixed(1);
273
- process.stdout.write(`\r\u23F3 Progreso: ${progress}%`);
307
+ const progress = {
308
+ downloaded: downloadedBytes,
309
+ total: totalBytes,
310
+ percentage: downloadedBytes / totalBytes * 100
311
+ };
312
+ this.updateDownloadProgress(prefix, progress);
274
313
  }
275
314
  });
276
315
  response.on("end", () => {
277
316
  file.end();
278
- console.log(`
279
- \u2705 Descarga de ${prefix} completada`);
280
317
  resolve5();
281
318
  });
282
319
  response.on("error", (err) => {
@@ -297,6 +334,17 @@ var Downloader = class {
297
334
  });
298
335
  });
299
336
  }
337
+ static updateDownloadProgress(binary, progress) {
338
+ const percentage = progress.percentage.toFixed(1);
339
+ const downloaded = (progress.downloaded / 1024 / 1024).toFixed(1);
340
+ const total = (progress.total / 1024 / 1024).toFixed(1);
341
+ const barWidth = 15;
342
+ const filled = Math.round(progress.percentage / 100 * barWidth);
343
+ const empty = barWidth - filled;
344
+ const progressBar = import_chalk.default.green("\u2588".repeat(filled)) + import_chalk.default.gray("\u2591".repeat(empty));
345
+ const progressText = `[${progressBar}] ${percentage}% (${downloaded}/${total}MB)`;
346
+ this.mainSpinner.text = `\u{1F4E5} ${import_chalk.default.bold(binary)} - ${progressText}`;
347
+ }
300
348
  static extractBinary(zipPath, outputPath, prefix) {
301
349
  return new Promise((resolve5, reject) => {
302
350
  let extracted = false;
@@ -310,7 +358,6 @@ var Downloader = class {
310
358
  fs.chmodSync(outputPath, 493);
311
359
  }
312
360
  this.cleanupFile(zipPath);
313
- console.log(`\u{1F4C1} ${prefix} extra\xEDdo correctamente`);
314
361
  resolve5();
315
362
  });
316
363
  writeStream.on("error", (err) => {
@@ -341,7 +388,7 @@ var Downloader = class {
341
388
  }
342
389
  static getDefaultBinDir() {
343
390
  const possibleDirs = [
344
- path.resolve(process.cwd(), "bin"),
391
+ path.resolve(process.cwd(), ".dbcube", "bin"),
345
392
  path.resolve(process.cwd(), "node_modules", ".dbcube", "bin"),
346
393
  path.resolve(__dirname, "..", "bin")
347
394
  ];
@@ -358,7 +405,7 @@ var Downloader = class {
358
405
  continue;
359
406
  }
360
407
  }
361
- const tempDir = path.join(os2.tmpdir(), "dbcube-bin");
408
+ const tempDir = path.join(os2.tmpdir(), ".dbcube", "bin");
362
409
  fs.mkdirSync(tempDir, { recursive: true });
363
410
  return tempDir;
364
411
  }
@@ -367,6 +414,7 @@ var Downloader = class {
367
414
  // src/lib/Binary.ts
368
415
  var fs2 = __toESM(require("fs"));
369
416
  var path2 = __toESM(require("path"));
417
+ var os3 = __toESM(require("os"));
370
418
  var Binary = class {
371
419
  static isDownloading = false;
372
420
  static downloadPromise = null;
@@ -398,11 +446,9 @@ var Binary = class {
398
446
  }
399
447
  }
400
448
  static async downloadBinaries() {
401
- console.log("\u{1F680} DBCube: Descargando binarios necesarios...");
402
449
  try {
403
450
  const binDir = this.getBinDir();
404
451
  await Downloader.download(binDir);
405
- console.log("\u2705 DBCube: Binarios descargados correctamente");
406
452
  } catch (error) {
407
453
  console.warn("\u26A0\uFE0F DBCube: Error descargando binarios:", error.message);
408
454
  console.log("\u{1F527} Los binarios se intentar\xE1n descargar en la pr\xF3xima ejecuci\xF3n");
@@ -410,7 +456,7 @@ var Binary = class {
410
456
  }
411
457
  static getBinDir() {
412
458
  const possibleDirs = [
413
- path2.resolve(process.cwd(), "bin"),
459
+ path2.resolve(process.cwd(), ".dbcube", "bin"),
414
460
  path2.resolve(process.cwd(), "node_modules", ".dbcube", "bin"),
415
461
  path2.resolve(__dirname, "..", "bin")
416
462
  ];
@@ -419,12 +465,32 @@ var Binary = class {
419
465
  if (!fs2.existsSync(dir)) {
420
466
  fs2.mkdirSync(dir, { recursive: true });
421
467
  }
468
+ if (dir.includes(path2.resolve(process.cwd(), ".dbcube"))) {
469
+ this.ensureGitIgnore(path2.resolve(process.cwd(), ".dbcube"));
470
+ }
422
471
  return dir;
423
472
  } catch {
424
473
  continue;
425
474
  }
426
475
  }
427
- return process.cwd();
476
+ const tempDir = path2.join(os3.tmpdir(), ".dbcube", "bin");
477
+ fs2.mkdirSync(tempDir, { recursive: true });
478
+ return tempDir;
479
+ }
480
+ static ensureGitIgnore(dbcubeDir) {
481
+ try {
482
+ const gitignorePath = path2.join(dbcubeDir, ".gitignore");
483
+ if (!fs2.existsSync(gitignorePath)) {
484
+ const gitignoreContent = `# DBCube binaries
485
+ bin/
486
+ *.exe
487
+ *.log
488
+ *.tmp
489
+ `;
490
+ fs2.writeFileSync(gitignorePath, gitignoreContent);
491
+ }
492
+ } catch {
493
+ }
428
494
  }
429
495
  static async get() {
430
496
  await this.ensureBinariesExist();