UNPKG

anyig

Version:

A powerful and extensible tool for generating ignore files for various development tools and environments

122 lines 4.73 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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigManager = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); class ConfigManager { static CONFIG_FILENAME = '.anyigrc.json'; static CONFIG_PATHS = [ process.cwd(), path.join(process.env['HOME'] || process.env['USERPROFILE'] || '', '.config'), path.join(process.env['HOME'] || process.env['USERPROFILE'] || '') ]; static findConfigFile() { for (const configPath of this.CONFIG_PATHS) { const fullPath = path.join(configPath, this.CONFIG_FILENAME); if (fs.existsSync(fullPath)) { return fullPath; } } return null; } static loadConfig() { const configFile = this.findConfigFile(); if (!configFile) { return this.getDefaultConfig(); } try { const content = fs.readFileSync(configFile, 'utf-8'); const config = JSON.parse(content); return { ...this.getDefaultConfig(), ...config }; } catch (error) { console.warn(`Warning: Failed to load config file ${configFile}:`, error.message); return this.getDefaultConfig(); } } static saveConfig(config, configPath) { const targetPath = configPath || path.join(process.cwd(), this.CONFIG_FILENAME); const dir = path.dirname(targetPath); if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } fs.writeFileSync(targetPath, JSON.stringify(config, null, 2), 'utf-8'); } static getDefaultConfig() { return { defaultTemplates: [], outputDir: process.cwd(), customTemplates: [], autoBackup: false, confirmOverwrite: true }; } static createExampleConfig(outputPath) { const exampleConfig = { defaultTemplates: ['gitignore', 'npmignore'], outputDir: './generated', autoBackup: true, confirmOverwrite: false, customTemplates: [ { name: 'custom-ignore', filename: '.customignore', description: 'Custom ignore file template', category: 'custom', content: '# Add your custom ignore patterns here\n*.tmp\n*.cache\n' } ] }; const targetPath = outputPath || path.join(process.cwd(), this.CONFIG_FILENAME); this.saveConfig(exampleConfig, targetPath); } static validateConfig(config) { const errors = []; if (config.outputDir && !path.isAbsolute(config.outputDir) && !config.outputDir.startsWith('./')) { errors.push('outputDir should be an absolute path or start with "./"'); } if (config.customTemplates) { config.customTemplates.forEach((template, index) => { if (!template.name || !template.filename || !template.content) { errors.push(`customTemplates[${index}] is missing required fields (name, filename, content)`); } }); } return errors; } } exports.ConfigManager = ConfigManager; //# sourceMappingURL=config.js.map