UNPKG

@stryke/fs

Version:

A package containing various file system utilities that expand the functionality of NodeJs's built-in `fs` module.

95 lines (94 loc) 2.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatNodeOptions = formatNodeOptions; exports.getFormattedNodeOptionsWithoutInspect = getFormattedNodeOptionsWithoutInspect; exports.getParsedNodeOptionsWithoutInspect = getParsedNodeOptionsWithoutInspect; exports.getRegistry = getRegistry; exports.tokenizeArgs = void 0; var _installPkg = require("@antfu/install-pkg"); var _getWorkspaceRoot = require("@stryke/path/get-workspace-root"); var _nodeChild_process = require("node:child_process"); var _nodeUtil = require("node:util"); const l = r => { const { values: e, tokens: n } = (0, _nodeUtil.parseArgs)({ args: r, strict: !1, tokens: !0 }); let o = null; for (let i = 0; i < n.length; i++) { const t = n[i]; if (t.kind === "option-terminator") break; if (t.kind === "option") { o = typeof t.value > "u" ? t : null; continue; } if (t.kind !== "positional") { o = null; continue; } o && (o.name in e && typeof e[o.name] == "string" ? e[o.name] += ` ${t.value}` : e[o.name] = t.value); } return e; }; const tokenizeArgs = r => { const e = []; let n = !1, o = !0; for (let i = 0; i < r.length; i++) { let t = r[i]; if (t === "\\" && n) { if (r.length === i + 1) throw new Error("Invalid escape character at the end."); t = r[++i]; } else if (t === " " && !n) { o = !0; continue; } else if (t === '"') { n = !n; continue; } o ? (e.push(t), o = !1) : e[e.length - 1] += t; } if (n) throw new Error("Unterminated string"); return e; }; exports.tokenizeArgs = tokenizeArgs; const f = () => process.env.NODE_OPTIONS ? tokenizeArgs(process.env.NODE_OPTIONS) : []; function formatNodeOptions(r) { return Object.entries(r).map(([e, n]) => n === !0 ? `--${e}` : n ? `--${e}=${n.includes(" ") && !n.startsWith('"') ? JSON.stringify(n) : n}` : null).filter(e => e !== null).join(" "); } function getParsedNodeOptionsWithoutInspect() { const r = f(); if (r.length === 0) return {}; const e = l(r); return delete e.inspect, delete e["inspect-brk"], delete e.inspect_brk, e; } function getFormattedNodeOptionsWithoutInspect() { const r = getParsedNodeOptionsWithoutInspect(); return Object.keys(r).length === 0 ? "" : formatNodeOptions(r); } async function getRegistry(r) { const e = (0, _getWorkspaceRoot.getWorkspaceRoot)(r), n = await (0, _installPkg.detectPackageManager)(e), o = n === "npm" ? "--no-workspaces" : ""; let i = "https://registry.npmjs.org/"; try { const t = (0, _nodeChild_process.execSync)(`${n} config get registry ${o}`, { env: { ...process.env, NODE_OPTIONS: getFormattedNodeOptionsWithoutInspect() } }).toString().trim(); t.startsWith("http") && (i = t.endsWith("/") ? t : `${t}/`); } catch (t) { throw new Error(`Failed to get registry from "${n}".`, { cause: t }); } return i; }