@hapico/cli 0.0.13 → 0.0.14

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/index.js CHANGED
@@ -300,7 +300,7 @@ class RoomState {
300
300
  return this.isConnected;
301
301
  }
302
302
  }
303
- commander_1.program.version("0.0.13").description("Hapico CLI for project management");
303
+ commander_1.program.version("0.0.14").description("Hapico CLI for project management");
304
304
  commander_1.program
305
305
  .command("clone <id>")
306
306
  .description("Clone a project by ID")
@@ -373,12 +373,38 @@ commander_1.program
373
373
  devSpinner.fail("Source directory 'src' does not exist. Please clone a project first.");
374
374
  return;
375
375
  }
376
+ // Directory to store session config
377
+ const tmpDir = path.join(pwd, ".tmp");
378
+ const sessionConfigFile = path.join(tmpDir, "config.json");
379
+ // Ensure .tmp directory exists
380
+ if (!fs.existsSync(tmpDir)) {
381
+ fs.mkdirSync(tmpDir, { recursive: true });
382
+ }
383
+ // Function to get stored session ID
384
+ const getStoredSessionId = () => {
385
+ if (fs.existsSync(sessionConfigFile)) {
386
+ const data = fs.readFileSync(sessionConfigFile, { encoding: "utf8" });
387
+ const json = JSON.parse(data);
388
+ return json.sessionId || null;
389
+ }
390
+ return null;
391
+ };
392
+ // Function to save session ID
393
+ const saveSessionId = (sessionId) => {
394
+ fs.writeFileSync(sessionConfigFile, JSON.stringify({ sessionId }, null, 2), { encoding: "utf8" });
395
+ };
396
+ // Get or generate session ID
397
+ let sessionId = getStoredSessionId();
398
+ if (!sessionId) {
399
+ sessionId = (0, crypto_1.randomUUID)();
400
+ saveSessionId(sessionId);
401
+ }
376
402
  const info = JSON.stringify({
377
- id: (0, crypto_1.randomUUID)(),
403
+ id: sessionId,
378
404
  createdAt: new Date().toISOString(),
379
405
  viewId: getStoredProjectId(pwd),
380
406
  });
381
- // convert info info to base64
407
+ // Convert info to base64
382
408
  const projectId = Buffer.from(info).toString("base64");
383
409
  if (!projectId) {
384
410
  devSpinner.fail("Project ID not found. Please ensure hapico.config.json exists in the project directory.");
@@ -402,7 +428,7 @@ commander_1.program
402
428
  });
403
429
  room.updateState("view", updatedView);
404
430
  });
405
- // LẤy thông tin về project
431
+ // Fetch project info
406
432
  const projectInfo = getStoredProjectId(pwd);
407
433
  if (!projectInfo) {
408
434
  console.error("Project ID not found. Please ensure hapico.config.json exists in the project directory.");
@@ -428,7 +454,6 @@ commander_1.program
428
454
  return;
429
455
  }
430
456
  else {
431
- // show link https://com.ai.vn/dev_preview/{projectId}
432
457
  const previewUrl = `https://com.ai.vn/dev_preview/${projectId}`;
433
458
  console.log(`Open this URL in your browser to preview the project: \n${previewUrl}`);
434
459
  await (0, open_1.default)(previewUrl);
package/dist/index.js CHANGED
@@ -300,7 +300,7 @@ class RoomState {
300
300
  return this.isConnected;
301
301
  }
302
302
  }
303
- commander_1.program.version("0.0.13").description("Hapico CLI for project management");
303
+ commander_1.program.version("0.0.14").description("Hapico CLI for project management");
304
304
  commander_1.program
305
305
  .command("clone <id>")
306
306
  .description("Clone a project by ID")
@@ -373,12 +373,38 @@ commander_1.program
373
373
  devSpinner.fail("Source directory 'src' does not exist. Please clone a project first.");
374
374
  return;
375
375
  }
376
+ // Directory to store session config
377
+ const tmpDir = path.join(pwd, ".tmp");
378
+ const sessionConfigFile = path.join(tmpDir, "config.json");
379
+ // Ensure .tmp directory exists
380
+ if (!fs.existsSync(tmpDir)) {
381
+ fs.mkdirSync(tmpDir, { recursive: true });
382
+ }
383
+ // Function to get stored session ID
384
+ const getStoredSessionId = () => {
385
+ if (fs.existsSync(sessionConfigFile)) {
386
+ const data = fs.readFileSync(sessionConfigFile, { encoding: "utf8" });
387
+ const json = JSON.parse(data);
388
+ return json.sessionId || null;
389
+ }
390
+ return null;
391
+ };
392
+ // Function to save session ID
393
+ const saveSessionId = (sessionId) => {
394
+ fs.writeFileSync(sessionConfigFile, JSON.stringify({ sessionId }, null, 2), { encoding: "utf8" });
395
+ };
396
+ // Get or generate session ID
397
+ let sessionId = getStoredSessionId();
398
+ if (!sessionId) {
399
+ sessionId = (0, crypto_1.randomUUID)();
400
+ saveSessionId(sessionId);
401
+ }
376
402
  const info = JSON.stringify({
377
- id: (0, crypto_1.randomUUID)(),
403
+ id: sessionId,
378
404
  createdAt: new Date().toISOString(),
379
405
  viewId: getStoredProjectId(pwd),
380
406
  });
381
- // convert info info to base64
407
+ // Convert info to base64
382
408
  const projectId = Buffer.from(info).toString("base64");
383
409
  if (!projectId) {
384
410
  devSpinner.fail("Project ID not found. Please ensure hapico.config.json exists in the project directory.");
@@ -402,7 +428,7 @@ commander_1.program
402
428
  });
