UNPKG

mydata-cli

Version:

A CLI tool for interacting with MyData API and managing data. Supports login, data retrieval, and more. Built with Node.js.

84 lines (74 loc) โ€ข 2.25 kB
import { Command } from "commander"; import { getConfig, setConfig, getToken, saveToken } from "../utils/config.js"; import fs from "fs-extra"; import os from "os"; import path from "path"; const config = new Command("config").description("Manage CLI config"); const configPath = path.join(os.homedir(), ".mycli-config.json"); // Set key-value config .command("set") .description("Set a config value") .argument("<key>", "Config key") .argument("<value>", "Config value") .action((key, value) => { if (key === "token") { saveToken(value); } else { setConfig(key, value); } console.log(`โœ… Set ${key} = ${value}`); }); // Get value config .command("get") .description("Get a config value") .argument("<key>", "Config key") .action((key) => { const config = getConfig(); if (key === "token") { const token = getToken(); if (token) console.log(`๐Ÿ” token = ${token}`); else console.log("โš ๏ธ Token not set"); } else { if (config[key]) console.log(`๐Ÿ“ฆ ${key} = ${config[key]}`); else console.log(`โš ๏ธ ${key} not set`); } }); // Delete key config .command("delete") .description("Delete a config key") .argument("<key>", "Config key") .action((key) => { if (!fs.existsSync(configPath)) { console.log("โš ๏ธ No config found."); return; } const config = fs.readJsonSync(configPath); if (config[key]) { delete config[key]; fs.writeJsonSync(configPath, config, { spaces: 2 }); console.log(`๐Ÿ—‘๏ธ Deleted ${key}`); } else { console.log(`โš ๏ธ ${key} not found`); } }); // List all configs config .command("list") .description("List all config values") .action(() => { if (!fs.existsSync(configPath)) { console.log("โš ๏ธ No config found."); return; } const config = fs.readJsonSync(configPath); console.log("๐Ÿงพ Current CLI config:"); Object.entries(config).forEach(([k, v]) => { const safe = k === "token" ? "****" : v; console.log(` ${k}: ${safe}`); }); console.log(`๐Ÿ“‚ Stored at: ${configPath}`); }); export default config;