UNPKG

vils

Version:

Recursively outputs file paths and contents in various formats

110 lines (109 loc) 4.14 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const yargs_1 = __importDefault(require("yargs")); const helpers_1 = require("yargs/helpers"); const config_1 = require("./config"); const walker_1 = require("./walker"); const Markdown_1 = require("./formatters/Markdown"); const Html_1 = require("./formatters/Html"); const Tree_1 = require("./formatters/Tree"); const Json_1 = require("./formatters/Json"); const Plain_1 = require("./formatters/Plain"); const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const utils_1 = require("./utils"); async function main() { var _a; console.log('vils started'); const startTime = Date.now(); const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv)) .scriptName('vils') .usage('$0 [source] [options]') .option('log', { alias: 'l', type: 'boolean', description: 'Log to console' }) .option('file', { alias: 'f', type: 'string', description: 'Output file' }) .option('config', { alias: 'c', type: 'string', description: 'Config file path' }) .option('ignore', { alias: 'i', array: true, type: 'string', description: 'Ignore patterns', default: [] // <-- добавляем default: [] }) .option('format', { alias: 'F', type: 'string', description: 'Format (md, html, tree, json, plain)' }) .option('y', { type: 'boolean', description: 'Assume yes for confirmations' }) .option('info', { type: 'boolean', description: 'Show info without executing' }) .help() .alias('h', 'help') .parseSync(); const config = (0, config_1.loadConfig)(argv); const entries = await (0, walker_1.walk)(config); const formatterMap = { md: Markdown_1.MarkdownFormatter, markdown: Markdown_1.MarkdownFormatter, html: Html_1.HtmlFormatter, tree: Tree_1.TreeFormatter, json: Json_1.JsonFormatter, plain: Plain_1.PlainFormatter, slash: Plain_1.PlainFormatter, hash: Plain_1.PlainFormatter }; const fmtKey = ((_a = config.format) !== null && _a !== void 0 ? _a : 'md').toLowerCase(); const FormatterClass = formatterMap[fmtKey] || Markdown_1.MarkdownFormatter; const formatter = new FormatterClass(); const output = formatter.format(entries, config); const textFormats = ['slash', 'tree', 'plain']; const outExt = textFormats.includes(fmtKey) ? 'txt' : fmtKey; const targetFile = config.file || `vils.${outExt}`; if (argv.info) { console.log(`Files: ${entries.length}`); console.log(`Output file: ${targetFile}`); console.log('File tree:'); console.log(new Tree_1.TreeFormatter().format(entries)); process.exit(0); } if (config.confirm && !argv.y) { console.log(`Writing ${entries.length} files to "${targetFile}"`); console.log(new Tree_1.TreeFormatter().format(entries)); const ok = await (0, utils_1.promptConfirm)('Continue? (y/n) '); if (!ok) process.exit(0); } if (config.log) console.log(output); if (config.file || !config.log) { const outPath = path_1.default.resolve(process.cwd(), targetFile); await fs_extra_1.default.writeFile(outPath, output, 'utf-8'); const lineCount = output.split('\n').length; const stats = await fs_extra_1.default.stat(outPath); const sizeKb = (stats.size / 1024).toFixed(2); const fileCount = entries.length; const duration = (Date.now() - startTime) / 1000; console.log(`${fileCount} files, ${lineCount} lines printed to "${targetFile}" (${sizeKb}Kb) in ${duration.toFixed(3)} seconds.`); } } main().catch(err => { console.error(err); process.exit(1); });