403
429
  room.updateState("view", updatedView);
404
430
  });
405
- // LẤy thông tin về project
431
+ // Fetch project info
406
432
  const projectInfo = getStoredProjectId(pwd);
407
433
  if (!projectInfo) {
408
434
  console.error("Project ID not found. Please ensure hapico.config.json exists in the project directory.");
@@ -428,7 +454,6 @@ commander_1.program
428
454
  return;
429
455
  }
430
456
  else {
431
- // show link https://com.ai.vn/dev_preview/{projectId}
432
457
  const previewUrl = `https://com.ai.vn/dev_preview/${projectId}`;
433
458
  console.log(`Open this URL in your browser to preview the project: \n${previewUrl}`);
434
459
  await (0, open_1.default)(previewUrl);
package/index.ts CHANGED
@@ -346,7 +346,7 @@ class RoomState {
346
346
  }
347
347
  }
348
348
 
349
- program.version("0.0.13").description("Hapico CLI for project management");
349
+ program.version("0.0.14").description("Hapico CLI for project management");
350
350
 
351
351
  program
352
352
  .command("clone <id>")
@@ -431,23 +431,58 @@ program
431
431
  console.error("You need to login first. Use 'hapico login' command.");
432
432
  return;
433
433
  }
434
- const devSpinner: Ora = ora(
435
- "Starting the project in development mode..."
436
- ).start();
437
- const pwd: string = process.cwd();
438
- const srcDir: string = path.join(pwd, "src");
434
+ const devSpinner = ora("Starting the project in development mode...").start();
435
+ const pwd = process.cwd();
436
+ const srcDir = path.join(pwd, "src");
439
437
  if (!fs.existsSync(srcDir)) {
440
438
  devSpinner.fail(
441
439
  "Source directory 'src' does not exist. Please clone a project first."
442
440
  );
443
441
  return;
444
442
  }
443
+
444
+ // Directory to store session config
445
+ const tmpDir = path.join(pwd, ".tmp");
446
+ const sessionConfigFile = path.join(tmpDir, "config.json");
447
+
448
+ // Ensure .tmp directory exists
449
+ if (!fs.existsSync(tmpDir)) {
450
+ fs.mkdirSync(tmpDir, { recursive: true });
451
+ }
452
+
453
+ // Function to get stored session ID
454
+ const getStoredSessionId = () => {
455
+ if (fs.existsSync(sessionConfigFile)) {
456
+ const data = fs.readFileSync(sessionConfigFile, { encoding: "utf8" });
457
+ const json = JSON.parse(data);
458
+ return json.sessionId || null;
459
+ }
460
+ return null;
461
+ };
462
+
463
+ // Function to save session ID
464
+ const saveSessionId = (sessionId: string) => {
465
+ fs.writeFileSync(
466
+ sessionConfigFile,
467
+ JSON.stringify({ sessionId }, null, 2),
468
+ { encoding: "utf8" }
469
+ );
470
+ };
471
+
472
+ // Get or generate session ID
473
+ let sessionId = getStoredSessionId();
474
+ if (!sessionId) {
475
+ sessionId = randomUUID();
476
+ saveSessionId(sessionId);
477
+ }
478
+
445
479
  const info = JSON.stringify({
446
- id: randomUUID(),
480
+ id: sessionId,
447
481
  createdAt: new Date().toISOString(),
448
482
  viewId: getStoredProjectId(pwd),
449
483
  });
450
- // convert info info to base64
484
+
485
+ // Convert info to base64
451
486
  const projectId = Buffer.from(info).toString("base64");
452
487
  if (!projectId) {
453
488
  devSpinner.fail(
@@ -455,6 +490,7 @@ program
455
490
  );
456
491
  return;
457
492
  }
493
+
458
494
  console.log(`Connecting to WebSocket server`);
459
495
  const room = new RoomState(`view_${projectId}`);
460
496
 
@@ -466,7 +502,7 @@ program
466
502
  const initialFiles = fileManager.listFiles();
467
503
  room.updateState("view", initialFiles);
468
504
 
469
- fileManager.setOnFileChange((filePath: string, content: string) => {
505
+ fileManager.setOnFileChange((filePath, content) => {
470
506
  const es5 = compileES5(content, filePath);
471
507
  console.log(`File changed: ${filePath?.replace(srcDir, ".")}`);
472
508
  const updatedView = room.files.map((file) => {
@@ -478,7 +514,7 @@ program
478
514
  room.updateState("view", updatedView);
479
515
  });
480
516
 
481
- // LẤy thông tin về project
517
+ // Fetch project info
482
518
  const projectInfo = getStoredProjectId(pwd);
483
519
  if (!projectInfo) {
484
520
  console.error(
@@ -509,14 +545,13 @@ program
509
545
  QRCode.generate(
510
546
  `https://zalo.me/s/3218692650896662017/player/${projectId}`,
511
547
  { small: true },
512
- (qrcode: string) => {
548
+ (qrcode) => {
513
549
  console.log("Scan this QR code to connect to the project:");
514
550
  console.log(qrcode);
515
551
  }
516
552
  );
517
553
  return;
518
554
  } else {
519
- // show link https://com.ai.vn/dev_preview/{projectId}
520
555
  const previewUrl = `https://com.ai.vn/dev_preview/${projectId}`;
521
556
  console.log(
522
557
  `Open this URL in your browser to preview the project: \n${previewUrl}`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hapico/cli",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "description": "A simple CLI tool for project management",
5
5
  "main": "index.js",
6
6
  "bin": {