vlt
Version:
The vlt CLI
274 lines (272 loc) • 7.18 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 {
views
} from "./chunk-GGVR2QNZ.js";
import {
Query
} from "./chunk-4HCFIDH4.js";
import {
SecurityArchive
} from "./chunk-G6XZXH3U.js";
import {
del,
get,
set
} from "./chunk-3SZCEJ7Q.js";
import "./chunk-M34JYYXI.js";
import {
GraphModifier,
actual
} from "./chunk-HFPRNHS6.js";
import {
init
} from "./chunk-SDAHMDDM.js";
import "./chunk-XZF5GYDF.js";
import "./chunk-D36DAG56.js";
import "./chunk-LEKM5RQR.js";
import "./chunk-YWPMIBJS.js";
import "./chunk-5UBJ3ZBM.js";
import "./chunk-SGEQHKFC.js";
import {
commandUsage
} from "./chunk-2Y5QRO5N.js";
import "./chunk-VYJVN3B6.js";
import "./chunk-GADRCS54.js";
import "./chunk-GY4L7O2Y.js";
import "./chunk-3HSZY4YW.js";
import "./chunk-6YRWYWZQ.js";
import "./chunk-TJHWNOOA.js";
import "./chunk-L3TCSQZJ.js";
import "./chunk-3RABDTYN.js";
import "./chunk-264UXZEG.js";
import "./chunk-X4RDKJKD.js";
import "./chunk-BNCOU5ZT.js";
import {
error
} from "./chunk-RV3EHS4P.js";
import "./chunk-AECDW3EJ.js";
// ../../src/cli-sdk/src/commands/pkg.ts
import { resolve } from "node:path";
import assert from "node:assert";
var json = (results) => JSON.stringify(results, null, 2);
var views2 = {
human: (_results, _options, config) => {
const results = _results;
if (config.positionals[0] === "init") {
return views.human(results);
}
return Array.isArray(results) ? typeof results[0] === "string" ? results.join("\n") : json(results) : json(results);
}
};
var usage = () => commandUsage({
command: "pkg",
usage: "[<command>] [<args>]",
description: "Get or manipulate package.json values",
subcommands: {
get: {
usage: "[<key>]",
description: "Get a single value"
},
init: {
usage: "",
description: "Initialize a new package.json file in the current directory"
},
pick: {
usage: "[<key> [<key> ...]]",
description: "Get multiple values or the entire package"
},
set: {
usage: "<key>=<value> [<key>=<value> ...]",
description: "Set one or more key value pairs"
},
delete: {
usage: "<key> [<key> ...]",
description: "Delete one or more keys from the package"
}
},
examples: {
'set "array[1].key=value"': {
description: "Set a value on an object inside an array"
},
'set "array[]=value"': {
description: "Append a value to an array"
}
}
});
var command = async (conf) => {
const [sub, ...args] = conf.positionals;
if (sub === "init") {
return await init({ cwd: process.cwd() });
}
const pkg = conf.options.packageJson;
const scopeQueryString = conf.get("scope");
let manifests;
if (scopeQueryString) {
const modifiers = GraphModifier.maybeLoad(conf.options);
const monorepo = conf.options.monorepo;
const mainManifest = conf.options.packageJson.read(
conf.options.projectRoot
);
const graph = actual.load({
...conf.options,
mainManifest,
modifiers,
monorepo,
loadManifests: false
});
const securityArchive = Query.hasSecuritySelectors(scopeQueryString) ? await SecurityArchive.start({
graph,
specOptions: conf.options
}) : void 0;
const query = new Query({
graph,
specOptions: conf.options,
securityArchive
});
const { nodes } = await query.search(scopeQueryString, {
signal: new AbortController().signal
});
const scopePaths = nodes.filter(
(n) => n.location !== void 0
).map((n) => resolve(conf.options.projectRoot, n.location));
manifests = scopePaths.map((location) => ({
manifest: pkg.read(location),
location
}));
if (manifests.length === 0) {
throw error("No matching package found using scope", {
found: scopeQueryString
});
}
} else {
const location = pkg.find() ?? conf.projectRoot;
manifests = [
{
manifest: pkg.read(location),
location
}
];
}
switch (sub) {
case "get":
return get2(manifests, args);
case "pick":
return pick(manifests, args);
case "set":
return set2(conf, manifests, pkg, args);
case "rm":
case "remove":
case "unset":
case "delete":
return rm(conf, manifests, pkg, args);
default: {
throw error("Unrecognized pkg command", {
code: "EUSAGE",
found: sub,
validOptions: ["get", "set", "rm"]
});
}
}
};
var get2 = (manifests, args) => {
const noArg = () => error(
"get requires not more than 1 argument. use `pick` to get more than 1.",
{ code: "EUSAGE" },
noArg
);
if (args.length !== 1) {
if (args.length > 1) {
throw noArg();
}
return pick(manifests, args);
}
assert(args[0] != null, noArg());
const [key] = args;
if (manifests.length === 1) {
const [firstManifest] = manifests;
if (!firstManifest?.manifest) {
throw error(
"No manifest found",
firstManifest?.location ? { path: firstManifest.location } : {}
);
}
return get(firstManifest.manifest, key);
}
return manifests.map(
(manifest) => get(manifest.manifest, key)
);
};
var pick = (manifests, args) => {
if (manifests.length === 1) {
const [firstManifest] = manifests;
if (!firstManifest?.manifest) {
throw error(
"No manifest found",
firstManifest?.location ? { path: firstManifest.location } : {}
);
}
const { manifest } = firstManifest;
return args.length ? args.reduce(
(acc, key) => set(acc, key, get(manifest, key)),
{}
) : manifest;
}
return manifests.map(
(manifest) => args.length ? args.reduce(
(acc, key) => set(acc, key, get(manifest.manifest, key)),
{}
) : manifest.manifest
);
};
var set2 = (_conf, manifests, pkg, args) => {
if (args.length < 1) {
throw error("set requires arguments", { code: "EUSAGE" });
}
for (const { manifest, location } of manifests) {
const res = args.reduce((acc, p) => {
const index = p.indexOf("=");
if (index === -1) {
throw error("set arguments must contain `=`", {
code: "EUSAGE"
});
}
return set(
acc,
p.substring(0, index),
p.substring(index + 1)
);
}, manifest);
pkg.write(location, res);
}
};
var rm = (_conf, manifests, pkg, args) => {
if (args.length < 1) {
throw error("rm requires arguments", { code: "EUSAGE" });
}
const results = [];
for (const { manifest, location } of manifests) {
const res = args.reduce((acc, key) => {
del(acc, key);
return acc;
}, manifest);
pkg.write(location, res);
results.push({
manifest: res,
location
});
}
if (manifests.length === 1) {
return results[0];
}
return results;
};
export {
command,
usage,
views2 as views
};
//# sourceMappingURL=pkg-Z3DVMTIR.js.map