UNPKG

memdisk

Version:

A library and a CLI to create RAM disk on macOS and Linux.

2 lines (1 loc) 3.27 kB
let e;import{mkdirSync as r,rmSync as o,existsSync as t}from"node:fs";import{mkdir as n,rm as i}from"node:fs/promises";import s from"node:path";import{sync as a,async as l}from"@jsdevtools/ez-spawn";import{platform as c}from"node:process";import{tmpdir as d}from"node:os";import y from"gensync";import m from"which";import p from"picocolors";let u=y({sync:m.sync,async:m}),f=y(function*(r){return(void 0===e&&(e=yield*u("sudo",{nothrow:!0})),null===e)?r:e+" "+r}),$=e=>"darwin"===c?`/Volumes/${e}`:`/mnt/${e}`,h=e=>{let r="";return"object"==typeof e&&e&&("name"in e&&"string"==typeof e.name&&(r+=e.name),"code"in e&&"string"==typeof e.code&&(r+=" "+e.code),"message"in e&&"string"==typeof e.message&&(r+=" "+e.message)),r},{yellow:k,red:w,blue:R}=p,b=()=>{},g=(e=!1)=>({warn:e?b:(...e)=>console.warn(k("WARN")+" ",...e),error:e?b:(...e)=>console.error(w("ERROR")+" ",...e),info:e?b:(...e)=>console.info(R("INFO")+" ",...e)}),A=y({sync:a,async:l}),M=y({sync:r,async:n}),v=y({sync:o,async:i}),x="Initializing RAM disk. You may be prompted for credentials",F="Mouting RAM disk. You may be prompted for credentials",S=e=>`Unmouting RAM disk at ${e}. You may be prompted for credentials`,j=e=>"darwin"===e||"linux"===e,z=Symbol("not supported"),E=e=>({darwin:{create:y(function*(r,o,t,n){e.info(x);let i=(yield*A(`hdiutil attach -nomount ram://${t/512}`)).stdout.trim();e.info(F),yield*A(`diskutil eraseVolume ${n?"HFS+":"APFS"} ${r} ${i}`)}),destroy:y(function*(r,o){e.info(S(r));let t=`hdiutil detach ${r}`;o&&(t+=" -force"),yield*A(t)})},linux:{create:y(function*(r,o,t,n){e.info(x),yield*A((yield*f(`mkdir -p ${o}`))),e.info(F),yield*A((yield*f(`mount -t tmpfs -o size=${t} tmpfs ${o}`)))}),destroy:y(function*(r,o){e.info(S(r));let t=o?`umount --force ${r}`:`umount ${r}`;yield*A((yield*f(t)))})},[z]:{create:y(function*(r,o,t,n){e.warn(`The current platform "${c}" does not support RAM disks. A temporary directory (which may or may not exists in the RAM) is created at "${o}".`),yield*M(o,{recursive:!0})}),destroy:y(function*(r,o){let t=`Current platform "${c}" does not support RAM disks, attempted to remove the directory "${r}"`;try{yield*v(r,{recursive:!0,force:!0}),e.warn(`${t} and successed.`)}catch(r){e.warn(`${t} but failed`+h(r))}})}}),Y=e=>{if(e)throw Error(`Unsupported platform "${c}"`)},I=y(function*(e,{quiet:r=!0,throwOnNotSupportedPlatform:o=!1,force:t=!0}={}){let n=g(r);if(j(c)){yield*E(n)[c].destroy(e,t);return}Y(o),yield*E(n)[z].destroy(e,t)}),N=y(function*(e,r,{quiet:o=!0,throwOnNotSupportedPlatform:n=!1,darwinUseHFSPlus:i=!1}={}){let a=g(o),l=$(e);try{if(j(c)){if(t(l))return a.warn(`The path "${l}" already exists, skipping creation`),l;return yield*E(a)[c].create(e,l,r,i),a.info(`RAM disk is avaliable at ${l}`),l}return Y(n),l=s.join(d(),".mocked-memdisk",e),yield*E(a)[z].create(e,l,r,i),l}catch{let e=`Failed to create RAM disk at ${l}`;throw t(l)&&(a.error(`${e}, clean it up`),yield*I(l,{quiet:o,throwOnNotSupportedPlatform:n})),Error(e)}}),O={sync:(...e)=>N.sync.apply(null,e),async:(...e)=>N.async.apply(null,e),errback:(...e)=>N.errback.apply(null,e)},P={sync:(...e)=>I.sync.apply(null,e),async:(...e)=>I.async.apply(null,e),errback:(...e)=>I.errback.apply(null,e)};export{O as create,P as destroy,j as isSupportedPlatform};