UNPKG

@stryke/fs

Version:

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

2 lines (1 loc) 1.93 kB
import{detectPackageManager as i}from"@antfu/install-pkg";import{getWorkspaceRoot as c}from"@stryke/path/get-workspace-root";import{execSync as a}from"node:child_process";import{parseArgs as g}from"node:util";const l=n=>{const{values:t,tokens:r}=g({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};export 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};const f=()=>process.env.NODE_OPTIONS?tokenizeArgs(process.env.NODE_OPTIONS):[];export 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(" ")}export 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}export function getFormattedNodeOptionsWithoutInspect(){const n=getParsedNodeOptionsWithoutInspect();return Object.keys(n).length===0?"":formatNodeOptions(n)}export async function getRegistry(n){const t=c(n),r=await i(t),o=r==="npm"?"--no-workspaces":"";let s="https://registry.npmjs.org/";try{const e=a(`${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}