i18n-management-cli
Version:
CLI tool for i18n management platform - import, export, and sync translations
72 lines (59 loc) • 1.47 kB
JavaScript
const fs = require('fs-extra');
const path = require('path');
const CONFIG_FILE = 'i18n.config.json';
class Config {
constructor() {
this.configPath = path.resolve(process.cwd(), CONFIG_FILE);
this.config = this.load();
}
load() {
try {
if (fs.existsSync(this.configPath)) {
return fs.readJsonSync(this.configPath);
}
} catch (error) {
console.warn('Warning: Could not load config file:', error.message);
}
return this.getDefaultConfig();
}
save() {
try {
fs.writeJsonSync(this.configPath, this.config, { spaces: 2 });
return true;
} catch (error) {
console.error('Error saving config:', error.message);
return false;
}
}
get(key) {
return key ? this.config[key] : this.config;
}
set(key, value) {
this.config[key] = value;
}
getDefaultConfig() {
return {
apiUrl: 'http://localhost:3000/api',
projectId: '',
token: '',
defaultLanguage: 'zh',
paths: {
zh: './messages/zh',
en: './messages/en',
tw: './messages/tw'
}
};
}
validate() {
const required = ['apiUrl', 'projectId'];
const missing = required.filter(key => !this.config[key]);
if (missing.length > 0) {
throw new Error(`Missing required configuration: ${missing.join(', ')}`);
}
return true;
}
exists() {
return fs.existsSync(this.configPath);
}
}
module.exports = Config;