UNPKG

vlt

Version:
423 lines (421 loc) 11 kB
var global = globalThis; import {Buffer} from "node:buffer"; import {setTimeout,clearTimeout,setImmediate,clearImmediate,setInterval,clearInterval} from "node:timers"; import {createRequire as _vlt_createRequire} from "node:module"; var require = _vlt_createRequire(import.meta.filename); import { del2 as del, edit, get, get2, list, set2 as set } from "./chunk-PVOBL7T7.js"; import "./chunk-3VS4XBYN.js"; import "./chunk-YF5FZDHL.js"; import "./chunk-GGPZGJ5H.js"; import "./chunk-OTLTOVZN.js"; import { commandUsage } from "./chunk-L3E552CT.js"; import "./chunk-GTAUGWLW.js"; import "./chunk-U5J4TCIV.js"; import "./chunk-KPA4XNCN.js"; import "./chunk-VYJVN3B6.js"; import "./chunk-B4MAUXR2.js"; import "./chunk-W7RMFRDJ.js"; import "./chunk-O57KIW5U.js"; import "./chunk-JBBINXAZ.js"; import "./chunk-OAYCZMD4.js"; import { find, load } from "./chunk-QOAKZNUG.js"; import "./chunk-BA67AKYJ.js"; import { error } from "./chunk-KVH5ECIG.js"; import "./chunk-AECDW3EJ.js"; // ../../src/cli-sdk/src/commands/config.ts import assert from "node:assert"; var views = { human: (results) => { if (Array.isArray(results) && typeof results[0] === "string") { return results.join("\n"); } return JSON.stringify(results, null, 2); } }; var usage = () => commandUsage({ command: "config", usage: "[<command>] [<args>]", description: "Get or manipulate vlt configuration values", subcommands: { get: { usage: "[<key>] [--config=<all | user | project>]", description: "Get a single config value. Use --config to specify which config to read from." }, pick: { usage: "[<key> [<key> ...]] [--config=<all | user | project>]", description: "Get multiple config values or all configuration. Use --config to specify which config to read from." }, list: { usage: "[--config=<all | user | project>]", description: "Print configuration settings. --config=all shows merged config (default), --config=user shows only user config, --config=project shows only project config." }, set: { usage: "<key>=<value> [<key>=<value> ...] [--config=<all | user | project>]", description: `Set config values. By default (or with --config=all), these are written to the project config file, \`vlt.json\` in the root of the project. To set things for all projects, run with \`--config=user\`.` }, delete: { usage: "<key> [<key> ...] [--config=<all | user | project>]", description: `Delete the named config fields. If no values remain in the config file, delete the file as well. By default (or with --config=all), operates on the \`vlt.json\` file in the root of the current project. To delete a config field from the user config file, specify \`--config=user\`.` }, edit: { usage: "[--config=<all | user | project>]", description: "Edit the configuration file. By default (or with --config=all), edits the project config file." }, location: { usage: "[--config=<user | project>]", description: "Show the file path of the configuration file. Defaults to project config." } } }); var command = async (conf) => { const [sub] = conf.positionals; assert( sub, error("config command requires a subcommand", { code: "EUSAGE", validOptions: [ "get", "pick", "set", "delete", "list", "edit", "location" ] }) ); switch (sub) { case "set": return configSet(conf); case "get": return configGet(conf); case "pick": return configPick(conf); case "ls": case "list": return configList(conf); case "edit": return configEdit(conf); case "location": return configLocation(conf); case "del": case "delete": case "rm": case "remove": case "unset": return configDelete(conf); default: { throw error("Unrecognized config command", { code: "EUSAGE", found: sub, validOptions: [ "get", "pick", "set", "delete", "list", "edit", "location" ] }); } } }; var configGet = async (conf) => { const keys = conf.positionals.slice(1); const configOption = conf.get("config"); if (keys.length === 0) { return configPick(conf); } if (keys.length === 1) { const key = keys[0]; if (!key) { throw error("Key is required", { code: "EUSAGE" }); } switch (configOption) { case "all": { const result = await get2(conf); return result; } case "user": { return getUserConfigValue(key); } case "project": { return getProjectConfigValue(key); } default: { const result = await get2(conf); return result; } } } return configPick(conf); }; var configPick = async (conf) => { const keys = conf.positionals.slice(1); const configOption = conf.get("config"); if (keys.length === 0) { switch (configOption) { case "all": return getSerializableConfig(conf); case "user": { const userConfig = getUserConfigObject(); return userConfig ?? {}; } case "project": { const projectConfig = getProjectConfigObject(); return projectConfig ?? {}; } } } const result = {}; for (const key of keys) { if (!key) continue; switch (configOption) { case "all": result[key] = await get2( Object.assign( Object.create(Object.getPrototypeOf(conf)), conf, { positionals: ["get", key] } ) ); break; case "user": result[key] = getUserConfigValue(key); break; case "project": result[key] = getProjectConfigValue(key); break; } } return result; }; var configList = (conf) => { const configOption = conf.get("config"); switch (configOption) { case "all": return list(conf); case "user": return getUserConfigList(); case "project": return getProjectConfigList(); default: return list(conf); } }; var configToStringArray = (config) => { const result = []; for (const [key, value] of Object.entries(config)) { if (value === void 0 || value === null) { continue; } if (Array.isArray(value)) { for (const item of value) { if (typeof item === "string") { result.push(`${key}=${item}`); } } } else if (typeof value === "object") { for (const [subKey, subValue] of Object.entries(value)) { if (subValue !== void 0 && subValue !== null) { result.push(`${key}.${subKey}=${String(subValue)}`); } } } else { let stringValue; if (typeof value === "string") { stringValue = value; } else if (typeof value === "number" || typeof value === "boolean") { stringValue = String(value); } else { stringValue = "[object]"; } result.push(`${key}=${stringValue}`); } } return result.sort(); }; var getUserConfigList = () => { try { const userConfig = load( "config", (x, file) => { if (x !== null && typeof x === "object" && !Array.isArray(x)) { return; } throw new Error(`Invalid config in ${file}`); }, "user" ); if (!userConfig) return ( /* c8 ignore next */ [] ); return configToStringArray(userConfig); } catch (_err) { return []; } }; var getProjectConfigList = () => { try { const projectConfig = load( "config", (x, file) => { if (x !== null && typeof x === "object" && !Array.isArray(x)) { return; } throw new Error(`Invalid config in ${file}`); }, "project" ); if (!projectConfig) return ( /* c8 ignore next */ [] ); return configToStringArray(projectConfig); } catch (_err) { return []; } }; var getUserConfigObject = () => { try { const userConfig = load( "config", (x, file) => { if (x !== null && typeof x === "object" && !Array.isArray(x)) { return; } throw new Error(`Invalid config in ${file}`); }, "user" ); if (!userConfig || typeof userConfig !== "object") return; return userConfig; } catch (_err) { return; } }; var getUserConfigValue = (key) => { const userConfig = getUserConfigObject(); if (!userConfig) return; return get(userConfig, key); }; var getProjectConfigObject = () => { try { const projectConfig = load( "config", (x, file) => { if (x !== null && typeof x === "object" && !Array.isArray(x)) { return; } throw new Error(`Invalid config in ${file}`); }, "project" ); if (!projectConfig || typeof projectConfig !== "object") return; return projectConfig; } catch (_err) { return; } }; var getProjectConfigValue = (key) => { const projectConfig = getProjectConfigObject(); if (!projectConfig) return; return get( projectConfig, key ); }; var getWriteConfigOption = (conf) => { const configOption = conf.get("config"); if (configOption === "all") { return "project"; } return configOption; }; var configSet = async (conf) => { const effectiveConfig = getWriteConfigOption(conf); const originalGet = conf.get; conf.get = ((key) => { if (key === "config") { return effectiveConfig; } return originalGet.call(conf, key); }); try { return await set(conf); } finally { conf.get = originalGet; } }; var configDelete = async (conf) => { const effectiveConfig = getWriteConfigOption(conf); const originalGet = conf.get; conf.get = ((key) => { if (key === "config") { return effectiveConfig; } return originalGet.call(conf, key); }); try { return await del(conf); } finally { conf.get = originalGet; } }; var configEdit = async (conf) => { const effectiveConfig = getWriteConfigOption(conf); const originalGet = conf.get; conf.get = ((key) => { if (key === "config") { return effectiveConfig; } return originalGet.call(conf, key); }); try { return await edit(conf); } finally { conf.get = originalGet; } }; var getSerializableConfig = (conf) => { return list(conf); }; var configLocation = (conf) => { const configOption = conf.get("config"); const effectiveConfig = configOption === "all" ? "project" : configOption; const configPath = find(effectiveConfig); return configPath; }; export { command, usage, views }; //# sourceMappingURL=config-46PLIERV.js.map