@freik/node-utils
Version:
My personal set of utilities for NodeJS
3 lines (2 loc) • 1.79 kB
JavaScript
import*as u from"os";import*as c from"child_process";import*as i from"path";import{promises as f}from"fs";import{promisify as a}from"util";const p=a(c.exec);function b(t,n){const r=n&&n[0]!=="."?"."+n:n||"";return i.join(u.tmpdir(),`${t}-tmp-${process.pid}${r}`)}function m(t){const n=i.extname(t);return n&&n.substring(1)}function W(t,n){const r=m(t);n&&n.length>1&&n[0]==="."&&(n=n.substring(1));let o=t.substring(0,t.length-r.length);return o.endsWith(".")||(o+="."),o+n}function g(t){return t.replaceAll("\\","/")}function j(t){return t.endsWith("\\")||t.endsWith("/")?g(t):g(t+i.sep)}function $(t){return g(i.resolve(g(t)))}function v(...t){return g(i.join(...t))}function A(t){return g(i.dirname(t))}async function y(){switch(u.platform()){case"win32":{const{stdout:t,stderr:n}=await p("wmic logicaldisk get name");return n.length>0?[]:t.split(`\r\r
`).filter(r=>/[A-Za-z]:/.test(r)).map(r=>r.trim())}case"darwin":return(await f.readdir("/Volumes")).map(n=>i.join("/Volumes",n));default:return["linux NYI"]}}const d=[["?","\u0241"],["*","\u2217"],[":","\u0589"],[";","\u037E"],["/","\u2571"],["\\","\u2572"],["|","\u2503"],["<","\u02C2"],[">","\u02C3"]];function V(t,n){let r=t;for(const[e,s]of d)r.indexOf(e)>=0&&(r=r.replaceAll(e,s));r.endsWith(".")&&!n&&(r=r.substring(0,r.length-1)+"\u2024");const o=r.split('"');r="";let l=!0;for(let e=0;e<o.length;e++){const s=o[e];s===""?e===0?r+="\u201C":e!==o.length-1&&(r+=l?"\u201C":"\u201D",l=!l):s.endsWith(" ")?(l=!1,r+=s+"\u201C"):/[0-9]$/.test(s)?r+=s+"\u201D":e===o.length-1?r+=s:(r+=s+(l?"\u201C":"\u201D"),l=!l)}return r.trim()}const k=i.basename,w=i.extname;export{k as basename,W as changeExt,A as dirname,w as extname,V as fileClean,m as getExtNoDot,y as getRoots,b as getTemp,v as join,$ as resolve,j as trailingSlash,g as xplat};