specrails-hub 1.2.0 → 1.3.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Javier Pulido
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -24,6 +24,7 @@ exports.formatDuration = formatDuration;
24
24
  exports.formatTokens = formatTokens;
25
25
  exports.printSummary = printSummary;
26
26
  const http_1 = __importDefault(require("http"));
27
+ const net_1 = __importDefault(require("net"));
27
28
  const child_process_1 = require("child_process");
28
29
  const child_process_2 = require("child_process");
29
30
  const readline_1 = require("readline");
@@ -607,6 +608,20 @@ async function handleJobs(port) {
607
608
  // Hub subcommand group
608
609
  // ---------------------------------------------------------------------------
609
610
  const HUB_PID_FILE = path_1.default.join(os_1.default.homedir(), '.specrails', 'manager.pid');
611
+ const HUB_LOG_FILE = path_1.default.join(os_1.default.homedir(), '.specrails', 'hub.log');
612
+ function isPortInUse(port) {
613
+ return new Promise((resolve) => {
614
+ const srv = net_1.default.createServer();
615
+ srv.once('error', (err) => {
616
+ resolve(err.code === 'EADDRINUSE');
617
+ });
618
+ srv.once('listening', () => {
619
+ srv.close();
620
+ resolve(false);
621
+ });
622
+ srv.listen(port, '127.0.0.1');
623
+ });
624
+ }
610
625
  function readPid() {
611
626
  try {
612
627
  const raw = fs_1.default.readFileSync(HUB_PID_FILE, 'utf-8').trim();
@@ -627,17 +642,20 @@ function isProcessRunning(pid) {
627
642
  }
628
643
  }
629
644
  function hubServerPath() {
630
- // CLI lives at cli/dist/specrails-hub.js; server is at server/dist/index.js (built)
631
- // In dev mode with tsx, we resolve from __filename
632
- const base = path_1.default.resolve(__dirname, '..');
633
- // Try compiled JS first, then fall back to tsx dev path
634
- const compiled = path_1.default.join(base, 'server', 'dist', 'index.js');
635
- const devTs = path_1.default.join(base, 'server', 'index.ts');
636
- if (fs_1.default.existsSync(compiled))
637
- return compiled;
645
+ // __dirname differs by runtime:
646
+ // compiled (npm install): <root>/cli/dist/ → need ../../server/dist/index.js
647
+ // tsx dev: <root>/cli/ → need ../server/dist/index.js
648
+ // Try both, compiled path first.
649
+ const fromDist = path_1.default.resolve(__dirname, '..', '..', 'server', 'dist', 'index.js');
650
+ const fromSrc = path_1.default.resolve(__dirname, '..', 'server', 'dist', 'index.js');
651
+ const devTs = path_1.default.resolve(__dirname, '..', 'server', 'index.ts');
652
+ if (fs_1.default.existsSync(fromDist))
653
+ return fromDist;
654
+ if (fs_1.default.existsSync(fromSrc))
655
+ return fromSrc;
638
656
  if (fs_1.default.existsSync(devTs))
639
657
  return devTs;
640
- return compiled;
658
+ return fromDist;
641
659
  }
642
660
  async function hubStart(port) {
643
661
  const pid = readPid();
@@ -645,25 +663,54 @@ async function hubStart(port) {
645
663
  cliLog(`hub already running (pid ${pid}) on port ${port}`);
646
664
  return 0;
647
665
  }
666
+ // Check if port is already in use by another process
667
+ const portBusy = await isPortInUse(port);
668
+ if (portBusy) {
669
+ cliError(`port ${port} is already in use by another process`);
670
+ cliError(`if a previous hub is stale, run: specrails-hub stop`);
671
+ cliError(`or use a different port: specrails-hub --port <port> start`);
672
+ return 1;
673
+ }
648
674
  const serverPath = hubServerPath();
649
675
  const isTs = serverPath.endsWith('.ts');
650
676
  const args = isTs
651
677
  ? ['tsx', serverPath, '--port', String(port)]
652
678
  : ['node', serverPath, '--port', String(port)];
679
+ // Ensure log dir exists and open log file for server output
680
+ try {
681
+ fs_1.default.mkdirSync(path_1.default.dirname(HUB_LOG_FILE), { recursive: true });
682
+ }
683
+ catch { /* ignore */ }
684
+ let logFd;
685
+ try {
686
+ logFd = fs_1.default.openSync(HUB_LOG_FILE, 'a');
687
+ }
688
+ catch { /* ignore — fall back to silent */ }
689
+ const stdio = [
690
+ 'ignore',
691
+ logFd ?? 'ignore',
692
+ logFd ?? 'ignore',
693
+ ];
653
694
  const child = (0, child_process_2.spawn)(args[0], args.slice(1), {
654
695
  detached: true,
655
- stdio: 'ignore',
696
+ stdio,
656
697
  env: { ...process.env },
657
698
  });
699
+ if (logFd !== undefined) {
700
+ try {
701
+ fs_1.default.closeSync(logFd);
702
+ }
703
+ catch { /* ignore */ }
704
+ }
658
705
  child.unref();
659
- // Wait briefly and confirm it started
660
- await new Promise((resolve) => setTimeout(resolve, 800));
706
+ // Wait for server to be ready (longer window for slow machines)
707
+ await new Promise((resolve) => setTimeout(resolve, 2000));
661
708
  const detection = await detectWebManager(port);
662
709
  if (detection.running) {
663
710
  cliLog(`hub started on http://127.0.0.1:${port}`);
664
711
  return 0;
665
712
  }
666
- cliError('hub failed to start (check logs)');
713
+ cliError(`hub failed to start logs: ${HUB_LOG_FILE}`);
667
714
  return 1;
668
715
  }
669
716
  async function hubStop() {