chrome-cdp-cli 1.8.1 → 2.0.0

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.
@@ -1,16 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CLIApplication = void 0;
4
- const CLIInterface_1 = require("./CLIInterface");
4
+ const EnhancedCLIInterface_1 = require("./EnhancedCLIInterface");
5
+ const ConfigurationManager_1 = require("../config/ConfigurationManager");
5
6
  const ConnectionManager_1 = require("../connection/ConnectionManager");
6
7
  const handlers_1 = require("../handlers");
7
8
  const logger_1 = require("../utils/logger");
8
9
  const CommandRouter_1 = require("./CommandRouter");
9
10
  const ProxyManager_1 = require("../proxy/ProxyManager");
11
+ const OutputManager_1 = require("./OutputManager");
10
12
  class CLIApplication {
11
13
  constructor() {
12
- this.cli = new CLIInterface_1.CLIInterface();
14
+ this.cli = new EnhancedCLIInterface_1.EnhancedCLIInterface();
15
+ this.configManager = new ConfigurationManager_1.ConfigurationManager();
13
16
  this.connectionManager = new ConnectionManager_1.ConnectionManager();
17
+ this.outputManager = new OutputManager_1.OutputManager();
14
18
  this.logger = new logger_1.Logger();
15
19
  this.proxyManager = ProxyManager_1.ProxyManager.getInstance();
16
20
  this.setupHandlers();
@@ -19,9 +23,7 @@ class CLIApplication {
19
23
  this.cli.registerHandler(new handlers_1.EvaluateScriptHandler());
20
24
  this.cli.registerHandler(new handlers_1.TakeScreenshotHandler());
21
25
  this.cli.registerHandler(new handlers_1.TakeSnapshotHandler());
22
- this.cli.registerHandler(new handlers_1.GetConsoleMessageHandler());
23
26
  this.cli.registerHandler(new handlers_1.ListConsoleMessagesHandler());
24
- this.cli.registerHandler(new handlers_1.GetNetworkRequestHandler());
25
27
  this.cli.registerHandler(new handlers_1.ListNetworkRequestsHandler());
26
28
  this.cli.registerHandler(new handlers_1.InstallCursorCommandHandler());
27
29
  this.cli.registerHandler(new handlers_1.InstallClaudeSkillHandler());
@@ -47,7 +49,34 @@ class CLIApplication {
47
49
  }
48
50
  async run(argv) {
49
51
  try {
50
- const command = this.cli.parseArgs(argv);
52
+ let command;
53
+ try {
54
+ command = this.cli.parseArgs(argv);
55
+ }
56
+ catch (parseError) {
57
+ if (parseError instanceof Error) {
58
+ if (parseError.message === 'VERSION_COMMAND_EXECUTED') {
59
+ return CommandRouter_1.ExitCode.SUCCESS;
60
+ }
61
+ if (parseError.message === 'HELP_COMMAND_EXECUTED') {
62
+ return CommandRouter_1.ExitCode.SUCCESS;
63
+ }
64
+ }
65
+ const errorMessage = parseError instanceof Error ? parseError.message : String(parseError);
66
+ console.error(errorMessage);
67
+ return CommandRouter_1.ExitCode.INVALID_ARGUMENTS;
68
+ }
69
+ try {
70
+ const cliOptions = this.extractCLIOptions(argv);
71
+ const configSources = await this.configManager.createConfigurationSources(cliOptions);
72
+ const loadedConfig = await this.configManager.loadConfiguration(configSources);
73
+ command.config = { ...loadedConfig, ...command.config };
74
+ }
75
+ catch (configError) {
76
+ const errorMessage = `Configuration error: ${configError instanceof Error ? configError.message : String(configError)}`;
77
+ console.error(this.formatError(errorMessage, command.config));
78
+ return CommandRouter_1.ExitCode.CONFIG_ERROR;
79
+ }
51
80
  if (command.config.debug) {
52
81
  this.logger.setLevel(3);
53
82
  }
@@ -75,7 +104,7 @@ class CLIApplication {
75
104
  catch (error) {
76
105
  this.logger.debug('Error in CLIApplication.run:', error);
77
106
  const errorMessage = error instanceof Error ? error.message : String(error);
78
- console.error(`Error: ${errorMessage}`);
107
+ console.error(this.formatError(errorMessage, { outputFormat: 'text', verbose: false, quiet: false, debug: false, host: 'localhost', port: 9222, timeout: 30000 }));
79
108
  return CommandRouter_1.ExitCode.GENERAL_ERROR;
80
109
  }
81
110
  }
@@ -134,6 +163,56 @@ class CLIApplication {
134
163
  console.error(output);
135
164
  }
136
165
  }
166
+ extractCLIOptions(argv) {
167
+ const options = {};
168
+ const args = argv.slice(2);
169
+ for (let i = 0; i < args.length; i++) {
170
+ const arg = args[i];
171
+ if (arg === '--config' || arg === '-c') {
172
+ if (i + 1 < args.length) {
173
+ options.configFile = args[i + 1];
174
+ }
175
+ }
176
+ else if (arg === '--profile') {
177
+ if (i + 1 < args.length) {
178
+ options.profile = args[i + 1];
179
+ }
180
+ }
181
+ else if (arg === '--host' || arg === '-h') {
182
+ if (i + 1 < args.length) {
183
+ options.host = args[i + 1];
184
+ }
185
+ }
186
+ else if (arg === '--port' || arg === '-p') {
187
+ if (i + 1 < args.length) {
188
+ options.port = parseInt(args[i + 1], 10);
189
+ }
190
+ }
191
+ else if (arg === '--timeout' || arg === '-t') {
192
+ if (i + 1 < args.length) {
193
+ options.timeout = parseInt(args[i + 1], 10);
194
+ }
195
+ }
196
+ else if (arg === '--format' || arg === '-f') {
197
+ if (i + 1 < args.length) {
198
+ options.outputFormat = args[i + 1];
199
+ }
200
+ }
201
+ else if (arg === '--verbose' || arg === '-v') {
202
+ options.verbose = true;
203
+ }
204
+ else if (arg === '--quiet' || arg === '-q') {
205
+ options.quiet = true;
206
+ }
207
+ else if (arg === '--debug' || arg === '-d') {
208
+ options.debug = true;
209
+ }
210
+ }
211
+ return options;
212
+ }
213
+ formatError(message, config) {
214
+ return this.outputManager.formatError(message, config);
215
+ }
137
216
  async shutdown() {
138
217
  if (this.client) {
139
218
  try {
@@ -153,8 +232,14 @@ class CLIApplication {
153
232
  getCLI() {
154
233
  return this.cli;
155
234
  }
235
+ getConfigurationManager() {
236
+ return this.configManager;
237
+ }
156
238
  getConnectionManager() {
157
239
  return this.connectionManager;
158
240
  }
241
+ getOutputManager() {
242
+ return this.outputManager;
243
+ }
159
244
  }
160
245
  exports.CLIApplication = CLIApplication;
@@ -39,12 +39,14 @@ const path = __importStar(require("path"));
39
39
  const CLIInterface_1 = require("../interfaces/CLIInterface");
40
40
  const CommandRegistry_1 = require("./CommandRegistry");
41
41
  const CommandRouter_1 = require("./CommandRouter");
42
+ const OutputManager_1 = require("./OutputManager");
42
43
  const packageJson = require('../../package.json');
43
44
  class CLIInterface {
44
45
  constructor() {
45
46
  this.program = new commander_1.Command();
46
47
  this.registry = new CommandRegistry_1.CommandRegistry();
47
48
  this.router = new CommandRouter_1.CommandRouter(this.registry);
49
+ this.outputManager = new OutputManager_1.OutputManager();
48
50
  this.setupProgram();
49
51
  }
50
52
  setupProgram() {
@@ -57,7 +59,7 @@ class CLIInterface {
57
59
  this.program
58
60
  .option('-h, --host <host>', 'Chrome host address', CLIInterface_1.DEFAULT_CLI_CONFIG.host)
59
61
  .option('-p, --port <port>', 'DevTools port', (value) => parseInt(value, 10), CLIInterface_1.DEFAULT_CLI_CONFIG.port)
60
- .option('-f, --format <format>', 'Output format (json|text)', CLIInterface_1.DEFAULT_CLI_CONFIG.outputFormat)
62
+ .option('-f, --format <format>', 'Output format (json|text|yaml)', CLIInterface_1.DEFAULT_CLI_CONFIG.outputFormat)
61
63
  .option('-v, --verbose', 'Enable verbose logging', CLIInterface_1.DEFAULT_CLI_CONFIG.verbose)
62
64
  .option('-q, --quiet', 'Enable quiet mode', CLIInterface_1.DEFAULT_CLI_CONFIG.quiet)
63
65
  .option('-t, --timeout <timeout>', 'Command timeout in milliseconds', (value) => parseInt(value, 10), CLIInterface_1.DEFAULT_CLI_CONFIG.timeout)
@@ -201,8 +203,8 @@ class CLIInterface {
201
203
  if (config.port !== undefined && (typeof config.port !== 'number' || config.port < 1 || config.port > 65535)) {
202
204
  throw new Error('Configuration "port" must be a number between 1 and 65535');
203
205
  }
204
- if (config.outputFormat !== undefined && !['json', 'text'].includes(config.outputFormat)) {
205
- throw new Error('Configuration "outputFormat" must be "json" or "text"');
206
+ if (config.outputFormat !== undefined && !['json', 'text', 'yaml'].includes(config.outputFormat)) {
207
+ throw new Error('Configuration "outputFormat" must be "json", "text", or "yaml"');
206
208
  }
207
209
  if (config.verbose !== undefined && typeof config.verbose !== 'boolean') {
208
210
  throw new Error('Configuration "verbose" must be a boolean');
@@ -343,7 +345,9 @@ class CLIInterface {
343
345
  }
344
346
  async execute(command) {
345
347
  try {
346
- return await this.router.execute(command);
348
+ this.outputManager.startOperation(command.name, command);
349
+ const result = await this.router.execute(command);
350
+ return result;
347
351
  }
348
352
  catch (error) {
349
353
  return {
@@ -354,84 +358,11 @@ class CLIInterface {
354
358
  }
355
359
  }
356
360
  formatOutput(result, format) {
357
- if (format === 'json') {
358
- return JSON.stringify(result, null, 2);
359
- }
360
- if (!result.success) {
361
- return `Error: ${result.error}`;
362
- }
363
- if (result.data === undefined || result.data === null) {
364
- return 'Success';
365
- }
366
- let output = '';
367
- let dataSourceInfo = '';
368
- if (result.dataSource === 'proxy' && result.hasHistoricalData) {
369
- dataSourceInfo = '📊 Data from proxy server (includes historical data)\n';
370
- }
371
- else if (result.dataSource === 'direct' && result.hasHistoricalData === false) {
372
- dataSourceInfo = '⚠️ Data from direct connection (new messages only, no historical data)\n';
373
- }
374
- if (result.data && typeof result.data === 'object') {
375
- const data = result.data;
376
- if (data.snapshot && typeof data.snapshot === 'string') {
377
- return data.snapshot;
378
- }
379
- if (data.messages && Array.isArray(data.messages)) {
380
- output += dataSourceInfo;
381
- if (data.messages.length === 0) {
382
- output += 'No console messages found.';
383
- }
384
- else {
385
- output += `Found ${data.messages.length} console message(s):\n\n`;
386
- data.messages.forEach((msg, index) => {
387
- const timestamp = new Date(msg.timestamp).toISOString();
388
- output += `[${index + 1}] ${timestamp} [${msg.type.toUpperCase()}] ${msg.text}\n`;
389
- if (msg.args && msg.args.length > 0) {
390
- output += ` Args: ${JSON.stringify(msg.args)}\n`;
391
- }
392
- });
393
- }
394
- return output.trim();
395
- }
396
- if (data.requests && Array.isArray(data.requests)) {
397
- output += dataSourceInfo;
398
- if (data.requests.length === 0) {
399
- output += 'No network requests found.';
400
- }
401
- else {
402
- output += `Found ${data.requests.length} network request(s):\n\n`;
403
- data.requests.forEach((req, index) => {
404
- const timestamp = new Date(req.timestamp).toISOString();
405
- const status = req.status ? ` [${req.status}]` : ' [pending]';
406
- output += `[${index + 1}] ${timestamp} ${req.method} ${req.url}${status}\n`;
407
- });
408
- }
409
- return output.trim();
410
- }
411
- if (data.type && data.text !== undefined && data.timestamp) {
412
- output += dataSourceInfo;
413
- const timestamp = new Date(data.timestamp).toISOString();
414
- output += `${timestamp} [${data.type.toUpperCase()}] ${data.text}`;
415
- if (data.args && data.args.length > 0) {
416
- output += `\nArgs: ${JSON.stringify(data.args)}`;
417
- }
418
- return output;
419
- }
420
- if (data.requestId && data.url && data.method) {
421
- output += dataSourceInfo;
422
- const timestamp = new Date(data.timestamp).toISOString();
423
- const status = data.status ? ` [${data.status}]` : ' [pending]';
424
- output += `${timestamp} ${data.method} ${data.url}${status}`;
425
- return output;
426
- }
427
- }
428
- if (typeof result.data === 'string') {
429
- return result.data;
430
- }
431
- if (typeof result.data === 'object') {
432
- return JSON.stringify(result.data, null, 2);
433
- }
434
- return String(result.data);
361
+ const config = {
362
+ ...CLIInterface_1.DEFAULT_CLI_CONFIG,
363
+ outputFormat: format
364
+ };
365
+ return this.outputManager.formatOutput(result, config);
435
366
  }
436
367
  showHelp(commandName) {
437
368
  if (commandName) {
@@ -460,5 +391,17 @@ class CLIInterface {
460
391
  getRouter() {
461
392
  return this.router;
462
393
  }
394
+ getOutputManager() {
395
+ return this.outputManager;
396
+ }
397
+ formatOutputWithTemplate(result, config, template) {
398
+ return this.outputManager.formatOutput(result, config, template);
399
+ }
400
+ formatError(error, config, exitCode = 1) {
401
+ return this.outputManager.formatError(error, config, exitCode);
402
+ }
403
+ formatSuccess(data, config) {
404
+ return this.outputManager.formatSuccess(config, data);
405
+ }
463
406
  }
464
407
  exports.CLIInterface = CLIInterface;
@@ -11,6 +11,8 @@ var ExitCode;
11
11
  ExitCode[ExitCode["TIMEOUT_ERROR"] = 4] = "TIMEOUT_ERROR";
12
12
  ExitCode[ExitCode["VALIDATION_ERROR"] = 5] = "VALIDATION_ERROR";
13
13
  ExitCode[ExitCode["FILE_ERROR"] = 6] = "FILE_ERROR";
14
+ ExitCode[ExitCode["CONFIG_ERROR"] = 7] = "CONFIG_ERROR";
15
+ ExitCode[ExitCode["INVALID_ARGUMENTS"] = 8] = "INVALID_ARGUMENTS";
14
16
  })(ExitCode || (exports.ExitCode = ExitCode = {}));
15
17
  class CommandRouter {
16
18
  constructor(registry) {
@@ -266,10 +268,8 @@ For more information about a specific command, use:
266
268
  'snapshot': 'Capture DOM snapshot with structure and styles',
267
269
  'console-messages': 'Get console messages',
268
270
  'network-requests': 'Get network requests',
269
- 'get_console_message': 'Get the latest console message',
270
- 'list_console_messages': 'List all console messages with filtering',
271
- 'get_network_request': 'Get the latest network request',
272
- 'list_network_requests': 'List all network requests with filtering',
271
+ 'console': 'List console messages',
272
+ 'network': 'List network requests',
273
273
  'install_cursor_command': 'Install Cursor IDE commands for Chrome automation',
274
274
  'install_claude_skill': 'Install Claude Code skill for Chrome automation',
275
275
  'help': 'Show help information'