@hapico/cli 0.0.12 → 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
@@ -71,10 +71,10 @@ const getStoredToken = () => {
71
71
  if (fs.existsSync(TOKEN_FILE)) {
72
72
  const data = fs.readFileSync(TOKEN_FILE, { encoding: "utf8" });
73
73
  const json = JSON.parse(data);
74
- return json !== null && json !== void 0 ? json : {
74
+ return (json !== null && json !== void 0 ? json : {
75
75
  accessToken: null,
76
76
  refreshToken: null,
77
- };
77
+ });
78
78
  }
79
79
  return {
80
80
  accessToken: null,
@@ -300,7 +300,7 @@ class RoomState {
300
300
  return this.isConnected;
301
301
  }
302
302
  }
303
- commander_1.program.version("0.0.12").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,11 +373,39 @@ commander_1.program
373
373
  devSpinner.fail("Source directory 'src' does not exist. Please clone a project first.");
374
374
  return;
375
375
  }
376
- const projectId = JSON.stringify({
377
- id: (0, crypto_1.randomUUID)(),
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
+ }
402
+ const info = JSON.stringify({
403
+ id: sessionId,
378
404
  createdAt: new Date().toISOString(),
379
405
  viewId: getStoredProjectId(pwd),
380
406
  });
407
+ // Convert info to base64
408
+ const projectId = Buffer.from(info).toString("base64");
381
409
  if (!projectId) {
382
410
  devSpinner.fail("Project ID not found. Please ensure hapico.config.json exists in the project directory.");
383
411
  return;
@@ -400,10 +428,36 @@ commander_1.program
400
428
  });
401
429
  room.updateState("view", updatedView);
402
430
  });
403
- qrcode_terminal_1.default.generate(`https://zalo.me/s/3218692650896662017/player/${projectId}`, { small: true }, (qrcode) => {
404
- console.log("Scan this QR code to connect to the project:");
405
- console.log(qrcode);
431
+ // Fetch project info
432
+ const projectInfo = getStoredProjectId(pwd);
433
+ if (!projectInfo) {
434
+ console.error("Project ID not found. Please ensure hapico.config.json exists in the project directory.");
435
+ return;
436
+ }
437
+ const project = await axios_1.default.get(`https://base.myworkbeast.com/api/views/${projectInfo}`, {
438
+ headers: {
439
+ Authorization: `Bearer ${accessToken}`,
440
+ "Content-Type": "application/json",
441
+ },
406
442
  });
443
+ if (project.status !== 200) {
444
+ console.error(`Error fetching project info: ${project.statusText}`);
445
+ return;
446
+ }
447
+ const projectType = project.data.type || "view";
448
+ console.log("Project type:", projectType);
449
+ if (projectType === "zalominiapp") {
450
+ qrcode_terminal_1.default.generate(`https://zalo.me/s/3218692650896662017/player/${projectId}`, { small: true }, (qrcode) => {
451
+ console.log("Scan this QR code to connect to the project:");
452
+ console.log(qrcode);
453
+ });
454
+ return;
455
+ }
456
+ else {
457
+ const previewUrl = `https://com.ai.vn/dev_preview/${projectId}`;
458
+ console.log(`Open this URL in your browser to preview the project: \n${previewUrl}`);
459
+ await (0, open_1.default)(previewUrl);
460
+ }
407
461
  });
408
462
  });
409
463
  commander_1.program
package/dist/index.js CHANGED
@@ -71,10 +71,10 @@ const getStoredToken = () => {
71
71
  if (fs.existsSync(TOKEN_FILE)) {
72
72
  const data = fs.readFileSync(TOKEN_FILE, { encoding: "utf8" });
73
73
  const json = JSON.parse(data);
74
- return json !== null && json !== void 0 ? json : {
74
+ return (json !== null && json !== void 0 ? json : {
75
75
  accessToken: null,
76
76
  refreshToken: null,
77
- };
77
+ });
78
78
  }
79
79
  return {
80
80
  accessToken: null,
@@ -300,7 +300,7 @@ class RoomState {
300
300
  return this.isConnected;
301
301
  }
302
302
  }
303
- commander_1.program.version("0.0.12").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,11 +373,39 @@ commander_1.program
373
373
  devSpinner.fail("Source directory 'src' does not exist. Please clone a project first.");
374
374
  return;
375
375
  }
