UNPKG

admina

Version:

Detect root/admin/sudo and execute commands as it if available

3 lines (2 loc) 1.7 kB
function e(e,t,r,o){Object.defineProperty(e,t,{get:r,set:o,enumerable:!0,configurable:!0})}function t(){return null!==m.sync("sudo",{nothrow:!0})}function r(){return process.getuid?.()===0||!!process.env.CI}function o(){return r()&&t()}function n(e){return o()?`sudo ${e}`:e}function s(e,t=[],r=h){return o()?b(a(e,t),r):l(e,i(t),r)}function c(e,t=[],r=h){return o()?y(a(e,t),r):S(e,i(t),r)}function a(e,t){return`sudo ${i([e,...t]).join(" ")}`}function i(e){return e.map(e=>`'${e}'`)}async function d(){try{return await S("fltmc"),!0}catch{return!1}}async function u(){if("win32"!==process.platform)return!1;try{return await S("fsutil",["dirty","query",process.env.systemdrive??""]),!0}catch(e){if("ENOENT"===e.code)return d();return!1}}function f(){return"win32"===process.platform?R():r()}async function p(e){if(("linux"===process.platform||"darwin"===process.platform)&&o()&&void 0!==process.env.SUDO_USER){let t=w.statSync(e).isDirectory();await c("chown",[...t?["-R"]:[],process.env.SUDO_USER,e],h)}}var $,x={};import m from"which";import{execaCommandSync as b,execaSync as l,execaCommand as y,execa as S}from"execa";import w from"fs";e(x,"hasSudo",()=>t),e(x,"isRoot",()=>r),e(x,"isSudo",()=>o),e(x,"prependSudo",()=>n),e(x,"defaultExecOptions",()=>h),e(x,"execRootSync",()=>s),e(x,"execRoot",()=>c);let h={stdio:"inherit",shell:!0};e($={},"isAdminWindows",()=>R),e($,"isAdminPosix",()=>A),e($,"isAdmin",()=>f);let R=u,A=r;e({},"grantUserWriteAccess",()=>p);export{t as hasSudo,r as isRoot,o as isSudo,n as prependSudo,h as defaultExecOptions,s as execRootSync,c as execRoot,R as isAdminWindows,A as isAdminPosix,f as isAdmin,p as grantUserWriteAccess}; //# sourceMappingURL=index.node.mjs.map