vlt
Version:
The vlt CLI
174 lines (171 loc) • 4.5 kB
JavaScript
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 {
get
} from "./chunk-3SZCEJ7Q.js";
import {
pairsToRecords,
recordsToPairs
} from "./chunk-FRVD5QAW.js";
import {
asRootError
} from "./chunk-VTABR43C.js";
import {
isObject
} from "./chunk-X4RDKJKD.js";
import {
getSortedKeys,
isRecordField
} from "./chunk-BNCOU5ZT.js";
import {
error
} from "./chunk-RV3EHS4P.js";
// ../../src/config/src/index.ts
import { spawnSync } from "node:child_process";
var list = (conf) => {
return recordsToPairs(conf.options);
};
var del = async (conf) => {
const fields = conf.positionals.slice(1);
if (!fields.length) {
throw error("At least one key is required", {
code: "EUSAGE"
});
}
await conf.deleteConfigKeys(conf.get("config"), 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`);
}
await conf.editConfigFile(conf.get("config"), (file) => {
args.push(file);
const res = spawnSync(command, args, {
stdio: "inherit"
});
if (res.status !== 0) {
throw error(`${command} command failed`, {
...res,
command,
args
});
}
});
};
var set = async (conf) => {
const pairs = conf.positionals.slice(1);
if (!pairs.length) {
await conf.addConfigToFile(conf.get("config"), {});
return;
}
const which = conf.get("config");
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) {
for (const { field, subKey, value } of dotPropPairs) {
if (isRecordField(field)) {
const recordPair = `${field}=${subKey}=${value}`;
try {
const parsed = conf.jack.parseRaw([
`--${recordPair}`
]).values;
await conf.addConfigToFile(which, pairsToRecords(parsed));
} catch (err) {
handleSetError([recordPair], err);
}
}
}
}
};
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 {
list,
del,
get2 as get,
edit,
set
};
//# sourceMappingURL=chunk-WJN6TZTJ.js.map