376
- const projectId = JSON.stringify({
377
- id: (0, crypto_1.randomUUID)(),
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
+ }
402
+ const info = JSON.stringify({
403
+ id: sessionId,
378
404
  createdAt: new Date().toISOString(),
379
405
  viewId: getStoredProjectId(pwd),
380
406
  });
407
+ // Convert info to base64
408
+ const projectId = Buffer.from(info).toString("base64");
381
409
  if (!projectId) {
382
410
  devSpinner.fail("Project ID not found. Please ensure hapico.config.json exists in the project directory.");
383
411
  return;
@@ -400,10 +428,36 @@ commander_1.program
400
428
  });
401
429
  room.updateState("view", updatedView);
402
430
  });
403
- qrcode_terminal_1.default.generate(`https://zalo.me/s/3218692650896662017/player/${projectId}`, { small: true }, (qrcode) => {
404
- console.log("Scan this QR code to connect to the project:");
405
- console.log(qrcode);
431
+ // Fetch project info
432
+ const projectInfo = getStoredProjectId(pwd);
433
+ if (!projectInfo) {
434
+ console.error("Project ID not found. Please ensure hapico.config.json exists in the project directory.");
435
+ return;
436
+ }
437
+ const project = await axios_1.default.get(`https://base.myworkbeast.com/api/views/${projectInfo}`, {
438
+ headers: {
439
+ Authorization: `Bearer ${accessToken}`,
440
+ "Content-Type": "application/json",
441
+ },
406
442
  });
443
+ if (project.status !== 200) {
444
+ console.error(`Error fetching project info: ${project.statusText}`);
445
+ return;
446
+ }
447
+ const projectType = project.data.type || "view";
448
+ console.log("Project type:", projectType);
449
+ if (projectType === "zalominiapp") {
450
+ qrcode_terminal_1.default.generate(`https://zalo.me/s/3218692650896662017/player/${projectId}`, { small: true }, (qrcode) => {
451
+ console.log("Scan this QR code to connect to the project:");
452
+ console.log(qrcode);
453
+ });
454
+ return;
455
+ }
456
+ else {
457
+ const previewUrl = `https://com.ai.vn/dev_preview/${projectId}`;
458
+ console.log(`Open this URL in your browser to preview the project: \n${previewUrl}`);
459
+ await (0, open_1.default)(previewUrl);
460
+ }
407
461
  });
408
462
  });
409
463
  commander_1.program
package/index.ts CHANGED
@@ -31,13 +31,9 @@ const saveToken = (tokens: { accessToken: string; refreshToken?: string }) => {
31
31
  if (!tokens || !tokens.accessToken) {
32
32
  throw new Error("Invalid token data");
33
33
  }
34
- fs.writeFileSync(
35
- TOKEN_FILE,
36
- JSON.stringify(tokens, null, 2),
37
- {
38
- encoding: "utf8",
39
- }
40
- );
34
+ fs.writeFileSync(TOKEN_FILE, JSON.stringify(tokens, null, 2), {
35
+ encoding: "utf8",
36
+ });
41
37
  };
42
38
 
43
39
  // Function to get stored token
@@ -48,10 +44,12 @@ const getStoredToken = (): {
48
44
  if (fs.existsSync(TOKEN_FILE)) {
49
45
  const data = fs.readFileSync(TOKEN_FILE, { encoding: "utf8" });
50
46
  const json = JSON.parse(data);
51
- return json ?? {
52
- accessToken: null,
53
- refreshToken: null,
54
- };
47
+ return (
48
+ json ?? {
49
+ accessToken: null,
50
+ refreshToken: null,
51
+ }
52
+ );
55
53
  }
56
54
  return {
57
55
  accessToken: null,
@@ -292,9 +290,7 @@ class RoomState {
292
290
  this.isConnected = false;
293
291
 
294
292
  this.reconnectAttempts++;
295
- connected.start(
296
- `Retry connection...`
297
- );
293
+ connected.start(`Retry connection...`);
298
294
 
299
295
  this.connect();
300
296
  });
@@ -350,7 +346,7 @@ class RoomState {
350
346
  }
351
347
  }
