UNPKG

@cloud-cli/cli

Version:

CLI for the Cloud CLI project

50 lines (49 loc) 1.6 kB
import { existsSync } from 'node:fs'; import { join } from 'node:path'; import { loadKey } from './authorization.js'; import { Logger } from './logger.js'; import { readJson } from './utils.js'; const defaults = { apiPort: 1234, apiHost: '127.0.0.1', remoteHost: 'http://127.0.0.1', key: '', invalidKeyPenalty: 5000, }; export async function getCloudyConfig(filePath) { filePath || (filePath = findFile()); if (!filePath) { throw new Error('Configuration file not found'); } try { const mod = await import(filePath); const configuration = { ...defaults, ...mod }; configuration.key || (configuration.key = await loadKey()); if (!configuration.key) { throw new Error('[error] A key must be define before we continue.'); } return configuration; } catch (error) { Logger.log(error.message); Logger.log(`[error] Invalid cloud configuration file at ${filePath}`); throw error; } } export function findFile() { const candidates = [join(process.cwd(), 'cloudy.conf.mjs'), '~/cloudy.conf.mjs']; if (process.env.HOME) { candidates.push(join(process.env.HOME, 'cloudy.conf.mjs')); } for (const filePath of candidates) { if (existsSync(filePath)) { return filePath; } } return ''; } export function getConfig(moduleName, defaults = null) { const filePath = join(process.cwd(), 'configuration', `${moduleName}.json`); const config = readJson(filePath); return Object.assign({}, defaults, config); }