UNPKG

chromancer

Version:

A powerful command-line interface for automating Chrome browser using Playwright. Perfect for web scraping, automation, testing, and browser workflows.

133 lines (132 loc) • 4.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@oclif/core"); const core_2 = require("@oclif/core"); const config_js_1 = require("../utils/config.js"); const chalk = { green: (s) => `\x1b[32m${s}\x1b[0m`, blue: (s) => `\x1b[34m${s}\x1b[0m`, yellow: (s) => `\x1b[33m${s}\x1b[0m`, cyan: (s) => `\x1b[36m${s}\x1b[0m`, gray: (s) => `\x1b[90m${s}\x1b[0m`, }; class Config extends core_2.Command { static description = 'Manage chromancer configuration'; static examples = [ '<%= config.bin %> <%= command.id %> get chrome.port', '<%= config.bin %> <%= command.id %> set chrome.port 9223', '<%= config.bin %> <%= command.id %> set ui.colorOutput false', '<%= config.bin %> <%= command.id %> list', '<%= config.bin %> <%= command.id %> reset', ]; static args = { action: core_1.Args.string({ description: 'Config action', required: true, options: ['get', 'set', 'list', 'reset', 'path'], }), key: core_1.Args.string({ description: 'Configuration key (e.g., chrome.port)', }), value: core_1.Args.string({ description: 'Value to set', }), }; static flags = { json: core_1.Flags.boolean({ description: 'Output in JSON format', default: false, }), }; async run() { const { args, flags } = await this.parse(Config); switch (args.action) { case 'get': await this.getConfig(args.key, flags.json); break; case 'set': await this.setConfig(args.key, args.value); break; case 'list': await this.listConfig(flags.json); break; case 'reset': await this.resetConfig(); break; case 'path': this.showPath(); break; default: this.error(`Unknown action: ${args.action}`); } } async getConfig(key, json) { if (!key) { this.error('Key is required for get action'); } const value = await config_js_1.config.get(key); if (value === undefined) { this.warn(`Configuration key not found: ${key}`); return; } if (json) { console.log(JSON.stringify(value)); } else { console.log(`${chalk.cyan(key)}: ${JSON.stringify(value, null, 2)}`); } } async setConfig(key, value) { if (!key || !value) { this.error('Key and value are required for set action'); } // Try to parse value as JSON let parsedValue = value; try { parsedValue = JSON.parse(value); } catch { // Keep as string if not valid JSON } await config_js_1.config.set(key, parsedValue); console.log(chalk.green(`āœ… Set ${key} = ${JSON.stringify(parsedValue)}`)); } async listConfig(json) { const configData = await config_js_1.config.load(); if (json) { console.log(JSON.stringify(configData, null, 2)); } else { console.log(chalk.cyan('\nšŸ“‹ Current Configuration:\n')); this.printConfig(configData); } } printConfig(obj, prefix = '') { Object.entries(obj).forEach(([key, value]) => { const fullKey = prefix ? `${prefix}.${key}` : key; if (typeof value === 'object' && value !== null && !Array.isArray(value)) { console.log(chalk.blue(`${fullKey}:`)); this.printConfig(value, fullKey); } else { console.log(` ${chalk.gray(fullKey)}: ${JSON.stringify(value)}`); } }); } async resetConfig() { console.log(chalk.yellow('āš ļø This will reset all configuration to defaults')); // Simple confirmation console.log('\nPress Enter to confirm or Ctrl+C to cancel'); await new Promise(resolve => { process.stdin.once('data', resolve); process.stdin.resume(); }); await config_js_1.config.reset(); console.log(chalk.green('āœ… Configuration reset to defaults')); } showPath() { console.log(chalk.cyan('šŸ“ Config file location:')); console.log(config_js_1.config.getConfigPath()); } } exports.default = Config;