UNPKG

koneko-cli

Version:

Your CLI for reading manga from the terminal

217 lines (216 loc) 11.3 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_extra_1 = __importDefault(require("fs-extra")); const os_1 = __importDefault(require("os")); const path_1 = __importDefault(require("path")); const chalk_1 = __importDefault(require("chalk")); const child_process_1 = require("child_process"); const Launcher_1 = require("./utils/Launcher"); const SettingsConfig_1 = __importDefault(require("./utils/SettingsConfig")); const DebugLogger_1 = require("./utils/events/DebugLogger"); const DebugLogger_2 = require("./utils/events/DebugLogger"); SettingsConfig_1.default.set("foo", "bar"); DebugLogger_2.logger.debug('SettingsConfig: set foo=bar'); process.on('SIGINT', () => { DebugLogger_2.logger.debug('Quit by force (Ctrl+C detected)'); console.log('Quit by force (Ctrl+C detected)'); process.exit(0); }); process.on('SIGTERM', () => { DebugLogger_2.logger.debug('Quit by force (SIGTERM detected)'); console.log('Quit by force (SIGTERM detected)'); process.exit(0); }); function printSystemStatus() { DebugLogger_2.logger.debug('Collecting detailed system status snapshot...'); const totalMemMB = (os_1.default.totalmem() / 1024 / 1024).toFixed(0); const freeMemMB = (os_1.default.freemem() / 1024 / 1024).toFixed(0); const uptimeMin = (os_1.default.uptime() / 60).toFixed(0); const cpus = os_1.default.cpus(); console.log(chalk_1.default.magenta("🖥️ System Status:")); console.log(chalk_1.default.magenta(`📦 Platform: ${os_1.default.platform()} ${os_1.default.arch()}`)); console.log(chalk_1.default.magenta(`🧠 Memory: ${freeMemMB}MB free / ${totalMemMB}MB total`)); console.log(chalk_1.default.magenta(`⏱️ Uptime: ${uptimeMin} minutes`)); console.log(chalk_1.default.magenta(`💻 CPUs: ${cpus.length} × ${cpus[0].model}`)); console.log(chalk_1.default.magenta(`🔢 Node Version: ${process.version}`)); console.log(chalk_1.default.magenta(`📁 CLI Path: ${__filename}`)); console.log(chalk_1.default.magenta(`📂 Working Directory: ${process.cwd()}`)); DebugLogger_2.logger.debug(`System platform: ${os_1.default.platform()} ${os_1.default.arch()}`); DebugLogger_2.logger.debug(`Total memory: ${totalMemMB} MB, Free memory: ${freeMemMB} MB`); DebugLogger_2.logger.debug(`System uptime: ${uptimeMin} minutes`); DebugLogger_2.logger.debug(`CPU Info: ${JSON.stringify(cpus, null, 2)}`); DebugLogger_2.logger.debug(`Node Version: ${process.version}`); DebugLogger_2.logger.debug(`CLI File Path: ${__filename}`); DebugLogger_2.logger.debug(`Working Directory: ${process.cwd()}`); } function printFullDebugSnapshot() { DebugLogger_2.logger.debug('=== FULL DEBUG SNAPSHOT ==='); DebugLogger_2.logger.debug(`ENV: ${JSON.stringify(process.env, null, 2)}`); DebugLogger_2.logger.debug(`SettingsConfig snapshot: ${JSON.stringify(SettingsConfig_1.default.getAll ? SettingsConfig_1.default.getAll() : { foo: SettingsConfig_1.default.get("foo") })}`); DebugLogger_2.logger.debug(`Process CWD: ${process.cwd()}`); DebugLogger_2.logger.debug(`Process argv: ${JSON.stringify(process.argv)}`); DebugLogger_2.logger.debug(`Process versions: ${JSON.stringify(process.versions, null, 2)}`); DebugLogger_2.logger.debug(`OS network interfaces: ${JSON.stringify(os_1.default.networkInterfaces(), null, 2)}`); DebugLogger_2.logger.debug('=========================='); } function findLocalChrome() { DebugLogger_2.logger.debug('Searching for local Chrome...'); const localPath = path_1.default.resolve(__dirname, '../local-chromium'); DebugLogger_2.logger.debug(`Checking localChromium path: ${localPath}`); if (!fs_extra_1.default.existsSync(localPath)) { DebugLogger_2.logger.debug('local-chromium folder does not exist.'); return null; } const dirs = fs_extra_1.default.readdirSync(localPath); DebugLogger_2.logger.debug(`Found directories in local-chromium: ${dirs.join(', ')}`); for (const dir of dirs) { const chromePath = path_1.default.join(localPath, dir, 'chrome-win', 'chrome.exe'); DebugLogger_2.logger.debug(`Checking path: ${chromePath}`); if (fs_extra_1.default.existsSync(chromePath)) { DebugLogger_2.logger.debug(`Found local Chrome executable at: ${chromePath}`); return chromePath; } } DebugLogger_2.logger.debug('No local Chrome executable found.'); return null; } function findSystemChrome() { DebugLogger_2.logger.debug('Searching for system Chrome...'); const platform = os_1.default.platform(); DebugLogger_2.logger.debug(`Detected platform: ${platform}`); try { if (platform === 'win32') { const output = (0, child_process_1.execSync)('where chrome', { stdio: ['pipe', 'pipe', 'ignore'] }).toString(); const paths = output.split(/\r?\n/).filter(Boolean); DebugLogger_2.logger.debug(`Windows chrome paths: ${paths.join(', ')}`); return paths[0] || null; } if (platform === 'darwin') { const macPaths = [ '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', '/Applications/Chromium.app/Contents/MacOS/Chromium', path_1.default.join(os_1.default.homedir(), 'Applications/Google Chrome.app/Contents/MacOS/Google Chrome') ]; DebugLogger_2.logger.debug(`Checking macOS chrome paths: ${macPaths.join(', ')}`); for (const p of macPaths) { if (fs_extra_1.default.existsSync(p)) { DebugLogger_2.logger.debug(`Found macOS Chrome executable at: ${p}`); return p; } } } if (platform === 'linux') { const binaries = ['google-chrome-stable', 'google-chrome', 'chromium-browser', 'chromium']; DebugLogger_2.logger.debug(`Checking Linux chrome binaries: ${binaries.join(', ')}`); for (const bin of binaries) { try { const binPath = (0, child_process_1.execSync)(`which ${bin}`, { stdio: ['pipe', 'pipe', 'ignore'] }).toString().trim(); DebugLogger_2.logger.debug(`'which ${bin}' returned: ${binPath}`); if (binPath && fs_extra_1.default.existsSync(binPath)) { DebugLogger_2.logger.debug(`Found Linux Chrome executable at: ${binPath}`); return binPath; } } catch (_a) { DebugLogger_2.logger.debug(`'which ${bin}' failed.`); continue; } } } } catch (err) { DebugLogger_2.logger.debug('Error searching for system Chrome:', err); return null; } DebugLogger_2.logger.debug('No system Chrome executable found.'); return null; } function findGit() { DebugLogger_2.logger.debug('Searching for git on system...'); const platform = os_1.default.platform(); try { if (platform === 'win32') { const output = (0, child_process_1.execSync)('where git', { stdio: ['pipe', 'pipe', 'ignore'] }).toString(); const paths = output.split(/\r?\n/).filter(Boolean); DebugLogger_2.logger.debug(`Windows git paths: ${paths.join(', ')}`); return paths[0] || null; } if (platform === 'darwin' || platform === 'linux') { const gitPath = (0, child_process_1.execSync)('which git', { stdio: ['pipe', 'pipe', 'ignore'] }).toString().trim(); DebugLogger_2.logger.debug(`which git returned: ${gitPath}`); return gitPath || null; } } catch (err) { DebugLogger_2.logger.debug('Error finding git:', err); return null; } DebugLogger_2.logger.debug('No git executable found.'); return null; } async function run() { try { console.log(chalk_1.default.cyan("🚀 Koneko starting...")); DebugLogger_2.logger.debug('Koneko CLI starting'); printSystemStatus(); printFullDebugSnapshot(); console.log(chalk_1.default.cyan("🔧 Checking git installation...")); const gitPath = findGit(); if (gitPath) { console.log(chalk_1.default.green(`✅ Found git at: ${gitPath}`)); DebugLogger_2.logger.debug(`Using git at: ${gitPath}`); } else { console.log(chalk_1.default.red("❌ Git is not installed on your system.")); console.log(chalk_1.default.yellow("Please install Git from https://git-scm.com/downloads and try again.")); DebugLogger_2.logger.debug('Git not found, exiting.'); process.exit(1); } console.log(chalk_1.default.cyan("🔧 Checking settings...")); console.log(chalk_1.default.cyan("📂 Ensuring directories...")); console.log(chalk_1.default.blue("🔍 Checking for local Chrome...")); let chromePath = findLocalChrome(); if (chromePath) { console.log(chalk_1.default.green(`✅ Found local Chrome at: ${chromePath}`)); DebugLogger_2.logger.debug(`Using local Chrome at: ${chromePath}`); } else { console.log(chalk_1.default.yellow("⚠️ No local Chrome found in `local-chromium`. Trying to find system Chrome...")); DebugLogger_2.logger.debug('No local Chrome found, searching system Chrome.'); chromePath = findSystemChrome(); if (chromePath) { console.log(chalk_1.default.green(`✅ Found system Chrome at: ${chromePath}`)); DebugLogger_2.logger.debug(`Using system Chrome at: ${chromePath}`); } else { console.log(chalk_1.default.red("❌ No Chrome executable found on your system.")); console.log(chalk_1.default.yellow("Please install Google Chrome or place Chromium in the `local-chromium` folder.")); DebugLogger_2.logger.debug('No Chrome executable found on system, exiting.'); process.exit(1); } } process.env.CHROME_PATH = chromePath; DebugLogger_2.logger.debug(`Set process.env.CHROME_PATH to: ${chromePath}`); const cli = new Launcher_1.MangaCLI(); DebugLogger_2.logger.debug('Created MangaCLI instance, running...'); // await cli.run(); DebugLogger_2.logger.debug('MangaCLI run completed.'); await (0, DebugLogger_1.setupDebugLogger)(); DebugLogger_2.logger.debug('Debug logger setup completed.'); } catch (error) { if ((error === null || error === void 0 ? void 0 : error.name) === 'ExitPromptError') { DebugLogger_2.logger.debug('User exited prompt with Ctrl+C (ExitPromptError).'); console.log(chalk_1.default.yellow('👋 Goodbye! You exited by force (Ctrl+C).')); process.exit(0); } DebugLogger_2.logger.debug('Error in run():', error); console.error(chalk_1.default.red('Fatal error occurred:'), error); process.exit(1); } } run();