UNPKG

repomix

Version:

A tool to pack repository contents to single file for AI consumption

116 lines 6.37 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import * as fs from 'node:fs/promises'; import path from 'node:path'; import JSON5 from 'json5'; import pc from 'picocolors'; import { RepomixError, rethrowValidationErrorIfZodError } from '../shared/errorHandle.js'; import { logger } from '../shared/logger.js'; import { defaultConfig, defaultFilePathMap, repomixConfigFileSchema, repomixConfigMergedSchema, } from './configSchema.js'; import { getGlobalDirectory } from './globalDirectory.js'; const defaultConfigPaths = ['repomix.config.json5', 'repomix.config.jsonc', 'repomix.config.json']; const getGlobalConfigPaths = () => { const globalDir = getGlobalDirectory(); return defaultConfigPaths.map((configPath) => path.join(globalDir, configPath)); }; const checkFileExists = (filePath) => __awaiter(void 0, void 0, void 0, function* () { try { const stats = yield fs.stat(filePath); return stats.isFile(); } catch (_a) { return false; } }); const findConfigFile = (configPaths, logPrefix) => __awaiter(void 0, void 0, void 0, function* () { for (const configPath of configPaths) { logger.trace(`Checking for ${logPrefix} config at:`, configPath); const fileExists = yield checkFileExists(configPath); if (fileExists) { logger.trace(`Found ${logPrefix} config at:`, configPath); return configPath; } } return null; }); export const loadFileConfig = (rootDir, argConfigPath) => __awaiter(void 0, void 0, void 0, function* () { if (argConfigPath) { // If a specific config path is provided, use it directly const fullPath = path.resolve(rootDir, argConfigPath); logger.trace('Loading local config from:', fullPath); const isLocalFileExists = yield checkFileExists(fullPath); if (isLocalFileExists) { return yield loadAndValidateConfig(fullPath); } throw new RepomixError(`Config file not found at ${argConfigPath}`); } // Try to find a local config file using the priority order const localConfigPaths = defaultConfigPaths.map((configPath) => path.resolve(rootDir, configPath)); const localConfigPath = yield findConfigFile(localConfigPaths, 'local'); if (localConfigPath) { return yield loadAndValidateConfig(localConfigPath); } // Try to find a global config file using the priority order const globalConfigPaths = getGlobalConfigPaths(); const globalConfigPath = yield findConfigFile(globalConfigPaths, 'global'); if (globalConfigPath) { return yield loadAndValidateConfig(globalConfigPath); } logger.log(pc.dim(`No custom config found at ${defaultConfigPaths.join(', ')} or global config at ${globalConfigPaths.join(', ')}.\nYou can add a config file for additional settings. Please check https://github.com/yamadashy/repomix for more information.`)); return {}; }); const loadAndValidateConfig = (filePath) => __awaiter(void 0, void 0, void 0, function* () { try { const fileContent = yield fs.readFile(filePath, 'utf-8'); const config = JSON5.parse(fileContent); return repomixConfigFileSchema.parse(config); } catch (error) { rethrowValidationErrorIfZodError(error, 'Invalid config schema'); if (error instanceof SyntaxError) { throw new RepomixError(`Invalid JSON5 in config file ${filePath}: ${error.message}`); } if (error instanceof Error) { throw new RepomixError(`Error loading config from ${filePath}: ${error.message}`); } throw new RepomixError(`Error loading config from ${filePath}`); } }); export const mergeConfigs = (cwd, fileConfig, cliConfig) => { var _a, _b, _c, _d, _e, _f; logger.trace('Default config:', defaultConfig); const baseConfig = defaultConfig; // If the output file path is not provided in the config file or CLI, use the default file path for the style if (((_a = cliConfig.output) === null || _a === void 0 ? void 0 : _a.filePath) == null && ((_b = fileConfig.output) === null || _b === void 0 ? void 0 : _b.filePath) == null) { const style = ((_c = cliConfig.output) === null || _c === void 0 ? void 0 : _c.style) || ((_d = fileConfig.output) === null || _d === void 0 ? void 0 : _d.style) || baseConfig.output.style; baseConfig.output.filePath = defaultFilePathMap[style]; logger.trace('Default output file path is set to:', baseConfig.output.filePath); } const mergedConfig = { cwd, input: Object.assign(Object.assign(Object.assign({}, baseConfig.input), fileConfig.input), cliConfig.input), output: Object.assign(Object.assign(Object.assign({}, baseConfig.output), fileConfig.output), cliConfig.output), include: [...(baseConfig.include || []), ...(fileConfig.include || []), ...(cliConfig.include || [])], ignore: Object.assign(Object.assign(Object.assign(Object.assign({}, baseConfig.ignore), fileConfig.ignore), cliConfig.ignore), { customPatterns: [ ...(baseConfig.ignore.customPatterns || []), ...(((_e = fileConfig.ignore) === null || _e === void 0 ? void 0 : _e.customPatterns) || []), ...(((_f = cliConfig.ignore) === null || _f === void 0 ? void 0 : _f.customPatterns) || []), ] }), security: Object.assign(Object.assign(Object.assign({}, baseConfig.security), fileConfig.security), cliConfig.security), }; try { return repomixConfigMergedSchema.parse(mergedConfig); } catch (error) { rethrowValidationErrorIfZodError(error, 'Invalid merged config'); throw error; } }; //# sourceMappingURL=configLoad.js.map