cursorai-errorprompter
Version:
AI-powered runtime error fixing for developers using Cursor
100 lines (99 loc) • 3.3 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CONFIG_FILENAME = void 0;
exports.loadConfig = loadConfig;
exports.findConfigFile = findConfigFile;
exports.validateGptConfig = validateGptConfig;
exports.getDefaultConfig = getDefaultConfig;
/**
* Configuration loader for DevMate
* Exports functions and types for loading and validating configuration
*/
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
// Constants
exports.CONFIG_FILENAME = 'devmate.config.json';
/**
* Loads and validates the DevMate configuration
* @returns Promise<Config> The loaded configuration
*/
async function loadConfig() {
try {
const configPath = await findConfigFile(process.cwd());
if (!configPath) {
console.warn('⚠️ No devmate.config.json found. Using default configuration.');
return getDefaultConfig();
}
const configContent = await fs_1.default.promises.readFile(configPath, 'utf-8');
const config = JSON.parse(configContent);
if (config.gpt) {
validateGptConfig(config.gpt);
}
return config;
}
catch (error) {
console.error('❌ Error loading configuration:', error);
return getDefaultConfig();
}
}
/**
* Finds the configuration file in the current or parent directories
* @param startDir The directory to start searching from
* @returns Promise<string | null> Path to the config file or null if not found
*/
async function findConfigFile(startDir) {
const configPath = path_1.default.join(startDir, exports.CONFIG_FILENAME);
try {
await fs_1.default.promises.access(configPath);
return configPath;
}
catch {
const parentDir = path_1.default.dirname(startDir);
if (parentDir === startDir) {
return null;
}
return findConfigFile(parentDir);
}
}
/**
* Validates GPT configuration settings
* @param config The GPT configuration to validate
* @throws Error if validation fails
*/
function validateGptConfig(config) {
if (config.apiKey && typeof config.apiKey !== 'string') {
throw new Error('GPT API key must be a string');
}
if (config.model && typeof config.model !== 'string') {
throw new Error('GPT model must be a string');
}
if (config.temperature !== undefined && (config.temperature < 0 || config.temperature > 1)) {
throw new Error('GPT temperature must be between 0 and 1');
}
if (config.maxTokens !== undefined && config.maxTokens < 1) {
throw new Error('GPT maxTokens must be greater than 0');
}
}
/**
* Returns the default configuration
* @returns Config The default configuration object
*/
function getDefaultConfig() {
return {
command: 'npm run dev',
targetLanguage: 'typescript',
browserToolsEnabled: true,
errorPatterns: [
{
type: 'TypeScript Error',
regex: /TS\d+/i,
filePathGroup: 1,
lineNumberGroup: 2,
messageGroup: 3
}
]
};
}