UNPKG

@mutasim77/loc-counter

Version:

A powerful lines of code counter with detailed statistics

111 lines (110 loc) 4.99 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.run = run; const commander_1 = require("commander"); const path = __importStar(require("path")); const fs = __importStar(require("fs")); const chalk_1 = __importDefault(require("chalk")); const lineCounter_1 = require("./utils/lineCounter"); const reporter_1 = require("./utils/reporter"); const fileUtils_1 = require("./utils/fileUtils"); async function run() { const program = new commander_1.Command(); program .name('loc-counter') .description('Count lines of code in your project with detailed statistics') .version('1.0.0') .option('-d, --dir <path>', 'directory to analyze', '.') .option('-e, --exclude <patterns...>', 'patterns to exclude', []) .option('-i, --include <patterns...>', 'patterns to include', []) .option('--no-ignoregit', 'do not respect .gitignore rules') .option('-v, --verbose', 'show verbose output'); program.parse(process.argv); const options = program.opts(); try { // Display banner console.log('\n'); console.log(chalk_1.default.bold.cyan('╔════════════════════════════════════╗')); console.log(chalk_1.default.bold.cyan('║ LOC-COUNTER ║')); console.log(chalk_1.default.bold.cyan('║ Lines of Code Analysis Tool ║')); console.log(chalk_1.default.bold.cyan('╚════════════════════════════════════╝')); console.log('\n'); // Check if directory exists const targetDir = path.resolve(options.dir); if (!fs.existsSync(targetDir)) { throw new Error(`Directory does not exist: ${targetDir}`); } (0, reporter_1.printProgress)(`Analyzing directory: ${targetDir}`); // Get all file paths const filePaths = await (0, fileUtils_1.getFilePaths)(targetDir, { exclude: options.exclude, include: options.include, useGitignore: options.ignoregit }); if (filePaths.length === 0) { (0, reporter_1.printError)('No files found to analyze.'); process.exit(1); } (0, reporter_1.printProgress)(`Found ${filePaths.length} files to analyze.`); // Count lines for each file const filePromises = filePaths.map(async (filePath) => { const stats = await (0, lineCounter_1.countLines)(filePath); if (options.verbose) { (0, reporter_1.printProgress)(`Analyzed: ${filePath}`); } return stats; }); const fileResults = await Promise.all(filePromises); // Use a type predicate to properly narrow the type const fileStats = fileResults.filter((stats) => stats !== null); if (fileStats.length === 0) { (0, reporter_1.printError)('No supported files found for analysis.'); process.exit(1); } // Aggregate stats const projectStats = (0, lineCounter_1.aggregateStats)(fileStats); // Print results (0, reporter_1.printSuccess)(`Analysis completed for ${fileStats.length} files.`); (0, reporter_1.printProjectSummary)(projectStats); } catch (error) { (0, reporter_1.printError)(error.message); process.exit(1); } }