lunel-cli 0.1.99 → 0.1.101

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.
Files changed (2) hide show
  1. package/dist/index.js +105 -51
  2. package/package.json +3 -1
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { WebSocket } from "ws";
3
3
  import qrcode from "qrcode-terminal";
4
+ import shell from "shelljs";
4
5
  import { createAiManager } from "./ai/index.js";
5
6
  import { V2SessionTransport } from "./transport/v2.js";
6
7
  import Ignore from "ignore";
@@ -566,64 +567,82 @@ async function handleFsGrep(payload) {
566
567
  throw Object.assign(new Error("pattern is required"), { code: "EINVAL" });
567
568
  const safePath = assertSafePath(reqPath);
568
569
  const matches = [];
569
- const regex = new RegExp(pattern, caseSensitive ? "g" : "gi");
570
+ let regex;
571
+ try {
572
+ regex = new RegExp(pattern, caseSensitive ? "g" : "gi");
573
+ }
574
+ catch {
575
+ throw Object.assign(new Error("pattern must be a valid regular expression"), { code: "EINVAL" });
576
+ }
570
577
  const rootIgnore = await loadGitignore(ROOT_DIR);
571
- async function searchFile(filePath, relativePath) {
572
- if (matches.length >= maxResults)
573
- return;
574
- try {
575
- const content = await fs.readFile(filePath, "utf-8");
576
- const lines = content.split("\n");
577
- for (let i = 0; i < lines.length && matches.length < maxResults; i++) {
578
- if (regex.test(lines[i])) {
579
- matches.push({
580
- file: relativePath,
581
- line: i + 1,
582
- content: lines[i].substring(0, 500),
583
- });
578
+ const previousSilent = shell.config.silent;
579
+ shell.config.silent = true;
580
+ try {
581
+ async function searchFile(filePath, relativePath) {
582
+ if (matches.length >= maxResults)
583
+ return;
584
+ try {
585
+ const grepResult = shell.grep(regex, filePath);
586
+ if (grepResult.code !== 0 || !grepResult.stdout.trim()) {
587
+ regex.lastIndex = 0;
588
+ return;
584
589
  }
590
+ const content = await fs.readFile(filePath, "utf-8");
591
+ const lines = content.split("\n");
592
+ for (let i = 0; i < lines.length && matches.length < maxResults; i++) {
593
+ if (regex.test(lines[i])) {
594
+ matches.push({
595
+ file: relativePath,
596
+ line: i + 1,
597
+ content: lines[i].substring(0, 500),
598
+ });
599
+ }
600
+ regex.lastIndex = 0;
601
+ }
602
+ }
603
+ catch {
585
604
  regex.lastIndex = 0;
586
605
  }
587
606
  }
588
- catch {
589
- // Skip unreadable files
590
- }
591
- }
592
- async function searchDir(dirPath, relativePath, ig) {
593
- if (matches.length >= maxResults)
594
- return;
595
- const localIgnore = ignore().add(ig);
596
- try {
597
- const localGitignorePath = path.join(dirPath, ".gitignore");
598
- const content = await fs.readFile(localGitignorePath, "utf-8");
599
- localIgnore.add(content);
600
- }
601
- catch {
602
- // No local .gitignore
603
- }
604
- const entries = await fs.readdir(dirPath, { withFileTypes: true });
605
- for (const entry of entries) {
607
+ async function searchDir(dirPath, relativePath, ig) {
606
608
  if (matches.length >= maxResults)
607
- break;
608
- const relPath = relativePath ? `${relativePath}/${entry.name}` : entry.name;
609
- const checkPath = entry.isDirectory() ? `${relPath}/` : relPath;
610
- if (localIgnore.ignores(checkPath))
611
- continue;
612
- const fullPath = path.join(dirPath, entry.name);
613
- if (entry.isDirectory()) {
614
- await searchDir(fullPath, relPath, localIgnore);
609
+ return;
610
+ const localIgnore = ignore().add(ig);
611
+ try {
612
+ const localGitignorePath = path.join(dirPath, ".gitignore");
613
+ const content = await fs.readFile(localGitignorePath, "utf-8");
614
+ localIgnore.add(content);
615
+ }
616
+ catch {
617
+ // No local .gitignore
615
618
  }
616
- else if (entry.isFile()) {
617
- await searchFile(fullPath, relPath);
619
+ const entries = await fs.readdir(dirPath, { withFileTypes: true });
620
+ for (const entry of entries) {
621
+ if (matches.length >= maxResults)
622
+ break;
623
+ const relPath = relativePath ? `${relativePath}/${entry.name}` : entry.name;
624
+ const checkPath = entry.isDirectory() ? `${relPath}/` : relPath;
625
+ if (localIgnore.ignores(checkPath))
626
+ continue;
627
+ const fullPath = path.join(dirPath, entry.name);
628
+ if (entry.isDirectory()) {
629
+ await searchDir(fullPath, relPath, localIgnore);
630
+ }
631
+ else if (entry.isFile()) {
632
+ await searchFile(fullPath, relPath);
633
+ }
618
634
  }
619
635
  }
636
+ const stat = await fs.stat(safePath);
637
+ if (stat.isDirectory()) {
638
+ await searchDir(safePath, reqPath === "." ? "" : reqPath, rootIgnore);
639
+ }
640
+ else {
641
+ await searchFile(safePath, reqPath);
642
+ }
620
643
  }
621
- const stat = await fs.stat(safePath);
622
- if (stat.isDirectory()) {
623
- await searchDir(safePath, reqPath === "." ? "" : reqPath, rootIgnore);
624
- }
625
- else {
626
- await searchFile(safePath, reqPath);
644
+ finally {
645
+ shell.config.silent = previousSilent;
627
646
  }
628
647
  return { matches };
629
648
  }
@@ -1525,7 +1544,7 @@ function handleProcessesList() {
1525
1544
  }
1526
1545
  return { processes: result };
1527
1546
  }
1528
- function handleProcessesSpawn(payload) {
1547
+ async function handleProcessesSpawn(payload) {
1529
1548
  const command = payload.command;
1530
1549
  const args = payload.args || [];
1531
1550
  const cwd = payload.cwd;
@@ -1539,7 +1558,31 @@ function handleProcessesSpawn(payload) {
1539
1558
  stdio: ["pipe", "pipe", "pipe"],
1540
1559
  shell: false,
1541
1560
  });
1542
- const pid = proc.pid;
1561
+ const pid = await new Promise((resolve, reject) => {
1562
+ let settled = false;
1563
+ const handleSpawn = () => {
1564
+ if (settled)
1565
+ return;
1566
+ settled = true;
1567
+ proc.removeListener("error", handleError);
1568
+ if (!proc.pid) {
1569
+ reject(Object.assign(new Error(`Failed to spawn "${command}"`), { code: "ERROR" }));
1570
+ return;
1571
+ }
1572
+ resolve(proc.pid);
1573
+ };
1574
+ const handleError = (err) => {
1575
+ if (settled)
1576
+ return;
1577
+ settled = true;
1578
+ proc.removeListener("spawn", handleSpawn);
1579
+ reject(Object.assign(new Error(err.message || `Failed to spawn "${command}"`), {
1580
+ code: err.code || "ERROR",
1581
+ }));
1582
+ };
1583
+ proc.once("spawn", handleSpawn);
1584
+ proc.once("error", handleError);
1585
+ });
1543
1586
  const channel = `proc-${pid}`;
1544
1587
  const managedProc = {
1545
1588
  pid,
@@ -1569,6 +1612,17 @@ function handleProcessesSpawn(payload) {
1569
1612
  };
1570
1613
  proc.stdout?.on("data", sendOutput("stdout"));
1571
1614
  proc.stderr?.on("data", sendOutput("stderr"));
1615
+ proc.on("error", (err) => {
1616
+ const message = err.message || `Process "${command}" failed`;
1617
+ processOutputBuffers.set(channel, (processOutputBuffers.get(channel) || "") + `${message}\n`);
1618
+ emitAppEvent({
1619
+ v: 1,
1620
+ id: `evt-${Date.now()}`,
1621
+ ns: "processes",
1622
+ action: "output",
1623
+ payload: { pid, channel, stream: "stderr", data: `${message}\n` },
1624
+ });
1625
+ });
1572
1626
  proc.on("close", (code, signal) => {
1573
1627
  const msg = {
1574
1628
  v: 1,
@@ -2565,7 +2619,7 @@ async function processMessage(message) {
2565
2619
  result = handleProcessesList();
2566
2620
  break;
2567
2621
  case "spawn":
2568
- result = handleProcessesSpawn(payload);
2622
+ result = await handleProcessesSpawn(payload);
2569
2623
  break;
2570
2624
  case "kill":
2571
2625
  result = handleProcessesKill(payload);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lunel-cli",
3
- "version": "0.1.99",
3
+ "version": "0.1.101",
4
4
  "author": [
5
5
  {
6
6
  "name": "Soham Bharambe",
@@ -30,12 +30,14 @@
30
30
  "ignore": "^6.0.2",
31
31
  "libsodium-wrappers": "^0.7.15",
32
32
  "qrcode-terminal": "^0.12.0",
33
+ "shelljs": "^0.10.0",
33
34
  "ws": "^8.18.0"
34
35
  },
35
36
  "devDependencies": {
36
37
  "@types/minimatch": "^5.1.2",
37
38
  "@types/node": "^20.0.0",
38
39
  "@types/qrcode-terminal": "^0.12.2",
40
+ "@types/shelljs": "^0.10.0",
39
41
  "@types/ws": "^8.5.13",
40
42
  "typescript": "^5.0.0"
41
43
  },