@fatdoge/wtree 0.1.2 → 0.1.3

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.
@@ -76,10 +76,48 @@ export async function startUiDevServer(options: {
76
76
  // Close the temporary apiServer we created earlier since we are using combinedApp now
77
77
  apiServer.close()
78
78
 
79
- const combinedServer = combinedApp.listen(uiPort, '127.0.0.1')
80
- await new Promise<void>((resolve) => combinedServer.once('listening', () => resolve()))
79
+ // Use port 0 if uiPort is not specified or if user wants random port (handled by caller passing 0?)
80
+ // If uiPort is 5173 (default), we might want to try it, and failover?
81
+ // But standard express behavior for port 0 is random port.
82
+ // If user explicitly passed a port, we should use it and fail if taken.
83
+ // If default (5173), we should probably try it, but if taken, maybe random?
84
+ // Let's implement simple retry logic or just use random port if default fails?
85
+ // The current error EADDRINUSE suggests we are forcing 5173.
81
86
 
82
- const url = `http://127.0.0.1:${uiPort}/`
87
+ // Let's modify the logic:
88
+ // If user provided a specific port (not default 5173 logic), use it.
89
+ // If we are using default 5173, we can try it, but to be safe and avoid conflicts,
90
+ // maybe we should just use port 0 (random) by default unless specified?
91
+ // Or keep 5173 as preference but fallback to 0 if busy.
92
+
93
+ let finalPort = uiPort
94
+ let combinedServer: ReturnType<typeof combinedApp.listen>
95
+
96
+ try {
97
+ combinedServer = await startServer(combinedApp, uiPort)
98
+ } catch (e: unknown) {
99
+ const err = e as { code?: string }
100
+ if (err.code === 'EADDRINUSE' && uiPort === 5173 && !options.uiPort) {
101
+ // Retry with random port if default port 5173 is taken and user didn't force it
102
+ combinedServer = await startServer(combinedApp, 0)
103
+ const addr = combinedServer.address()
104
+ if (typeof addr === 'object' && addr) {
105
+ finalPort = addr.port
106
+ }
107
+ } else {
108
+ throw e
109
+ }
110
+ }
111
+
112
+ // const combinedServer = combinedApp.listen(uiPort, '127.0.0.1')
113
+ // await new Promise<void>((resolve) => combinedServer.once('listening', () => resolve()))
114
+
115
+ const address = combinedServer.address()
116
+ if (typeof address === 'object' && address) {
117
+ finalPort = address.port
118
+ }
119
+
120
+ const url = `http://127.0.0.1:${finalPort}/`
83
121
 
84
122
  if (open) {
85
123
  openPath(url)
@@ -92,3 +130,11 @@ export async function startUiDevServer(options: {
92
130
  },
93
131
  }
94
132
  }
133
+
134
+ function startServer(app: express.Express, port: number): Promise<ReturnType<typeof app.listen>> {
135
+ return new Promise((resolve, reject) => {
136
+ const server = app.listen(port, '127.0.0.1')
137
+ server.once('listening', () => resolve(server))
138
+ server.once('error', (err) => reject(err))
139
+ })
140
+ }
@@ -52,9 +52,44 @@ export async function startUiDevServer(options) {
52
52
  });
53
53
  // Close the temporary apiServer we created earlier since we are using combinedApp now
54
54
  apiServer.close();
55
- const combinedServer = combinedApp.listen(uiPort, '127.0.0.1');
56
- await new Promise((resolve) => combinedServer.once('listening', () => resolve()));
57
- const url = `http://127.0.0.1:${uiPort}/`;
55
+ // Use port 0 if uiPort is not specified or if user wants random port (handled by caller passing 0?)
56
+ // If uiPort is 5173 (default), we might want to try it, and failover?
57
+ // But standard express behavior for port 0 is random port.
58
+ // If user explicitly passed a port, we should use it and fail if taken.
59
+ // If default (5173), we should probably try it, but if taken, maybe random?
60
+ // Let's implement simple retry logic or just use random port if default fails?
61
+ // The current error EADDRINUSE suggests we are forcing 5173.
62
+ // Let's modify the logic:
63
+ // If user provided a specific port (not default 5173 logic), use it.
64
+ // If we are using default 5173, we can try it, but to be safe and avoid conflicts,
65
+ // maybe we should just use port 0 (random) by default unless specified?
66
+ // Or keep 5173 as preference but fallback to 0 if busy.
67
+ let finalPort = uiPort;
68
+ let combinedServer;
69
+ try {
70
+ combinedServer = await startServer(combinedApp, uiPort);
71
+ }
72
+ catch (e) {
73
+ const err = e;
74
+ if (err.code === 'EADDRINUSE' && uiPort === 5173 && !options.uiPort) {
75
+ // Retry with random port if default port 5173 is taken and user didn't force it
76
+ combinedServer = await startServer(combinedApp, 0);
77
+ const addr = combinedServer.address();
78
+ if (typeof addr === 'object' && addr) {
79
+ finalPort = addr.port;
80
+ }
81
+ }
82
+ else {
83
+ throw e;
84
+ }
85
+ }
86
+ // const combinedServer = combinedApp.listen(uiPort, '127.0.0.1')
87
+ // await new Promise<void>((resolve) => combinedServer.once('listening', () => resolve()))
88
+ const address = combinedServer.address();
89
+ if (typeof address === 'object' && address) {
90
+ finalPort = address.port;
91
+ }
92
+ const url = `http://127.0.0.1:${finalPort}/`;
58
93
  if (open) {
59
94
  openPath(url);
60
95
  }
@@ -65,3 +100,10 @@ export async function startUiDevServer(options) {
65
100
  },
66
101
  };
67
102
  }
103
+ function startServer(app, port) {
104
+ return new Promise((resolve, reject) => {
105
+ const server = app.listen(port, '127.0.0.1');
106
+ server.once('listening', () => resolve(server));
107
+ server.once('error', (err) => reject(err));
108
+ });
109
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fatdoge/wtree",
3
3
  "private": false,
4
- "version": "0.1.2",
4
+ "version": "0.1.3",
5
5
  "description": "CLI + UI tool for managing git worktrees",
6
6
  "keywords": [
7
7
  "git",