dotenv
Version:
Loads environment variables from .env file
9 lines (8 loc) • 10.7 kB
JavaScript
var j=(e,r)=>()=>{try{return r||e((r={exports:{}}).exports,r),r.exports}catch(t){throw r=0,t}};var G=j((Ee,F)=>{var k=require("child_process"),K=require("fs"),R=require("path");function X(e){let r=['"'],t=0;for(let o of e){if(o==="\\"){t++;continue}o==='"'?r.push("\\".repeat(t*2+1),'"'):r.push("\\".repeat(t),o),t=0}return r.push("\\".repeat(t*2),'"'),r.join("")}function P(e,r=1){for(let t=0;t<r;t++){let o=[];for(let n of e){let s=n.charCodeAt(0),i=s>=48&&s<=57||s>=65&&s<=90||s>=97&&s<=122,l="\\/:._-".includes(n);!i&&!l&&s<128&&o.push("^"),o.push(n)}e=o.join("")}return e}function D(e,r){let t=Object.keys(e).reverse().find(o=>o.toUpperCase()===r);return t===void 0?void 0:e[t]}function Y(e,r,t){let o=(D(r,"PATHEXT")||".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean),s=o.some(l=>e.toLowerCase().endsWith(l.toLowerCase()))?["",...o]:[...o,""],i=/[\\/]/.test(e)?[t]:[t,...(D(r,"PATH")||"").split(";")];for(let l of i)for(let d of s){let p=R.resolve(t,l.replace(/^"|"$/g,""),e+d);try{if(K.statSync(p).isFile())return p}catch{}}}function z(e,r,t){if(process.platform!=="win32")return k.spawn(e,r,t);let o=t.env||process.env,n=Y(e,o,t.cwd||process.cwd());if(n&&/\.(?:exe|com)$/i.test(n))return k.spawn(n,r,t);let s=/\.(?:bat|cmd)$/i.test(n||e),i=[P(R.normalize(n||e))];for(let d of r)i.push(P(X(d),s?2:1));let l=i.join(" ");return k.spawn(D(o,"COMSPEC")||"cmd.exe",["/d","/v:off","/s","/c",`"${l}"`],{...t,windowsVerbatimArguments:!0})}F.exports=z});var I=j((be,V)=>{function U(e){return typeof e=="string"?!["false","0","no","off",""].includes(e.toLowerCase()):!!e}function Z(e=process.env){let r={};for(let t of["ENCODING","PATH","QUIET","DEBUG","OVERRIDE","FAST"]){let o=e[`DOTENV_${t}`]!=null?e[`DOTENV_${t}`]:e[`DOTENV_CONFIG_${t}`];o!=null&&(r[t.toLowerCase()]=t==="ENCODING"||t==="PATH"?o:U(o))}return r}V.exports={parseBoolean:U,optionsFromEnv:Z}});var L=j((Oe,y)=>{var ee=require("fs"),N=require("path"),te=require("os"),{URL:ne,fileURLToPath:re}=require("url"),{parseBoolean:$,optionsFromEnv:H}=I(),oe=/(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg,b=new Uint8Array(256);for(let e=48;e<=57;e++)b[e]=1;for(let e=65;e<=90;e++)b[e]=1;for(let e=97;e<=122;e++)b[e]=1;b[45]=1;b[46]=1;b[95]=1;function se(e){let r={},t=e.toString();t=t.replace(/\r\n?/mg,`
`);let o;for(;(o=oe.exec(t))!=null;){let n=o[1],s=o[2]||"";s=s.trim();let i=s[0];s=s.replace(/^(['"`])([\s\S]*)\1$/mg,"$2"),i==='"'&&(s=s.replace(/\\n/g,`
`),s=s.replace(/\\r/g,"\r")),r[n]=s}return r}function w(e){return e<=32?e===32||e>=9&&e<=13:e>=160&&(e===160||e===5760||e>=8192&&e<=8202||e===8232||e===8233||e===8239||e===8287||e===12288||e===65279)}function O(e){return e===10||e===8232||e===8233}function ie(e){let r={},t=typeof e=="string"?e:e.toString();t.indexOf("\r")!==-1&&(t=t.replace(/\r\n?/g,`
`));let o=t.length,n=0;for(;n<o;){let s=t.charCodeAt(n);for(;n<o&&w(s);)n++,s=t.charCodeAt(n);if(n>=o)break;if(s===35){for(;n<o&&!O(t.charCodeAt(n));)n++;continue}let i=-1;if(s===101&&n+6<o&&t.charCodeAt(n+1)===120&&t.charCodeAt(n+2)===112&&t.charCodeAt(n+3)===111&&t.charCodeAt(n+4)===114&&t.charCodeAt(n+5)===116){let C=t.charCodeAt(n+6);if(w(C)){let u=n+7;for(;u<o&&w(t.charCodeAt(u));)u++;b[t.charCodeAt(u)]&&(i=n+6,n=u)}else s=t.charCodeAt(n)}let l=n,d=0;for(;n<o&&(d=t.charCodeAt(n),b[d]);)n++;if(n===l){for(;n<o&&!O(t.charCodeAt(n));)n++;continue}let p=t.slice(l,n),f=n;if(n>=o&&(d=0),w(d))do n++,d=n<o?t.charCodeAt(n):0;while(w(d));if(d===61)n++;else if(d===58&&n===f&&n+1<o&&w(t.charCodeAt(n+1)))n+=2;else{for(n=i===-1?f:i;n<o&&!O(t.charCodeAt(n));)n++;continue}let c=n,a=n;for(;a<o&&w(t.charCodeAt(a));)a++;let g=t.charCodeAt(a),h,x=!1;if(g===39||g===34||g===96){let C=t[a],u=t.indexOf(C,a+1),m=-1,v=-1;for(;u!==-1;){let q=t.charCodeAt(u-1)===92,E=u+1;for(;E<o&&!O(t.charCodeAt(E))&&w(t.charCodeAt(E));)E++;if((E===o||O(t.charCodeAt(E))||t.charCodeAt(E)===35)&&(m=u,v=E),!q)break;u=t.indexOf(C,u+1)}if(m!==-1){if(h=t.slice(a+1,m),n=v,t.charCodeAt(n)===35)for(;n<o&&!O(t.charCodeAt(n));)n++;x=!0}}if(!x){let C=t.indexOf(`
`,c);C===-1&&(C=o);let u;if(o<4096)u=t.indexOf("#",c),(u===-1||u>C)&&(u=C);else for(u=c;u<C&&t.charCodeAt(u)!==35;)u++;let m=c,v=u;for(;m<v&&w(t.charCodeAt(m));)m++;for(;v>m&&w(t.charCodeAt(v-1));)v--;let q=t.charCodeAt(m);if(h=t.slice(m,v),h.includes("\u2028")||h.includes("\u2029")?h=h.replace(/^(['"`])([\s\S]*)\1$/mg,"$2"):v-m>=2&&(q===39||q===34||q===96)&&t.charCodeAt(v-1)===q&&(h=t.slice(m+1,v-1)),n=u,u<C)for(;n<o&&!O(t.charCodeAt(n));)n++}g===34&&(x||a<n)&&h.indexOf("\\")!==-1&&(h=h.replace(/\\n/g,`
`).replace(/\\r/g,"\r")),r[p]=h}return r}function ce(e,r){return r&&$(r.fast)?ie(e):se(e)}function T(e){console.log(`\u2506 ${e}`)}function ae(e){console.error(`\u25C7 ${e}`)}function B(e){return e[0]==="~"?N.join(te.homedir(),e.slice(1)):e}function le(e={}){let r={...e};for(let t of Object.keys(r))r[t]===void 0&&delete r[t];return{...H(),...r}}function fe(e){e=le(e);let r=N.resolve(process.cwd(),".env"),t="utf8",o=process.env;e&&e.processEnv!=null&&(o=e.processEnv);let n=$(e&&e.debug);e&&e.encoding?t=e.encoding:n&&T("no encoding is specified (UTF-8 is used by default)");let s=[r];if(e&&e.path)if(!Array.isArray(e.path))s=[B(e.path)];else{s=[];for(let c of e.path)s.push(B(c))}let i,l={},d={fast:e.fast};for(let c of s)try{let a=A.parse(ee.readFileSync(c,{encoding:t}),d);A.populate(l,a,e)}catch(a){n&&T(`failed to load ${c} ${a.message}`),i=a}let p=A.populate(o,l,e),f=$(Object.prototype.hasOwnProperty.call(e,"quiet")?e.quiet:H(o).quiet);if(n||!f){let c=Object.keys(p).length,a=[];for(let g of s)try{let h=N.relative(process.cwd(),g instanceof ne?re(g):g);a.push(h)}catch(h){n&&T(`failed to load ${g} ${h.message}`),i=h}ae(`injected env (${c}) from ${a.join(",")}`)}return i?{parsed:l,error:i}:{parsed:l}}function ue(e){return A.configDotenv(e)}function de(e,r,t={}){let o=$(t&&t.debug),n=$(t&&t.override),s={};if(e===null||typeof e!="object"||r===null||typeof r!="object"){let i=new Error("OBJECT_REQUIRED: Please check the processEnv argument being passed to populate");throw i.code="OBJECT_REQUIRED",i}for(let i of Object.keys(r))Object.prototype.hasOwnProperty.call(e,i)?(n===!0&&(e[i]=r[i],s[i]=r[i]),o&&T(n===!0?`"${i}" is already defined and WAS overwritten`:`"${i}" is already defined and was NOT overwritten`)):(e[i]=r[i],s[i]=r[i]);return s}var A={configDotenv:fe,config:ue,parse:ce,populate:de};y.exports.configDotenv=A.configDotenv;y.exports.config=A.config;y.exports.parse=A.parse;y.exports.populate=A.populate;y.exports=A});var he=require("fs"),W=require("os"),M=require("path"),pe=require("child_process"),ge=G(),_=L(),{optionsFromEnv:Q}=I();function S(){console.log(["Usage: dotenv run [--help] [-q|--quiet] [--debug] [--override] [--fast] [-f|--file <paths>] [--] <command> [args...]","","Run a command with environment variables from a .env file.","Place dotenv options before the command; all following arguments go to the command.","","Options:"," -f, --file <paths> .env paths, comma-separated or repeated (default: .env)"," -q, --quiet suppress the injected env message"," --debug enable debug logging"," --override override existing environment variables"," --fast use the faster character-scanner parser","","Environment variables (DOTENV_CONFIG_* names remain as fallbacks):"," DOTENV_PATH, DOTENV_ENCODING, DOTENV_QUIET,"," DOTENV_DEBUG, DOTENV_OVERRIDE,"," DOTENV_FAST"].join(`
`))}function me(e){let r=[],t=!1,o,n,s,i,l=-1;for(let p=0;p<e.length;p++){let f=e[p];if(f==="--"){l=p+1;break}if(f==="--help"||f==="-h")return{help:!0};if(f==="--quiet"||f==="-q"){o=!0;continue}if(f==="--debug"){n=!0;continue}if(f==="--override"){s=!0;continue}if(f==="--fast"){i=!0;continue}if(f==="-f"||f==="--file"||f.startsWith("-f=")||f.startsWith("--file=")){let c=f.indexOf("="),a=c===-1?f:f.slice(0,c),g=c===-1?e[++p]:f.slice(c+1);if(!g||g==="--")return{error:`${a} requires a path`};let h=g.split(",").map(x=>x.trim()).filter(Boolean);if(h.length===0)return{error:`${a} requires a path`};r.push(...h),t=!0;continue}if(f.startsWith("-"))return{error:`unknown option: ${f}`};l=p;break}let d=l===-1?[]:e.slice(l);return{paths:r,pathSet:t,quiet:o,debug:n,override:s,fast:i,command:d}}function Ce(e){return e[0]==="~"?M.join(W.homedir(),e.slice(1)):e}function ve(e){let r=Q(),t={encoding:r.encoding||"utf8",quiet:r.quiet,debug:r.debug===!0,override:r.override===!0,fast:r.fast===!0,paths:[".env"],defaultPath:!0};return r.path&&(t.paths=[r.path],t.defaultPath=!1),e.pathSet&&(t.paths=e.paths,t.defaultPath=!1),e.quiet!=null&&(t.quiet=e.quiet),e.debug!=null&&(t.debug=e.debug),e.override!=null&&(t.override=e.override),e.fast!=null&&(t.fast=e.fast),t}function we(e){let r={},t=[],o={override:e.override,debug:e.debug};for(let s of e.paths){let i=M.resolve(process.cwd(),Ce(s));try{let l=_.parse(he.readFileSync(i,{encoding:e.encoding}),{fast:e.fast});_.populate(r,l,o),t.push(s)}catch(l){if(e.debug&&console.log(`\u2506 failed to load ${s} ${l.message}`),!(e.defaultPath&&l.code==="ENOENT"))throw l}}return{injected:_.populate(process.env,r,o),loadedPaths:t}}function J(e){let r=e[0];if(r==="--help"||r==="-h"){S();return}if(r!=="run"){S(),process.exitCode=1;return}let t=me(e.slice(1));if(t.help){S();return}if(t.error){console.error(`dotenv: ${t.error}`),S(),process.exitCode=1;return}if(t.command.length===0){S(),process.exitCode=1;return}let o=ve(t);try{let c=we(o);if(!(o.quiet==null?Q().quiet:o.quiet)){let g=`\u25C7 injected env (${Object.keys(c.injected).length})`;c.loadedPaths.length>0&&(g+=` from ${c.loadedPaths.join(", ")}`),console.error(g)}}catch(c){console.error(`dotenv: ${c.message}`),process.exitCode=1;return}let n=!!process.stdin.isTTY,s=process.platform!=="win32"&&!n,i=ge(t.command[0],t.command.slice(1),{stdio:"inherit",detached:s}),l=new Map,d=0;function p(c){if(!(!i.pid||i.exitCode!==null||i.signalCode!==null)){if(process.platform==="win32"){pe.spawnSync("taskkill",["/pid",String(i.pid),"/T","/F"],{stdio:"ignore"});return}try{process.kill(s?-i.pid:i.pid,c)}catch(a){if(a.code!=="ESRCH")throw a}}}function f(){for(let[c,a]of l)process.removeListener(c,a)}for(let c of["SIGINT","SIGTERM","SIGHUP","SIGQUIT"]){let a=()=>{if(c==="SIGINT"){if(d++,n&&process.platform!=="win32"&&d===1)return;if(d>1){p(d===2?"SIGTERM":"SIGKILL");return}}p(c)};l.set(c,a),process.on(c,a)}i.on("error",function(c){f(),console.error(`dotenv: ${c.message}`),process.exitCode=1}),i.on("exit",function(c,a){f(),typeof c=="number"?process.exit(c):(setTimeout(()=>process.exit(128+W.constants.signals[a]),1e3),process.kill(process.pid,a))})}module.exports=J;require.main===module&&J(process.argv.slice(2));