lomi.cli
Version:
Command line interface for lomi's payment infrastructure
80 lines (79 loc) • 2.54 kB
JavaScript
import Conf from "conf";
import chalk from "chalk";
// Define the schema for type validation (optional but recommended)
const schema = {
projectName: { type: "string" },
apiKey: { type: "string" },
cliToken: { type: "string" },
environment: { type: "string", enum: ["production", "sandbox"] },
language: { type: "string", enum: ["TypeScript", "JavaScript"] },
supabaseUrl: { type: "string", format: "url" },
};
// Create a Conf instance
// Project name determines the config file location
// e.g., ~/.config/lomi-cli-nodejs/config.json
const config = new Conf({
projectName: "lomi-cli",
// schema, // You can enable schema validation if needed
});
/**
* Saves the configuration data.
* Merges with existing data by default.
* @param {Partial<ConfigData>} data - The configuration data to save.
*/
export function saveConfig(data) {
// Type assertion needed as conf.set doesn't directly use the generic
config.set(data);
}
/**
* Loads the entire configuration object.
* @returns {ConfigData | null} The configuration data, or null if not found/empty.
*/
export function loadConfig() {
// Return the whole store. Returns an empty object if config file doesn't exist.
const store = config.store;
// Return null if the store is effectively empty
return Object.keys(store).length > 0 ? store : null;
}
/**
* Gets a specific configuration value.
* @param {keyof ConfigData} key - The configuration key to retrieve.
* @returns {T | undefined} The value or undefined if not set.
*/
export function getConfigValue(key) {
return config.get(key);
}
/**
* Deletes a specific configuration key.
* @param {keyof ConfigData} key - The key to delete.
*/
export function deleteConfigValue(key) {
config.delete(key);
}
/**
* Clears the entire configuration.
*/
export function clearConfig() {
config.clear();
}
/**
* Gets the path to the configuration file.
* @returns {string} The absolute path to the config file.
*/
export function getConfigFilePath() {
return config.path;
}
/**
* Checks if the user is logged in (CLI token exists).
* If not, prints an error message and exits the process.
*/
export function ensureLoggedIn() {
const token = getConfigValue("cliToken");
if (!token) {
console.error(chalk.red("Error: You are not logged in."));
console.log(`Please run ${chalk.bold("lomi. login")} first.`);
process.exit(1);
}
}
// Example of how to set a default value if needed
// config.set('defaultSetting', 'defaultValue');