352
348
 
353
- program.version("0.0.12").description("Hapico CLI for project management");
349
+ program.version("0.0.14").description("Hapico CLI for project management");
354
350
 
355
351
  program
356
352
  .command("clone <id>")
@@ -435,28 +431,66 @@ program
435
431
  console.error("You need to login first. Use 'hapico login' command.");
436
432
  return;
437
433
  }
438
- const devSpinner: Ora = ora(
439
- "Starting the project in development mode..."
440
- ).start();
441
- const pwd: string = process.cwd();
442
- 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");
443
437
  if (!fs.existsSync(srcDir)) {
444
438
  devSpinner.fail(
445
439
  "Source directory 'src' does not exist. Please clone a project first."
446
440
  );
447
441
  return;
448
442
  }
449
- const projectId = JSON.stringify({
450
- id: randomUUID(),
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
+
479
+ const info = JSON.stringify({
480
+ id: sessionId,
451
481
  createdAt: new Date().toISOString(),
452
482
  viewId: getStoredProjectId(pwd),
453
483
  });
484
+
485
+ // Convert info to base64
486
+ const projectId = Buffer.from(info).toString("base64");
454
487
  if (!projectId) {
455
488
  devSpinner.fail(
456
489
  "Project ID not found. Please ensure hapico.config.json exists in the project directory."
457
490
  );
458
491
  return;
459
492
  }
493
+
460
494
  console.log(`Connecting to WebSocket server`);
461
495
  const room = new RoomState(`view_${projectId}`);
462
496
 
@@ -468,7 +502,7 @@ program
468
502
  const initialFiles = fileManager.listFiles();
469
503
  room.updateState("view", initialFiles);
470
504
 
471
- fileManager.setOnFileChange((filePath: string, content: string) => {
505
+ fileManager.setOnFileChange((filePath, content) => {
472
506
  const es5 = compileES5(content, filePath);
473
507
  console.log(`File changed: ${filePath?.replace(srcDir, ".")}`);
474
508
  const updatedView = room.files.map((file) => {
@@ -480,14 +514,50 @@ program
480
514
  room.updateState("view", updatedView);
481
515
  });
482
516
 
483
- QRCode.generate(
484
- `https://zalo.me/s/3218692650896662017/player/${projectId}`,
485
- { small: true },
486
- (qrcode: string) => {
487
- console.log("Scan this QR code to connect to the project:");
488
- console.log(qrcode);
517
+ // Fetch project info
518
+ const projectInfo = getStoredProjectId(pwd);
519
+ if (!projectInfo) {
520
+ console.error(
521
+ "Project ID not found. Please ensure hapico.config.json exists in the project directory."
522
+ );
523
+ return;
524
+ }
525
+ const project = await axios.get(
526
+ `https://base.myworkbeast.com/api/views/${projectInfo}`,
527
+ {
528
+ headers: {
529
+ Authorization: `Bearer ${accessToken}`,
530
+ "Content-Type": "application/json",
531
+ },
489
532
  }
490
533
  );
534
+
535
+ if (project.status !== 200) {
536
+ console.error(`Error fetching project info: ${project.statusText}`);
537
+ return;
538
+ }
539
+
540
+ const projectType = project.data.type || "view";
541
+
542
+ console.log("Project type:", projectType);
543
+
544
+ if (projectType === "zalominiapp") {
545
+ QRCode.generate(
546
+ `https://zalo.me/s/3218692650896662017/player/${projectId}`,
547
+ { small: true },
548
+ (qrcode) => {
549
+ console.log("Scan this QR code to connect to the project:");
550
+ console.log(qrcode);
551
+ }
552
+ );
553
+ return;
554
+ } else {
555
+ const previewUrl = `https://com.ai.vn/dev_preview/${projectId}`;
556
+ console.log(
557
+ `Open this URL in your browser to preview the project: \n${previewUrl}`
558
+ );
559
+ await open(previewUrl);
560
+ }
491
561
  });
492
562
  });
493
563
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hapico/cli",
3
- "version": "0.0.12",
3
+ "version": "0.0.14",
4
4
  "description": "A simple CLI tool for project management",
5
5
  "main": "index.js",
6
6
  "bin": {