@fgv/ts-res-browser-cli 5.0.0-5 → 5.0.0-6
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/.rush/temp/chunked-rush-logs/ts-res-browser-cli.build.chunks.jsonl +2 -2
- package/.rush/temp/{fc74ac6ba66e84321a437fc3494453b741d38afe.tar.log → e396976e8696a2ca8b87b25896a78681780d28b3.tar.log} +6 -6
- package/.rush/temp/operation/build/all.log +2 -2
- package/.rush/temp/operation/build/log-chunks.jsonl +2 -2
- package/.rush/temp/operation/build/state.json +1 -1
- package/.rush/temp/shrinkwrap-deps.json +1 -1
- package/README.md +35 -11
- package/lib/cli.d.ts +0 -4
- package/lib/cli.d.ts.map +1 -1
- package/lib/cli.js +8 -17
- package/lib/cli.js.map +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/options.d.ts +5 -0
- package/lib/options.d.ts.map +1 -1
- package/lib/options.js.map +1 -1
- package/lib/simpleBrowserLauncher.d.ts +32 -0
- package/lib/simpleBrowserLauncher.d.ts.map +1 -0
- package/lib/{browserLauncher.js → simpleBrowserLauncher.js} +53 -163
- package/lib/simpleBrowserLauncher.js.map +1 -0
- package/package.json +7 -6
- package/rush-logs/ts-res-browser-cli.build.cache.log +1 -1
- package/rush-logs/ts-res-browser-cli.build.log +2 -2
- package/src/cli.ts +17 -20
- package/src/index.ts +1 -1
- package/src/options.ts +6 -0
- package/src/{browserLauncher.ts → simpleBrowserLauncher.ts} +56 -186
- package/lib/browserLauncher.d.ts +0 -57
- package/lib/browserLauncher.d.ts.map +0 -1
- package/lib/browserLauncher.js.map +0 -1
|
@@ -20,24 +20,33 @@
|
|
|
20
20
|
* SOFTWARE.
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
|
-
import { exec
|
|
23
|
+
import { exec } from 'child_process';
|
|
24
24
|
import * as path from 'path';
|
|
25
25
|
import * as http from 'http';
|
|
26
26
|
import { Result, succeed, fail } from '@fgv/ts-utils';
|
|
27
27
|
import { IBrowseOptions } from './options';
|
|
28
28
|
import { zipArchiver } from './zipArchiver';
|
|
29
|
-
import * as fs from 'fs';
|
|
30
29
|
|
|
31
30
|
/**
|
|
32
|
-
*
|
|
31
|
+
* Simplified browser launcher using direct dependency on ts-res-browser
|
|
33
32
|
*/
|
|
34
|
-
export class
|
|
35
|
-
private _devServer?: ChildProcess;
|
|
33
|
+
export class SimpleBrowserLauncher {
|
|
36
34
|
/**
|
|
37
35
|
* Launches the browser with the specified options
|
|
38
36
|
*/
|
|
39
37
|
public async launch(options: IBrowseOptions): Promise<Result<void>> {
|
|
40
38
|
try {
|
|
39
|
+
// Validate options before proceeding
|
|
40
|
+
if (options.interactive && !options.dev && !options.serve && !options.url) {
|
|
41
|
+
return fail(
|
|
42
|
+
'Interactive mode requires either --dev/--serve to start a local server or --url to specify a remote server.\n\n' +
|
|
43
|
+
'Examples:\n' +
|
|
44
|
+
' ts-res-browser-cli --interactive --dev\n' +
|
|
45
|
+
' ts-res-browser-cli --interactive --serve\n' +
|
|
46
|
+
' ts-res-browser-cli --interactive --url https://example.com/ts-res-browser'
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
41
50
|
// Check if we need to create a ZIP archive for file-based inputs
|
|
42
51
|
let zipCreated = false;
|
|
43
52
|
let finalOptions = { ...options };
|
|
@@ -70,28 +79,16 @@ export class BrowserLauncher {
|
|
|
70
79
|
zipCreated = true;
|
|
71
80
|
}
|
|
72
81
|
|
|
73
|
-
// Start
|
|
74
|
-
if (finalOptions.dev && !finalOptions.url) {
|
|
82
|
+
// Start server if requested
|
|
83
|
+
if ((finalOptions.dev || finalOptions.serve) && !finalOptions.url) {
|
|
75
84
|
if (!finalOptions.quiet) {
|
|
76
|
-
console.log('Starting TS-RES Browser
|
|
85
|
+
console.log('Starting TS-RES Browser server...');
|
|
77
86
|
}
|
|
78
87
|
|
|
79
|
-
const serverResult = await this.
|
|
88
|
+
const serverResult = await this._startServer(finalOptions);
|
|
80
89
|
if (serverResult.isFailure()) {
|
|
81
90
|
return serverResult;
|
|
82
91
|
}
|
|
83
|
-
|
|
84
|
-
// Wait for server to be ready
|
|
85
|
-
const port = options.port || 3000;
|
|
86
|
-
const readyResult = await this._waitForServerReady(port, options.verbose || false);
|
|
87
|
-
if (readyResult.isFailure()) {
|
|
88
|
-
this._stopDevServer();
|
|
89
|
-
return readyResult;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (!options.quiet) {
|
|
93
|
-
console.log(`Development server started on port ${port}`);
|
|
94
|
-
}
|
|
95
92
|
}
|
|
96
93
|
|
|
97
94
|
// Build URL with parameters
|
|
@@ -136,6 +133,41 @@ export class BrowserLauncher {
|
|
|
136
133
|
}
|
|
137
134
|
}
|
|
138
135
|
|
|
136
|
+
/**
|
|
137
|
+
* Starts the server using ts-res-browser CLI directly
|
|
138
|
+
*/
|
|
139
|
+
private async _startServer(options: IBrowseOptions): Promise<Result<void>> {
|
|
140
|
+
try {
|
|
141
|
+
// Use ts-res-browser directly from node_modules
|
|
142
|
+
const browserPath = require.resolve('@fgv/ts-res-browser/cli.js');
|
|
143
|
+
const command = `node "${browserPath}" ${options.dev ? 'dev' : 'serve'}`;
|
|
144
|
+
|
|
145
|
+
if (options.verbose) {
|
|
146
|
+
console.log(`Running: ${command}`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return new Promise((resolve) => {
|
|
150
|
+
const child = exec(command, (error) => {
|
|
151
|
+
if (error) {
|
|
152
|
+
resolve(fail(`Failed to start server: ${error.message}`));
|
|
153
|
+
} else {
|
|
154
|
+
resolve(succeed(undefined));
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// Give server time to start, then assume success
|
|
159
|
+
setTimeout(() => {
|
|
160
|
+
if (options.verbose) {
|
|
161
|
+
console.log('Server startup initiated');
|
|
162
|
+
}
|
|
163
|
+
resolve(succeed(undefined));
|
|
164
|
+
}, 2000);
|
|
165
|
+
});
|
|
166
|
+
} catch (error) {
|
|
167
|
+
return fail(`Failed to start server: ${error}`);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
139
171
|
/**
|
|
140
172
|
* Opens the browser to the specified URL
|
|
141
173
|
*/
|
|
@@ -161,8 +193,9 @@ export class BrowserLauncher {
|
|
|
161
193
|
* Builds the URL with query parameters based on options
|
|
162
194
|
*/
|
|
163
195
|
private _buildUrl(options: IBrowseOptions): string {
|
|
164
|
-
// Use provided URL or default
|
|
165
|
-
const
|
|
196
|
+
// Use provided URL or default based on server type
|
|
197
|
+
const defaultPort = options.dev ? 3000 : 8080;
|
|
198
|
+
const baseUrl = options.url || `http://localhost:${options.port || defaultPort}`;
|
|
166
199
|
const params = new URLSearchParams();
|
|
167
200
|
|
|
168
201
|
if (options.input) {
|
|
@@ -213,162 +246,6 @@ export class BrowserLauncher {
|
|
|
213
246
|
return queryString ? `${baseUrl}?${queryString}` : baseUrl;
|
|
214
247
|
}
|
|
215
248
|
|
|
216
|
-
/**
|
|
217
|
-
* Starts the development server for ts-res-browser
|
|
218
|
-
*/
|
|
219
|
-
private async _startDevServer(options: IBrowseOptions): Promise<Result<void>> {
|
|
220
|
-
try {
|
|
221
|
-
const port = options.port || 3000;
|
|
222
|
-
const env = { ...process.env, PORT: port.toString() };
|
|
223
|
-
|
|
224
|
-
// Detect if we're in a monorepo or using published packages
|
|
225
|
-
const isMonorepo = this._isMonorepoEnvironment();
|
|
226
|
-
|
|
227
|
-
if (isMonorepo) {
|
|
228
|
-
// Monorepo development: use rushx from sibling project
|
|
229
|
-
const browserProjectPath = path.resolve(__dirname, '../../ts-res-browser');
|
|
230
|
-
|
|
231
|
-
if (options.verbose) {
|
|
232
|
-
console.log(`Looking for ts-res-browser at: ${browserProjectPath}`);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
this._devServer = spawn('rushx', ['dev'], {
|
|
236
|
-
cwd: browserProjectPath,
|
|
237
|
-
env,
|
|
238
|
-
stdio: options.verbose ? 'inherit' : ['ignore', 'pipe', 'pipe']
|
|
239
|
-
});
|
|
240
|
-
} else {
|
|
241
|
-
// Published packages: dev server not available
|
|
242
|
-
return fail(
|
|
243
|
-
'Development server is not available in published packages.\n\n' +
|
|
244
|
-
'The dev server requires webpack-cli and webpack-dev-server which are\n' +
|
|
245
|
-
'not included in published packages to keep them lightweight.\n\n' +
|
|
246
|
-
'To use the development server:\n' +
|
|
247
|
-
'1. Clone the repository\n' +
|
|
248
|
-
'2. Install dependencies: rush install\n' +
|
|
249
|
-
'3. Run: rushx dev from tools/ts-res-browser\n\n' +
|
|
250
|
-
'For published packages, omit the --dev flag to use static serving.'
|
|
251
|
-
);
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
this._devServer.on('error', (error) => {
|
|
255
|
-
return fail(`Failed to start development server: ${error}`);
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
// Give the server a moment to start
|
|
259
|
-
await this._sleep(3000);
|
|
260
|
-
|
|
261
|
-
if (this._devServer && !this._devServer.killed) {
|
|
262
|
-
if (options.verbose) {
|
|
263
|
-
console.log('Development server process started successfully');
|
|
264
|
-
}
|
|
265
|
-
return succeed(undefined);
|
|
266
|
-
} else {
|
|
267
|
-
return fail('Development server process failed to start');
|
|
268
|
-
}
|
|
269
|
-
} catch (error) {
|
|
270
|
-
return fail(`Failed to start development server: ${error}`);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* Waits for the development server to be ready by checking HTTP availability
|
|
276
|
-
*/
|
|
277
|
-
private async _waitForServerReady(
|
|
278
|
-
port: number,
|
|
279
|
-
verbose: boolean,
|
|
280
|
-
maxAttempts: number = 60
|
|
281
|
-
): Promise<Result<void>> {
|
|
282
|
-
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
283
|
-
try {
|
|
284
|
-
const isReady = await this._checkServerHealth(port);
|
|
285
|
-
if (isReady) {
|
|
286
|
-
if (verbose) {
|
|
287
|
-
console.log(`Server is ready at http://localhost:${port}`);
|
|
288
|
-
}
|
|
289
|
-
return succeed(undefined);
|
|
290
|
-
}
|
|
291
|
-
} catch (error) {
|
|
292
|
-
// Server not ready yet, continue waiting
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
if (verbose && attempt % 10 === 0) {
|
|
296
|
-
console.log(`Waiting for server to be ready... (attempt ${attempt}/${maxAttempts})`);
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
await this._sleep(1000);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
return fail(`Server did not become ready after ${maxAttempts} attempts`);
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/**
|
|
306
|
-
* Checks if the server is responding
|
|
307
|
-
*/
|
|
308
|
-
private async _checkServerHealth(port: number): Promise<boolean> {
|
|
309
|
-
return new Promise((resolve) => {
|
|
310
|
-
const request = http.get(`http://localhost:${port}`, (res) => {
|
|
311
|
-
resolve(res.statusCode !== undefined && res.statusCode >= 200 && res.statusCode < 400);
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
request.on('error', () => {
|
|
315
|
-
resolve(false);
|
|
316
|
-
});
|
|
317
|
-
|
|
318
|
-
request.setTimeout(3000, () => {
|
|
319
|
-
request.destroy();
|
|
320
|
-
resolve(false);
|
|
321
|
-
});
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
/**
|
|
326
|
-
* Stops the development server
|
|
327
|
-
*/
|
|
328
|
-
private _stopDevServer(): void {
|
|
329
|
-
if (this._devServer && !this._devServer.killed) {
|
|
330
|
-
this._devServer.kill('SIGTERM');
|
|
331
|
-
setTimeout(() => {
|
|
332
|
-
if (this._devServer && !this._devServer.killed) {
|
|
333
|
-
this._devServer.kill('SIGKILL');
|
|
334
|
-
}
|
|
335
|
-
}, 5000);
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* Sleep for the specified number of milliseconds
|
|
341
|
-
*/
|
|
342
|
-
private _sleep(ms: number): Promise<void> {
|
|
343
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
/**
|
|
347
|
-
* Detects if we're running in a monorepo environment
|
|
348
|
-
*/
|
|
349
|
-
private _isMonorepoEnvironment(): boolean {
|
|
350
|
-
try {
|
|
351
|
-
// Check if we have a rush.json file in parent directories (indicating monorepo)
|
|
352
|
-
let currentDir = __dirname;
|
|
353
|
-
for (let i = 0; i < 5; i++) {
|
|
354
|
-
// Look up to 5 levels up
|
|
355
|
-
const rushJsonPath = path.join(currentDir, 'rush.json');
|
|
356
|
-
if (fs.existsSync(rushJsonPath)) {
|
|
357
|
-
return true;
|
|
358
|
-
}
|
|
359
|
-
currentDir = path.dirname(currentDir);
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
// Also check if ts-res-browser sibling directory exists
|
|
363
|
-
const browserProjectPath = path.resolve(__dirname, '../../ts-res-browser');
|
|
364
|
-
return (
|
|
365
|
-
fs.existsSync(browserProjectPath) && fs.existsSync(path.join(browserProjectPath, 'package.json'))
|
|
366
|
-
);
|
|
367
|
-
} catch {
|
|
368
|
-
return false;
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
|
|
372
249
|
/**
|
|
373
250
|
* Determines if a ZIP archive should be created based on the options
|
|
374
251
|
*/
|
|
@@ -398,11 +275,4 @@ export class BrowserLauncher {
|
|
|
398
275
|
return fail(`Failed to create ZIP archive: ${error instanceof Error ? error.message : String(error)}`);
|
|
399
276
|
}
|
|
400
277
|
}
|
|
401
|
-
|
|
402
|
-
/**
|
|
403
|
-
* Graceful shutdown - stops development server if running
|
|
404
|
-
*/
|
|
405
|
-
public shutdown(): void {
|
|
406
|
-
this._stopDevServer();
|
|
407
|
-
}
|
|
408
278
|
}
|
package/lib/browserLauncher.d.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { Result } from '@fgv/ts-utils';
|
|
2
|
-
import { IBrowseOptions } from './options';
|
|
3
|
-
/**
|
|
4
|
-
* Manages launching the browser application with specified options
|
|
5
|
-
*/
|
|
6
|
-
export declare class BrowserLauncher {
|
|
7
|
-
private _devServer?;
|
|
8
|
-
/**
|
|
9
|
-
* Launches the browser with the specified options
|
|
10
|
-
*/
|
|
11
|
-
launch(options: IBrowseOptions): Promise<Result<void>>;
|
|
12
|
-
/**
|
|
13
|
-
* Opens the browser to the specified URL
|
|
14
|
-
*/
|
|
15
|
-
private _openBrowser;
|
|
16
|
-
/**
|
|
17
|
-
* Builds the URL with query parameters based on options
|
|
18
|
-
*/
|
|
19
|
-
private _buildUrl;
|
|
20
|
-
/**
|
|
21
|
-
* Starts the development server for ts-res-browser
|
|
22
|
-
*/
|
|
23
|
-
private _startDevServer;
|
|
24
|
-
/**
|
|
25
|
-
* Waits for the development server to be ready by checking HTTP availability
|
|
26
|
-
*/
|
|
27
|
-
private _waitForServerReady;
|
|
28
|
-
/**
|
|
29
|
-
* Checks if the server is responding
|
|
30
|
-
*/
|
|
31
|
-
private _checkServerHealth;
|
|
32
|
-
/**
|
|
33
|
-
* Stops the development server
|
|
34
|
-
*/
|
|
35
|
-
private _stopDevServer;
|
|
36
|
-
/**
|
|
37
|
-
* Sleep for the specified number of milliseconds
|
|
38
|
-
*/
|
|
39
|
-
private _sleep;
|
|
40
|
-
/**
|
|
41
|
-
* Detects if we're running in a monorepo environment
|
|
42
|
-
*/
|
|
43
|
-
private _isMonorepoEnvironment;
|
|
44
|
-
/**
|
|
45
|
-
* Determines if a ZIP archive should be created based on the options
|
|
46
|
-
*/
|
|
47
|
-
private _shouldCreateZip;
|
|
48
|
-
/**
|
|
49
|
-
* Creates a ZIP archive containing the input and config files
|
|
50
|
-
*/
|
|
51
|
-
private _createZipArchive;
|
|
52
|
-
/**
|
|
53
|
-
* Graceful shutdown - stops development server if running
|
|
54
|
-
*/
|
|
55
|
-
shutdown(): void;
|
|
56
|
-
}
|
|
57
|
-
//# sourceMappingURL=browserLauncher.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"browserLauncher.d.ts","sourceRoot":"","sources":["../src/browserLauncher.ts"],"names":[],"mappings":"AAyBA,OAAO,EAAE,MAAM,EAAiB,MAAM,eAAe,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAI3C;;GAEG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,UAAU,CAAC,CAAe;IAClC;;OAEG;IACU,MAAM,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IAoGnE;;OAEG;YACW,YAAY;IAkB1B;;OAEG;IACH,OAAO,CAAC,SAAS;IAqDjB;;OAEG;YACW,eAAe;IAuD7B;;OAEG;YACW,mBAAmB;IA4BjC;;OAEG;YACW,kBAAkB;IAiBhC;;OAEG;IACH,OAAO,CAAC,cAAc;IAWtB;;OAEG;IACH,OAAO,CAAC,MAAM;IAId;;OAEG;IACH,OAAO,CAAC,sBAAsB;IAuB9B;;OAEG;IACH,OAAO,CAAC,gBAAgB;IAWxB;;OAEG;YACW,iBAAiB;IAa/B;;OAEG;IACI,QAAQ,IAAI,IAAI;CAGxB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"browserLauncher.js","sourceRoot":"","sources":["../src/browserLauncher.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,iDAA0D;AAC1D,2CAA6B;AAC7B,2CAA6B;AAC7B,4CAAsD;AAEtD,+CAA4C;AAC5C,uCAAyB;AAEzB;;GAEG;AACH,MAAa,eAAe;IAE1B;;OAEG;IACI,KAAK,CAAC,MAAM,CAAC,OAAuB;QACzC,IAAI,CAAC;YACH,iEAAiE;YACjE,IAAI,UAAU,GAAG,KAAK,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AC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,IAAA,eAAI,EAAC,iCAAiC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACzG,CAAC;IACH,CAAC;IAED;;OAEG;IACI,QAAQ;QACb,IAAI,CAAC,cAAc,EAAE,CAAC;IACxB,CAAC;CACF;AAtXD,0CAsXC","sourcesContent":["/*\n * Copyright (c) 2025 Erik Fortune\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { exec, spawn, ChildProcess } from 'child_process';\nimport * as path from 'path';\nimport * as http from 'http';\nimport { Result, succeed, fail } from '@fgv/ts-utils';\nimport { IBrowseOptions } from './options';\nimport { zipArchiver } from './zipArchiver';\nimport * as fs from 'fs';\n\n/**\n * Manages launching the browser application with specified options\n */\nexport class BrowserLauncher {\n private _devServer?: ChildProcess;\n /**\n * Launches the browser with the specified options\n */\n public async launch(options: IBrowseOptions): Promise<Result<void>> {\n try {\n // Check if we need to create a ZIP archive for file-based inputs\n let zipCreated = false;\n let finalOptions = { ...options };\n\n if (this._shouldCreateZip(options)) {\n if (!options.quiet) {\n console.log('Creating ZIP archive for file-based resources...');\n }\n\n const zipResult = await this._createZipArchive(options);\n if (zipResult.isFailure()) {\n return fail(`Failed to create ZIP archive: ${zipResult.message}`);\n }\n\n if (!options.quiet) {\n console.log(`ZIP archive created: ${zipResult.value.zipPath}`);\n }\n\n // Modify options to use ZIP loading instead of file paths\n const zipFileName = path.basename(zipResult.value.zipPath);\n finalOptions = {\n ...options,\n input: undefined,\n config: undefined,\n // Add ZIP loading parameters\n loadZip: true,\n zipFile: zipFileName,\n zipPath: zipResult.value.zipPath\n };\n zipCreated = true;\n }\n\n // Start development server if requested\n if (finalOptions.dev && !finalOptions.url) {\n if (!finalOptions.quiet) {\n console.log('Starting TS-RES Browser development server...');\n }\n\n const serverResult = await this._startDevServer(finalOptions);\n if (serverResult.isFailure()) {\n return serverResult;\n }\n\n // Wait for server to be ready\n const port = options.port || 3000;\n const readyResult = await this._waitForServerReady(port, options.verbose || false);\n if (readyResult.isFailure()) {\n this._stopDevServer();\n return readyResult;\n }\n\n if (!options.quiet) {\n console.log(`Development server started on port ${port}`);\n }\n }\n\n // Build URL with parameters\n const url = this._buildUrl(finalOptions);\n\n if (!finalOptions.quiet) {\n console.log('Opening TS-RES Browser...');\n if (zipCreated) {\n console.log('ZIP archive created - browser will load from Downloads folder');\n } else {\n if (finalOptions.input) {\n console.log(`Resource file: ${finalOptions.input}`);\n }\n if (finalOptions.config) {\n console.log(`Configuration: ${finalOptions.config}`);\n }\n }\n if (finalOptions.contextFilter) {\n console.log(`Context filter: ${finalOptions.contextFilter}`);\n }\n console.log(`URL: ${url}`);\n }\n\n // Open browser if requested (default to true)\n const shouldOpen = finalOptions.open !== false;\n if (shouldOpen) {\n const openResult = await this._openBrowser(url, finalOptions.verbose || false);\n if (openResult.isFailure()) {\n return fail(`Could not open browser: ${openResult.message}`);\n }\n\n if (!finalOptions.quiet) {\n console.log('Browser opened successfully');\n }\n } else {\n console.log(`Browser URL: ${url}`);\n }\n\n return succeed(undefined);\n } catch (error) {\n return fail(`Failed to launch browser: ${error}`);\n }\n }\n\n /**\n * Opens the browser to the specified URL\n */\n private async _openBrowser(url: string, verbose: boolean): Promise<Result<void>> {\n return new Promise((resolve) => {\n const command =\n process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start \"\"' : 'xdg-open';\n\n exec(`${command} \"${url}\"`, (error) => {\n if (error) {\n resolve(fail(`Failed to open browser: ${error.message}`));\n } else {\n if (verbose) {\n console.log(`Opened browser to: ${url}`);\n }\n resolve(succeed(undefined));\n }\n });\n });\n }\n\n /**\n * Builds the URL with query parameters based on options\n */\n private _buildUrl(options: IBrowseOptions): string {\n // Use provided URL or default to localhost\n const baseUrl = options.url || `http://localhost:${options.port || 3000}`;\n const params = new URLSearchParams();\n\n if (options.input) {\n params.set('input', options.input);\n }\n\n if (options.config) {\n params.set('config', options.config);\n }\n\n if (options.contextFilter) {\n params.set('contextFilter', options.contextFilter);\n }\n\n if (options.qualifierDefaults) {\n params.set('qualifierDefaults', options.qualifierDefaults);\n }\n\n if (options.resourceTypes) {\n params.set('resourceTypes', options.resourceTypes);\n }\n\n if (options.maxDistance !== undefined) {\n params.set('maxDistance', options.maxDistance.toString());\n }\n\n if (options.reduceQualifiers) {\n params.set('reduceQualifiers', 'true');\n }\n\n if (options.interactive) {\n params.set('interactive', 'true');\n }\n\n if (options.loadZip) {\n params.set('loadZip', 'true');\n }\n\n if (options.zipFile) {\n params.set('zipFile', options.zipFile);\n }\n\n if (options.zipPath) {\n params.set('zipPath', options.zipPath);\n }\n\n const queryString = params.toString();\n return queryString ? `${baseUrl}?${queryString}` : baseUrl;\n }\n\n /**\n * Starts the development server for ts-res-browser\n */\n private async _startDevServer(options: IBrowseOptions): Promise<Result<void>> {\n try {\n const port = options.port || 3000;\n const env = { ...process.env, PORT: port.toString() };\n\n // Detect if we're in a monorepo or using published packages\n const isMonorepo = this._isMonorepoEnvironment();\n\n if (isMonorepo) {\n // Monorepo development: use rushx from sibling project\n const browserProjectPath = path.resolve(__dirname, '../../ts-res-browser');\n\n if (options.verbose) {\n console.log(`Looking for ts-res-browser at: ${browserProjectPath}`);\n }\n\n this._devServer = spawn('rushx', ['dev'], {\n cwd: browserProjectPath,\n env,\n stdio: options.verbose ? 'inherit' : ['ignore', 'pipe', 'pipe']\n });\n } else {\n // Published packages: dev server not available\n return fail(\n 'Development server is not available in published packages.\\n\\n' +\n 'The dev server requires webpack-cli and webpack-dev-server which are\\n' +\n 'not included in published packages to keep them lightweight.\\n\\n' +\n 'To use the development server:\\n' +\n '1. Clone the repository\\n' +\n '2. Install dependencies: rush install\\n' +\n '3. Run: rushx dev from tools/ts-res-browser\\n\\n' +\n 'For published packages, omit the --dev flag to use static serving.'\n );\n }\n\n this._devServer.on('error', (error) => {\n return fail(`Failed to start development server: ${error}`);\n });\n\n // Give the server a moment to start\n await this._sleep(3000);\n\n if (this._devServer && !this._devServer.killed) {\n if (options.verbose) {\n console.log('Development server process started successfully');\n }\n return succeed(undefined);\n } else {\n return fail('Development server process failed to start');\n }\n } catch (error) {\n return fail(`Failed to start development server: ${error}`);\n }\n }\n\n /**\n * Waits for the development server to be ready by checking HTTP availability\n */\n private async _waitForServerReady(\n port: number,\n verbose: boolean,\n maxAttempts: number = 60\n ): Promise<Result<void>> {\n for (let attempt = 1; attempt <= maxAttempts; attempt++) {\n try {\n const isReady = await this._checkServerHealth(port);\n if (isReady) {\n if (verbose) {\n console.log(`Server is ready at http://localhost:${port}`);\n }\n return succeed(undefined);\n }\n } catch (error) {\n // Server not ready yet, continue waiting\n }\n\n if (verbose && attempt % 10 === 0) {\n console.log(`Waiting for server to be ready... (attempt ${attempt}/${maxAttempts})`);\n }\n\n await this._sleep(1000);\n }\n\n return fail(`Server did not become ready after ${maxAttempts} attempts`);\n }\n\n /**\n * Checks if the server is responding\n */\n private async _checkServerHealth(port: number): Promise<boolean> {\n return new Promise((resolve) => {\n const request = http.get(`http://localhost:${port}`, (res) => {\n resolve(res.statusCode !== undefined && res.statusCode >= 200 && res.statusCode < 400);\n });\n\n request.on('error', () => {\n resolve(false);\n });\n\n request.setTimeout(3000, () => {\n request.destroy();\n resolve(false);\n });\n });\n }\n\n /**\n * Stops the development server\n */\n private _stopDevServer(): void {\n if (this._devServer && !this._devServer.killed) {\n this._devServer.kill('SIGTERM');\n setTimeout(() => {\n if (this._devServer && !this._devServer.killed) {\n this._devServer.kill('SIGKILL');\n }\n }, 5000);\n }\n }\n\n /**\n * Sleep for the specified number of milliseconds\n */\n private _sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n }\n\n /**\n * Detects if we're running in a monorepo environment\n */\n private _isMonorepoEnvironment(): boolean {\n try {\n // Check if we have a rush.json file in parent directories (indicating monorepo)\n let currentDir = __dirname;\n for (let i = 0; i < 5; i++) {\n // Look up to 5 levels up\n const rushJsonPath = path.join(currentDir, 'rush.json');\n if (fs.existsSync(rushJsonPath)) {\n return true;\n }\n currentDir = path.dirname(currentDir);\n }\n\n // Also check if ts-res-browser sibling directory exists\n const browserProjectPath = path.resolve(__dirname, '../../ts-res-browser');\n return (\n fs.existsSync(browserProjectPath) && fs.existsSync(path.join(browserProjectPath, 'package.json'))\n );\n } catch {\n return false;\n }\n }\n\n /**\n * Determines if a ZIP archive should be created based on the options\n */\n private _shouldCreateZip(options: IBrowseOptions): boolean {\n // Create ZIP if input or config are file paths (not predefined names)\n const hasFilePath = (path: string | undefined): boolean => {\n if (!path) return false;\n // Check if it's a file path (contains directory separators or file extensions)\n return path.includes('/') || path.includes('\\\\') || (path.includes('.') && !path.startsWith('.'));\n };\n\n return hasFilePath(options.input) || hasFilePath(options.config);\n }\n\n /**\n * Creates a ZIP archive containing the input and config files\n */\n private async _createZipArchive(\n options: IBrowseOptions\n ): Promise<Result<{ zipPath: string; manifest: any }>> {\n try {\n return await zipArchiver.createArchive({\n input: options.input,\n config: options.config\n });\n } catch (error) {\n return fail(`Failed to create ZIP archive: ${error instanceof Error ? error.message : String(error)}`);\n }\n }\n\n /**\n * Graceful shutdown - stops development server if running\n */\n public shutdown(): void {\n this._stopDevServer();\n }\n}\n"]}
|