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 = n => { const { values: t, tokens: r } = (0, _nodeUtil.parseArgs)({ args: n, strict: !1, tokens: !0 }); let o = null; for (let s = 0; s < r.length; s++) { const e = r[s]; if (e.kind === "option-terminator") break; if (e.kind === "option") { o = typeof e.value > "u" ? e : null; continue; } if (e.kind !== "positional") { o = null; continue; } o && (o.name in t && typeof t[o.name] == "string" ? t[o.name] += ` ${e.value}` : t[o.name] = e.value); } return t; }; const tokenizeArgs = n => { const t = []; let r = !1, o = !0; for (let s = 0; s < n.length; s++) { let e = n[s]; if (e === "\\" && r) { if (n.length === s + 1) throw new Error("Invalid escape character at the end."); e = n[++s]; } else if (e === " " && !r) { o = !0; continue; } else if (e === '"') { r = !r; continue; } o ? (t.push(e), o = !1) : t[t.length - 1] += e; } if (r) throw new Error("Unterminated string"); return t; }; exports.tokenizeArgs = tokenizeArgs; const f = () => process.env.NODE_OPTIONS ? tokenizeArgs(process.env.NODE_OPTIONS) : []; function formatNodeOptions(n) { return Object.entries(n).map(([t, r]) => r === !0 ? `--${t}` : r ? `--${t}=${r.includes(" ") && !r.startsWith('"') ? JSON.stringify(r) : r}` : null).filter(t => t !== null).join(" "); } function getParsedNodeOptionsWithoutInspect() { const n = f(); if (n.length === 0) return {}; const t = l(n); return delete t.inspect, delete t["inspect-brk"], delete t.inspect_brk, t; } function getFormattedNodeOptionsWithoutInspect() { const n = getParsedNodeOptionsWithoutInspect(); return Object.keys(n).length === 0 ? "" : formatNodeOptions(n); } async function getRegistry(n) { const t = (0, _getWorkspaceRoot.getWorkspaceRoot)(n), r = await (0, _installPkg.detectPackageManager)(t), o = r === "npm" ? "--no-workspaces" : ""; let s = "https://registry.npmjs.org/"; try { const e = (0, _nodeChild_process.execSync)(`${r} config get registry ${o}`, { env: { ...process.env, NODE_OPTIONS: getFormattedNodeOptionsWithoutInspect() } }).toString().trim(); e.startsWith("http") && (s = e.endsWith("/") ? e : `${e}/`); } catch (e) { throw new Error(`Failed to get registry from "${r}".`, { cause: e }); } return s; }