UNPKG

code-graph-generator

Version:

Generate Json Object of code that can be used to generate code-graphs for JavaScript/TypeScript/Range projects

115 lines 4.99 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 commander_1 = require("commander"); const chalk_1 = __importDefault(require("chalk")); const ora_1 = __importDefault(require("ora")); const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const index_1 = require("./index"); const file_utils_1 = require("./utils/file-utils"); // Set up the program commander_1.program .name('code-graph-generator') .description('Generate a code graph for JavaScript/TypeScript projects') .version('1.0.3'); // Define command-line options commander_1.program .argument('<projectPath>', 'Path to the project directory') .option('-o, --output <path>', 'Output path for the generated graph') .option('-i, --include <patterns...>', 'File patterns to include (default: **/*.js, **/*.jsx, **/*.ts, **/*.tsx)') .option('-e, --exclude <patterns...>', 'File patterns to exclude') .option('-d, --debug', 'Enable debug logging') .option('-c, --concurrency <number>', 'Number of concurrent file operations', '10') .option('--include-declarations', 'Include .d.ts files in analysis') .option('--include-node-modules', 'Include node_modules in analysis (not recommended)') .option('--stream', 'Stream output directly without relationship analysis') .option('--pretty', 'Format the output JSON with indentation') .action(async (projectPath, options) => { try { // Validate project path const rootDir = path_1.default.resolve(process.cwd(), projectPath); if (!fs_1.default.existsSync(rootDir)) { console.error(chalk_1.default.red(`Error: Project directory ${rootDir} does not exist`)); process.exit(1); } // Set up options const projectName = path_1.default.basename(rootDir); const outputPath = options.output ? path_1.default.resolve(process.cwd(), options.output) : path_1.default.join(process.cwd(), 'code-graph.json'); // Set up spinner const spinner = (0, ora_1.default)('Analyzing codebase...').start(); // Configure code graph options const graphOptions = { projectName, rootDir, include: options.include, exclude: options.exclude, concurrency: parseInt(options.concurrency, 10), includeDeclarations: options.includeDeclarations || false, includeNodeModules: options.includeNodeModules || false, debug: options.debug || false, outputPath, streamOutput: options.stream || false, }; // Set up custom logger for CLI file_utils_1.logger.setDebug(options.debug); if (options.debug) { // In debug mode, show all logs directly file_utils_1.logger.info = (message, ...args) => { spinner.stop(); console.info(chalk_1.default.blue(`[CodeGraph] ${message}`), ...args); spinner.start(); }; file_utils_1.logger.warn = (message, ...args) => { spinner.stop(); console.warn(chalk_1.default.yellow(`[CodeGraph] ${message}`), ...args); spinner.start(); }; file_utils_1.logger.error = (message, ...args) => { spinner.stop(); console.error(chalk_1.default.red(`[CodeGraph] ${message}`), ...args); spinner.start(); }; } else { // In normal mode, suppress most logs file_utils_1.logger.info = () => { }; file_utils_1.logger.warn = () => { }; file_utils_1.logger.error = (message, ...args) => { spinner.stop(); console.error(chalk_1.default.red(`[CodeGraph] ${message}`), ...args); spinner.start(); }; } // Generate the code graph const graph = await (0, index_1.createCodeGraph)(graphOptions); // If output path wasn't specified in options, write to stdout or to file if (!options.output) { spinner.succeed('Code graph generated'); if (options.pretty) { console.log(JSON.stringify(graph, null, 2)); } else { console.log(JSON.stringify(graph)); } } else { spinner.succeed(`Code graph written to ${outputPath}`); } } catch (error) { // spinner?.fail('Error generating code graph'); console.error(chalk_1.default.red(`Error: ${error.message}`)); if (options.debug) { console.error(error); } process.exit(1); } }); commander_1.program.parse(); //# sourceMappingURL=cli.js.map