logggai
Version:
AI-powered CLI for transforming your development work into professional content
96 lines • 2.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfig = getConfig;
const fs = require("fs");
const path = require("path");
const os = require("os");
const chalk = require("chalk");
const CONFIG_FILENAME = '.logggai-config.json';
const configPath = path.join(os.homedir(), CONFIG_FILENAME);
const defaultApiUrl = process.env.LOGGGAI_API_URL || process.env.NEXT_PUBLIC_APP_URL || 'https://logggai.run';
const defaultConfig = {
apiUrl: defaultApiUrl,
userEmail: undefined
};
function mergeConfig(fileConfig = {}) {
return {
...defaultConfig,
...fileConfig,
apiUrl: defaultApiUrl // always prioritize env over file
};
}
function loadConfig() {
try {
if (fs.existsSync(configPath)) {
const raw = fs.readFileSync(configPath, 'utf8');
const parsed = JSON.parse(raw);
if (typeof parsed !== 'object' || !parsed.apiUrl) {
throw new Error('Invalid config file format');
}
return mergeConfig(parsed);
}
}
catch (err) {
console.error(chalk.default.red('Error loading config:'), err);
}
return { ...defaultConfig };
}
function saveConfig(config) {
try {
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), { mode: 0o600 });
}
catch (err) {
console.error(chalk.default.red('Error saving config:'), err);
}
}
function setConfig(key, value) {
const config = loadConfig();
config[key] = value;
saveConfig(config);
}
function getConfig(key) {
return loadConfig()[key];
}
function getAllConfig() {
return loadConfig();
}
function clearConfig() {
try {
if (fs.existsSync(configPath)) {
fs.unlinkSync(configPath);
}
}
catch (err) {
console.error(chalk.default.red('Error clearing config:'), err);
}
}
function isLoggedIn() {
const { sessionToken, userId } = loadConfig();
return !!(sessionToken && userId);
}
// Helpers
const getSessionToken = () => getConfig('sessionToken');
const getRefreshToken = () => getConfig('refreshToken');
const getTokenExpiresAt = () => getConfig('tokenExpiresAt');
const getUserId = () => getConfig('userId');
const getCurrentContext = () => getConfig('currentContext');
const getCurrentOrganizationId = () => getConfig('currentOrganizationId');
const getApiUrl = () => getConfig('apiUrl');
const getUserEmail = () => getConfig('userEmail');
// ✅ Compatibilité CommonJS et ESM
module.exports = {
setConfig,
getConfig,
getAllConfig,
clearConfig,
isLoggedIn,
getSessionToken,
getRefreshToken,
getTokenExpiresAt,
getUserId,
getCurrentContext,
getCurrentOrganizationId,
getApiUrl,
getUserEmail
};
//# sourceMappingURL=config.js.map