@marteye/studio-cli
Version:
CLI for MartEye Studio API
103 lines (99 loc) • 2.89 kB
JavaScript
;
var fs = require('fs');
var path = require('path');
var os = require('os');
var chalk = require('chalk');
const CONFIG_DIR = path.join(os.homedir(), '.studio');
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
function ensureConfigDir() {
if (!fs.existsSync(CONFIG_DIR)) {
fs.mkdirSync(CONFIG_DIR, { recursive: true });
}
}
function loadConfig() {
ensureConfigDir();
if (fs.existsSync(CONFIG_FILE)) {
try {
const content = fs.readFileSync(CONFIG_FILE, 'utf-8');
return JSON.parse(content);
}
catch (error) {
console.warn(chalk.yellow('Warning: Failed to parse config file'));
return {};
}
}
return {};
}
function saveConfig(config) {
ensureConfigDir();
fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
}
function getConfigValue(key) {
const config = loadConfig();
const keys = key.split('.');
let value = config;
for (const k of keys) {
if (value && typeof value === 'object' && k in value) {
value = value[k];
}
else {
return undefined;
}
}
return value;
}
function setConfigValue(key, value) {
const config = loadConfig();
const keys = key.split('.');
let obj = config;
for (let i = 0; i < keys.length - 1; i++) {
const k = keys[i];
if (!(k in obj) || typeof obj[k] !== 'object') {
obj[k] = {};
}
obj = obj[k];
}
obj[keys[keys.length - 1]] = value;
saveConfig(config);
}
function createProfile(name, profile) {
const config = loadConfig();
if (!config.profiles) {
config.profiles = {};
}
config.profiles[name] = profile;
saveConfig(config);
}
function deleteProfile(name) {
const config = loadConfig();
if (config.profiles && name in config.profiles) {
delete config.profiles[name];
// If deleted profile was default, unset default
if (config.defaultProfile === name) {
delete config.defaultProfile;
}
saveConfig(config);
}
}
function setDefaultProfile(name) {
const config = loadConfig();
if (!config.profiles || !(name in config.profiles)) {
throw new Error(`Profile '${name}' does not exist`);
}
config.defaultProfile = name;
saveConfig(config);
}
function listProfiles() {
const config = loadConfig();
return Object.keys(config.profiles || {});
}
exports.createProfile = createProfile;
exports.deleteProfile = deleteProfile;
exports.ensureConfigDir = ensureConfigDir;
exports.getConfigValue = getConfigValue;
exports.listProfiles = listProfiles;
exports.loadConfig = loadConfig;
exports.saveConfig = saveConfig;
exports.setConfigValue = setConfigValue;
exports.setDefaultProfile = setDefaultProfile;
//# sourceMappingURL=config-manager.js.map