heyjarvis
Version:
J.A.R.V.I.S. - Advanced Node.js MVC Framework with ORM, built-in validation, soft delete, and query builder
166 lines (140 loc) • 5.11 kB
JavaScript
const fs = require('fs');
const path = require('path');
const os = require('os');
const chalk = require('chalk');
const locales = require('./locales');
// Config dosyası konumu (kullanıcının home dizini)
const CONFIG_DIR = path.join(os.homedir(), '.jarvis');
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
// Varsayılan config
const DEFAULT_CONFIG = {
language: 'en',
version: '1.0.0',
theme: 'default',
templates: {
customPath: null
}
};
class JarvisConfig {
constructor() {
this.config = this.loadConfig();
}
// Config dosyasını yükle
loadConfig() {
try {
if (!fs.existsSync(CONFIG_DIR)) {
fs.mkdirSync(CONFIG_DIR, { recursive: true });
}
if (!fs.existsSync(CONFIG_FILE)) {
this.saveConfig(DEFAULT_CONFIG);
return DEFAULT_CONFIG;
}
const configData = fs.readFileSync(CONFIG_FILE, 'utf8');
return { ...DEFAULT_CONFIG, ...JSON.parse(configData) };
} catch (error) {
console.warn(chalk.yellow('⚠️ Config dosyası okunamadı, varsayılan ayarlar kullanılıyor'));
return DEFAULT_CONFIG;
}
}
// Config dosyasını kaydet
saveConfig(config = this.config) {
try {
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
this.config = config;
return true;
} catch (error) {
console.error(chalk.red('❌ Config dosyası kaydedilemedi:'), error.message);
return false;
}
}
// Dil ayarını değiştir
setLanguage(lang) {
if (!locales[lang]) {
console.error(chalk.red(`❌ Desteklenmeyen dil: ${lang}`));
console.log(chalk.cyan('📝 Desteklenen diller: en, tr'));
return false;
}
this.config.language = lang;
const saved = this.saveConfig();
if (saved) {
console.log(chalk.green(`✅ Dil ${lang} olarak ayarlandı`));
console.log(chalk.blue(`🌍 Language set to: ${lang === 'tr' ? 'Türkçe' : 'English'}`));
}
return saved;
}
// Mevcut dili al
getLanguage() {
return this.config.language || 'en';
}
// Çeviri metnini al
getText(key, params = {}) {
const lang = this.getLanguage();
const locale = locales[lang] || locales['en'];
// Nested key desteği (örn: 'model.fieldTypes.string')
const keys = key.split('.');
let text = locale;
for (const k of keys) {
if (text && typeof text === 'object' && text[k] !== undefined) {
text = text[k];
} else {
// Fallback to English if key not found
const enLocale = locales['en'];
let enText = enLocale;
for (const k2 of keys) {
if (enText && typeof enText === 'object' && enText[k2] !== undefined) {
enText = enText[k2];
} else {
return key; // Return key if not found anywhere
}
}
text = enText;
break;
}
}
// Parameter substitution
if (typeof text === 'string' && Object.keys(params).length > 0) {
Object.keys(params).forEach(param => {
text = text.replace(new RegExp(`{${param}}`, 'g'), params[param]);
});
}
return text || key;
}
// Config bilgilerini göster
showConfig() {
console.log(chalk.cyan('\n🎯 J.A.R.V.I.S. Configuration:'));
console.log(chalk.white(` 📍 Config dosyası: ${CONFIG_FILE}`));
console.log(chalk.white(` 🌍 Dil/Language: ${this.config.language} ${this.config.language === 'tr' ? '(Türkçe)' : '(English)'}`));
console.log(chalk.white(` 🎨 Tema/Theme: ${this.config.theme}`));
console.log(chalk.white(` 📦 Versiyon/Version: ${this.config.version}`));
}
// Tüm config'i sıfırla
resetConfig() {
this.config = { ...DEFAULT_CONFIG };
const saved = this.saveConfig();
if (saved) {
console.log(chalk.green('✅ Config varsayılan ayarlara sıfırlandı'));
}
return saved;
}
// Config value getter/setter
get(key) {
return this.config[key];
}
set(key, value) {
this.config[key] = value;
return this.saveConfig();
}
}
// Singleton instance
const config = new JarvisConfig();
// Helper functions for easy access
const getText = (key, params = {}) => config.getText(key, params);
const getLanguage = () => config.getLanguage();
const setLanguage = (lang) => config.setLanguage(lang);
module.exports = {
config,
getText,
getLanguage,
setLanguage,
JarvisConfig
};