UNPKG

vlt

Version:
400 lines (396 loc) 11.2 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 { asRootError } from "./chunk-3VS4XBYN.js"; import { pairsToRecords, recordsToPairs } from "./chunk-YF5FZDHL.js"; import { getSortedKeys, isRecordField } from "./chunk-W7RMFRDJ.js"; import { isObject } from "./chunk-JBBINXAZ.js"; import { error } from "./chunk-KVH5ECIG.js"; // ../../src/dot-prop/src/index.ts var DISALLOWED_KEYS = /* @__PURE__ */ new Set([ "__proto__", "prototype", "constructor" ]); var DIGITS = new Set("0123456789"); var ARRAY_PUSH = Symbol("ARRAY_PUSH"); var Characters = { Escape: "\\", Dot: ".", Empty: "", LeftBracket: "[", RightBracket: "]" }; var Parts = { Start: "start", Index: "index", IndexEnd: "indexEnd", Property: "property" }; var checkInvalidCharacter = (part, current, msg) => { if (current === part) { if (msg === void 0) { switch (current) { case Parts.Index: msg = "character in an index"; break; case Parts.IndexEnd: msg = "character after an index"; break; /* c8 ignore next 3 */ default: msg = ""; break; } } throw new Error(`Invalid ${msg}`.trim()); } }; var getPathSegments = (path, allowEmptyIndex = false) => { const segments = []; let currentSegment = Characters.Empty; let currentPart = Parts.Start; let isIgnoring = false; for (const character of path.split("")) { switch (character) { case Characters.Escape: { checkInvalidCharacter(Parts.Index, currentPart); checkInvalidCharacter(Parts.IndexEnd, currentPart); if (isIgnoring) currentSegment += character; currentPart = Parts.Property; isIgnoring = !isIgnoring; break; } case Characters.Dot: { checkInvalidCharacter(Parts.Index, currentPart); if (currentPart === Parts.IndexEnd) { currentPart = Parts.Property; break; } if (isIgnoring) { isIgnoring = false; currentSegment += character; break; } if (DISALLOWED_KEYS.has(currentSegment)) return []; segments.push(currentSegment); currentSegment = Characters.Empty; currentPart = Parts.Property; break; } case Characters.LeftBracket: { checkInvalidCharacter(Parts.Index, currentPart); if (currentPart === Parts.IndexEnd) { currentPart = Parts.Index; break; } if (isIgnoring) { isIgnoring = false; currentSegment += character; break; } if (currentPart === Parts.Property) { if (DISALLOWED_KEYS.has(currentSegment)) return []; segments.push(currentSegment); currentSegment = Characters.Empty; } currentPart = Parts.Index; break; } case Characters.RightBracket: { if (currentPart === Parts.Index) { if (allowEmptyIndex) checkInvalidCharacter( Characters.Empty, currentSegment, "empty index" ); segments.push( currentSegment === Characters.Empty ? ARRAY_PUSH : Number.parseInt(currentSegment, 10) ); currentPart = Parts.IndexEnd; currentSegment = Characters.Empty; break; } } default: { if (!DIGITS.has(character)) checkInvalidCharacter(Parts.Index, currentPart); checkInvalidCharacter(Parts.IndexEnd, currentPart); if (currentPart === Parts.Start) currentPart = Parts.Property; if (isIgnoring) { isIgnoring = false; currentSegment += Characters.Escape; } currentSegment += character; } } } if (isIgnoring) currentSegment += Characters.Escape; checkInvalidCharacter( Parts.Index, currentPart, "index was not closed" ); if (currentPart === Parts.Property) { if (DISALLOWED_KEYS.has(currentSegment)) return []; segments.push(currentSegment); } else if (currentPart === Parts.Start) { segments.push(Characters.Empty); } return segments; }; var isObject2 = (value) => value !== null && typeof value === "object"; var isLast = (arr, i) => i === arr.length - 1; var isStringIndex = (object, key) => { if (typeof key !== "symbol" && typeof key !== "number" && Array.isArray(object)) { const index = Number.parseInt(key, 10); return Number.isInteger(index) && object[index] === object[key]; } return false; }; var assertNotStringIndex = (object, key) => { if (isStringIndex(object, key)) { throw new Error("Cannot use string index"); } }; var get = (ogObject, path, defaultValue) => { let object = ogObject; const pathArray = getPathSegments(path, true); if (!pathArray.length) { return defaultValue; } for (const [index, key] of pathArray.entries()) { if (isStringIndex(object, key)) { object = isLast(pathArray, index) ? void 0 : null; } else { object = object[key]; } if ((object === void 0 || object === null) && !isLast(pathArray, index)) { return defaultValue; } } return object === void 0 ? defaultValue : object; }; var set = (object, path, value) => { const root = object; const pathArray = getPathSegments(path); for (const [index, key] of pathArray.entries()) { assertNotStringIndex(object, key); if (isLast(pathArray, index)) { if (key === ARRAY_PUSH) { ; object.push(value); } else { ; object[key] = value; } } else if (!isObject2(object[key])) { const next = pathArray[index + 1]; object[key] = typeof next === "number" || next === ARRAY_PUSH ? [] : {}; } object = object[key]; } return root; }; var del = (object, path) => { const pathArray = getPathSegments(path); for (const [index, key] of pathArray.entries()) { assertNotStringIndex(object, key); if (isLast(pathArray, index)) { if (Array.isArray(object)) { object.splice(key, 1); } else { delete object[key]; } return true; } object = object[key]; if (!isObject2(object)) { return false; } } return false; }; // ../../src/config/src/index.ts import { spawnSync } from "node:child_process"; var list = (conf) => { return recordsToPairs(conf.options); }; var del2 = async (conf) => { const fields = conf.positionals.slice(1); if (!fields.length) { throw error("At least one key is required", { code: "EUSAGE" }); } const configOption = conf.get("config"); const whichConfig = configOption === "all" ? "project" : configOption; await conf.deleteConfigKeys(whichConfig, fields); }; var get2 = async (conf) => { const keys = conf.positionals.slice(1); const k = keys[0]; if (!k || keys.length > 1) { throw error("Exactly one key is required", { code: "EUSAGE" }); } if (k.includes(".")) { const [field, ...rest] = k.split("."); const subKey = rest.join("."); if (!field || !subKey) { throw error("Could not read property", { found: k }); } const record = conf.getRecord(field); return get(record, subKey); } return isRecordField(k) ? conf.getRecord(k) : conf.get(k); }; var edit = async (conf) => { const [command, ...args] = conf.get("editor").split(" "); if (!command) { throw error(`editor is empty`); } const configOption = conf.get("config"); const whichConfig = configOption === "all" ? "project" : configOption; await conf.editConfigFile(whichConfig, (file) => { args.push(file); const res = spawnSync(command, args, { stdio: "inherit" }); if (res.status !== 0) { throw error(`${command} command failed`, { ...res, command, args }); } }); }; var set2 = async (conf) => { const pairs = conf.positionals.slice(1); if (!pairs.length) { const configOption2 = conf.get("config"); const whichConfig = configOption2 === "all" ? "project" : configOption2; await conf.addConfigToFile(whichConfig, {}); return; } const configOption = conf.get("config"); const which = configOption === "all" ? "project" : configOption; const dotPropPairs = []; const simplePairs = []; for (const pair of pairs) { const eq = pair.indexOf("="); if (eq === -1) { throw error("Set arguments must contain `=`", { code: "EUSAGE" }); } const key = pair.substring(0, eq); const value = pair.substring(eq + 1); if (key.includes(".")) { const [field, ...rest] = key.split("."); const subKey = rest.join("."); if (field && subKey) { dotPropPairs.push({ key, field, subKey, value }); } else { throw error("Could not read property", { found: pair }); } } else { simplePairs.push(pair); } } if (simplePairs.length > 0) { try { const parsed = conf.jack.parseRaw( simplePairs.map((kv) => `--${kv}`) ).values; await conf.addConfigToFile(which, pairsToRecords(parsed)); } catch (err) { handleSetError(simplePairs, err); } } if (dotPropPairs.length > 0) { const recordPairs = []; const nestedProps = []; for (const { key, field, subKey, value } of dotPropPairs) { if (isRecordField(field)) { recordPairs.push({ field, subKey, value }); } else { nestedProps.push({ key, value }); } } for (const { field, subKey, value } of recordPairs) { const recordPair = `${field}=${subKey}=${value}`; try { const parsed = conf.jack.parseRaw([`--${recordPair}`]).values; await conf.addConfigToFile(which, pairsToRecords(parsed)); } catch (err) { handleSetError([recordPair], err); } } if (nestedProps.length > 0) { const nested = {}; for (const { key, value } of nestedProps) { set(nested, key, value); } await conf.addConfigToFile(which, nested); } } }; var handleSetError = (simplePairs, err) => { const { name, found, validOptions } = asRootError(err).cause; if (isObject(found) && typeof found.name === "string" && typeof found.value === "string") { const { name: name2, value } = found; throw error( `Boolean flag must be "${name2}" or "no-${name2}", not a value`, { code: "ECONFIG", name: name2, found: `${name2}=${value}` } ); } if (Array.isArray(validOptions)) { throw error(`Invalid value provided for ${name}`, { code: "ECONFIG", found, validOptions }); } throw error("Invalid config keys", { code: "ECONFIG", found: simplePairs.map((kv) => kv.split("=")[0]), validOptions: getSortedKeys() }); }; export { get, set, del, list, del2, get2, edit, set2 }; //# sourceMappingURL=chunk-PVOBL7T7.js.map