UNPKG

vlt

Version:
278 lines (276 loc) 7.53 kB
var global = globalThis; import {Buffer} from "node:buffer"; import {setTimeout as _vlt_setTimeout,clearTimeout as _vlt_clearTimeout,setImmediate as _vlt_setImmediate,clearImmediate as _vlt_clearImmediate,setInterval as _vlt_setInterval,clearInterval as _vlt_clearInterval} from "node:timers"; globalThis.setTimeout = _vlt_setTimeout; globalThis.clearTimeout = _vlt_clearTimeout; globalThis.setImmediate = _vlt_setImmediate; globalThis.clearImmediate = _vlt_clearImmediate; globalThis.setInterval = _vlt_setInterval; globalThis.clearInterval = _vlt_clearInterval; import {createRequire as _vlt_createRequire} from "node:module"; var require = _vlt_createRequire(import.meta.filename); import { views } from "./chunk-RRPN3YUI.js"; import { createHostContextsMap } from "./chunk-6K6533KS.js"; import { del, get, set } from "./chunk-IN6BBUSG.js"; import { GraphModifier, Query, SecurityArchive, VIRTUAL_ROOT_ID, actual } from "./chunk-V52UM37T.js"; import { init } from "./chunk-6F6VOBW3.js"; import "./chunk-QEOLSUUZ.js"; import { commandUsage } from "./chunk-32II74H6.js"; import "./chunk-C3WXW4MY.js"; import "./chunk-JG56ZRZC.js"; import "./chunk-X4GYYGTU.js"; import "./chunk-CGWKWA24.js"; import "./chunk-MBW6A3RQ.js"; import "./chunk-YD3KKYTM.js"; import "./chunk-XGSKO6BL.js"; import "./chunk-RXFAZHP7.js"; import "./chunk-TW6XJ6XF.js"; import "./chunk-4LVU5YJD.js"; import "./chunk-TG2CR2PD.js"; import "./chunk-REP7WICP.js"; import "./chunk-XN35SAI7.js"; import "./chunk-52JFXOJH.js"; import { error } from "./chunk-CHD5BAMM.js"; import "./chunk-C5WO664N.js"; // ../../src/cli-sdk/src/commands/pkg.ts import assert from "node:assert"; import { resolve } from "node:path"; var views2 = { human: (results, _, config) => { if (config.positionals[0] === "init") { return 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: "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() }); } assert( sub, error("pkg command requires a subcommand", { code: "EUSAGE", validOptions: ["get", "pick", "set", "rm", "init"] }) ); const { options, projectRoot } = conf; const queryString = conf.get("scope"); const paths = conf.get("workspace"); const groups = conf.get("workspace-group"); const recursive = conf.get("recursive"); const locations = []; let single = null; if (queryString) { const modifiers = GraphModifier.maybeLoad(options); const mainManifest = options.packageJson.maybeRead(projectRoot); let graph; let securityArchive; if (mainManifest) { graph = actual.load({ ...options, mainManifest, modifiers, monorepo: options.monorepo, loadManifests: false }); securityArchive = Query.hasSecuritySelectors(queryString) ? await SecurityArchive.start({ nodes: [...graph.nodes.values()] }) : void 0; } const hostContexts = await createHostContextsMap(conf); const edges = graph?.edges ?? /* @__PURE__ */ new Set(); const nodes = graph?.nodes ? new Set(graph.nodes.values()) : ( /* c8 ignore next */ /* @__PURE__ */ new Set() ); const importers = graph?.importers ?? /* @__PURE__ */ new Set(); const query = new Query({ edges, nodes, importers, securityArchive, hostContexts }); const { nodes: resultNodes } = await query.search(queryString, { signal: new AbortController().signal }); for (const node of resultNodes) { const location = node.location; assert( location, error(`node ${node.id} has no location`, { found: node }) ); if (node.id !== VIRTUAL_ROOT_ID) { locations.push(resolve(node.projectRoot, location)); } } } else if (paths?.length || groups?.length || recursive) { for (const workspace of options.monorepo ?? []) { locations.push(workspace.fullpath); } } else { single = options.packageJson.find(process.cwd()) ?? projectRoot; } if (single) { return commandSingle(single, sub, args, conf); } assert( locations.length > 0, error("No matching package found using scope", { found: queryString || "workspace selection" }) ); const results = []; for (const location of locations) { results.push(await commandSingle(location, sub, args, conf)); } return results; }; var commandSingle = async (location, sub, args, conf) => { const pkg = conf.options.packageJson; const manifest = pkg.read(location); switch (sub) { case "get": return get2(manifest, args); case "pick": return pick(manifest, args); case "set": return set2(manifest, location, pkg, args); case "rm": case "remove": case "unset": case "delete": return rm(manifest, location, pkg, args); default: { throw error("Unrecognized pkg command", { code: "EUSAGE", found: sub, validOptions: ["get", "set", "rm"] }); } } }; var get2 = (manifest, 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) { const [key] = args; assert(key, noArg()); return get(manifest, key); } assert(args.length === 0, noArg()); return pick(manifest, args); }; var pick = (manifest, args) => { return args.length ? args.reduce( (acc, key) => set(acc, key, get(manifest, key)), {} ) : manifest; }; var set2 = (manifest, location, pkg, args) => { assert( args.length >= 1, error("set requires arguments", { code: "EUSAGE" }) ); const res = args.reduce((acc, p) => { const index = p.indexOf("="); assert( index !== -1, 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 = (manifest, location, pkg, args) => { assert( args.length >= 1, error("rm requires arguments", { code: "EUSAGE" }) ); const res = args.reduce((acc, key) => { del(acc, key); return acc; }, manifest); pkg.write(location, res); return { manifest: res, location }; }; export { command, usage, views2 as views }; //# sourceMappingURL=pkg-IC7PGL4C.js.map