dotenv
Version:
Loads environment variables from .env file
9 lines (8 loc) • 10.9 kB
JavaScript
var j=(e,n)=>()=>{try{return n||e((n={exports:{}}).exports,n),n.exports}catch(t){throw n=0,t}};var k=j((xe,U)=>{function G(e){return typeof e=="string"?!["false","0","no","off",""].includes(e.toLowerCase()):!!e}function Y(e=process.env){let n={};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&&(n[t.toLowerCase()]=t==="ENCODING"||t==="PATH"?o:G(o))}return n}U.exports={parseBoolean:G,optionsFromEnv:Y}});var N=j((ye,x)=>{var z=require("fs"),I=require("path"),Z=require("os"),{URL:ee,fileURLToPath:te}=require("url"),{parseBoolean:$,optionsFromEnv:B}=k(),re=/(?:^|^)\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 ne(e){let n={},t=e.toString();t=t.replace(/\r\n?/mg,`
`);let o;for(;(o=re.exec(t))!=null;){let r=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")),n[r]=s}return n}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 q(e){return e===10||e===8232||e===8233}function oe(e){let n={},t=typeof e=="string"?e:e.toString();t.indexOf("\r")!==-1&&(t=t.replace(/\r\n?/g,`
`));let o=t.length,r=0;for(;r<o;){let s=t.charCodeAt(r);for(;r<o&&w(s);)r++,s=t.charCodeAt(r);if(r>=o)break;if(s===35){for(;r<o&&!q(t.charCodeAt(r));)r++;continue}let i=-1;if(s===101&&r+6<o&&t.charCodeAt(r+1)===120&&t.charCodeAt(r+2)===112&&t.charCodeAt(r+3)===111&&t.charCodeAt(r+4)===114&&t.charCodeAt(r+5)===116){let v=t.charCodeAt(r+6);if(w(v)){let u=r+7;for(;u<o&&w(t.charCodeAt(u));)u++;b[t.charCodeAt(u)]&&(i=r+6,r=u)}else s=t.charCodeAt(r)}let l=r,d=0;for(;r<o&&(d=t.charCodeAt(r),b[d]);)r++;if(r===l){for(;r<o&&!q(t.charCodeAt(r));)r++;continue}let h=t.slice(l,r),f=r;if(r>=o&&(d=0),w(d))do r++,d=r<o?t.charCodeAt(r):0;while(w(d));if(d===61)r++;else if(d===58&&r===f&&r+1<o&&w(t.charCodeAt(r+1)))r+=2;else{for(r=i===-1?f:i;r<o&&!q(t.charCodeAt(r));)r++;continue}let c=r,a=r;for(;a<o&&w(t.charCodeAt(a));)a++;let g=t.charCodeAt(a),p,y=!1;if(g===39||g===34||g===96){let v=t[a],u=t.indexOf(v,a+1),m=-1,C=-1;for(;u!==-1;){let O=t.charCodeAt(u-1)===92,E=u+1;for(;E<o&&!q(t.charCodeAt(E))&&w(t.charCodeAt(E));)E++;if((E===o||q(t.charCodeAt(E))||t.charCodeAt(E)===35)&&(m=u,C=E),!O)break;u=t.indexOf(v,u+1)}if(m!==-1){if(p=t.slice(a+1,m),r=C,t.charCodeAt(r)===35)for(;r<o&&!q(t.charCodeAt(r));)r++;y=!0}}if(!y){let v=t.indexOf(`
`,c);v===-1&&(v=o);let u;if(o<4096)u=t.indexOf("#",c),(u===-1||u>v)&&(u=v);else for(u=c;u<v&&t.charCodeAt(u)!==35;)u++;let m=c,C=u;for(;m<C&&w(t.charCodeAt(m));)m++;for(;C>m&&w(t.charCodeAt(C-1));)C--;let O=t.charCodeAt(m);if(p=t.slice(m,C),p.includes("\u2028")||p.includes("\u2029")?p=p.replace(/^(['"`])([\s\S]*)\1$/mg,"$2"):C-m>=2&&(O===39||O===34||O===96)&&t.charCodeAt(C-1)===O&&(p=t.slice(m+1,C-1)),r=u,u<v)for(;r<o&&!q(t.charCodeAt(r));)r++}g===34&&(y||a<r)&&p.indexOf("\\")!==-1&&(p=p.replace(/\\n/g,`
`).replace(/\\r/g,"\r")),n[h]=p}return n}function se(e,n){return n&&$(n.fast)?oe(e):ne(e)}function T(e){console.log(`\u2506 ${e}`)}function ie(e){console.error(`\u25C7 ${e}`)}function V(e){return e[0]==="~"?I.join(Z.homedir(),e.slice(1)):e}function ce(e={}){let n={...e};for(let t of Object.keys(n))n[t]===void 0&&delete n[t];return{...B(),...n}}function ae(e){e=ce(e);let n=I.resolve(process.cwd(),".env"),t="utf8",o=process.env;e&&e.processEnv!=null&&(o=e.processEnv);let r=$(e&&e.debug);e&&e.encoding?t=e.encoding:r&&T("no encoding is specified (UTF-8 is used by default)");let s=[n];if(e&&e.path)if(!Array.isArray(e.path))s=[V(e.path)];else{s=[];for(let c of e.path)s.push(V(c))}let i,l={},d={fast:e.fast};for(let c of s)try{let a=A.parse(z.readFileSync(c,{encoding:t}),d);A.populate(l,a,e)}catch(a){r&&T(`failed to load ${c} ${a.message}`),i=a}let h=A.populate(o,l,e),f=$(Object.prototype.hasOwnProperty.call(e,"quiet")?e.quiet:B(o).quiet);if(r||!f){let c=Object.keys(h).length,a=[];for(let g of s)try{let p=I.relative(process.cwd(),g instanceof ee?te(g):g);a.push(p)}catch(p){r&&T(`failed to load ${g} ${p.message}`),i=p}ie(`injected env (${c}) from ${a.join(",")}`)}return i?{parsed:l,error:i}:{parsed:l}}function le(e){return A.configDotenv(e)}function fe(e,n,t={}){let o=$(t&&t.debug),r=$(t&&t.override),s={};if(e===null||typeof e!="object"||n===null||typeof n!="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(n))Object.prototype.hasOwnProperty.call(e,i)?(r===!0&&(e[i]=n[i],s[i]=n[i]),o&&T(r===!0?`"${i}" is already defined and WAS overwritten`:`"${i}" is already defined and was NOT overwritten`)):(e[i]=n[i],s[i]=n[i]);return s}var A={configDotenv:ae,config:le,parse:se,populate:fe};x.exports.configDotenv=A.configDotenv;x.exports.config=A.config;x.exports.parse=A.parse;x.exports.populate=A.populate;x.exports=A});var M=j((Te,W)=>{var _=require("child_process"),ue=require("fs"),L=require("path");function de(e){let n=['"'],t=0;for(let o of e){if(o==="\\"){t++;continue}o==='"'?n.push("\\".repeat(t*2+1),'"'):n.push("\\".repeat(t),o),t=0}return n.push("\\".repeat(t*2),'"'),n.join("")}function H(e,n=1){for(let t=0;t<n;t++){let o=[];for(let r of e){let s=r.charCodeAt(0),i=s>=48&&s<=57||s>=65&&s<=90||s>=97&&s<=122,l="\\/:._-".includes(r);!i&&!l&&s<128&&o.push("^"),o.push(r)}e=o.join("")}return e}function P(e,n){let t=Object.keys(e).reverse().find(o=>o.toUpperCase()===n);return t===void 0?void 0:e[t]}function pe(e,n,t){let o=(P(n,"PATHEXT")||".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean),s=o.some(l=>e.toLowerCase().endsWith(l.toLowerCase()))?["",...o]:[...o,""],i=/[\\/]/.test(e)?[t]:[t,...(P(n,"PATH")||"").split(";")];for(let l of i)for(let d of s){let h=L.resolve(t,l.replace(/^"|"$/g,""),e+d);try{if(ue.statSync(h).isFile())return h}catch{}}}function he(e,n,t){if(process.platform!=="win32")return _.spawn(e,n,t);let o=t.env||process.env,r=pe(e,o,t.cwd||process.cwd());if(r&&/\.(?:exe|com)$/i.test(r))return _.spawn(r,n,t);let s=/\.(?:bat|cmd)$/i.test(r||e),i=[H(L.normalize(r||e))];for(let d of n)i.push(H(de(d),s?2:1));let l=i.join(" ");return _.spawn(P(o,"COMSPEC")||"cmd.exe",["/d","/v:off","/s","/c",`"${l}"`],{...t,windowsVerbatimArguments:!0})}W.exports=he});var X=j(($e,F)=>{var ge=require("fs"),me=require("os"),Q=require("path"),ve=require("child_process"),Ce=M(),R=N(),{optionsFromEnv:J}=k();function D(){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 we(e){let n=[],t=!1,o,r,s,i,l=-1;for(let h=0;h<e.length;h++){let f=e[h];if(f==="--"){l=h+1;break}if(f==="--help"||f==="-h")return{help:!0};if(f==="--quiet"||f==="-q"){o=!0;continue}if(f==="--debug"){r=!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[++h]:f.slice(c+1);if(!g||g==="--")return{error:`${a} requires a path`};let p=g.split(",").map(y=>y.trim()).filter(Boolean);if(p.length===0)return{error:`${a} requires a path`};n.push(...p),t=!0;continue}if(f.startsWith("-"))return{error:`unknown option: ${f}`};l=h;break}let d=l===-1?[]:e.slice(l);return{paths:n,pathSet:t,quiet:o,debug:r,override:s,fast:i,command:d}}function Ae(e){return e[0]==="~"?Q.join(me.homedir(),e.slice(1)):e}function Ee(e){let n=J(),t={encoding:n.encoding||"utf8",quiet:n.quiet,debug:n.debug===!0,override:n.override===!0,fast:n.fast===!0,paths:[".env"],defaultPath:!0};return n.path&&(t.paths=[n.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 be(e){let n={},t=[],o={override:e.override,debug:e.debug};for(let s of e.paths){let i=Q.resolve(process.cwd(),Ae(s));try{let l=R.parse(ge.readFileSync(i,{encoding:e.encoding}),{fast:e.fast});R.populate(n,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:R.populate(process.env,n,o),loadedPaths:t}}function K(e){let n=e[0];if(n==="--help"||n==="-h"){D();return}if(n!=="run"){D(),process.exitCode=1;return}let t=we(e.slice(1));if(t.help){D();return}if(t.error){console.error(`dotenv: ${t.error}`),D(),process.exitCode=1;return}if(t.command.length===0){D(),process.exitCode=1;return}let o=Ee(t);try{let c=be(o);if(!(o.quiet==null?J().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 r=!!process.stdin.isTTY,s=process.platform!=="win32"&&!r,i=Ce(t.command[0],t.command.slice(1),{stdio:"inherit",detached:s}),l=new Map,d=0;function h(c){if(!(!i.pid||i.exitCode!==null||i.signalCode!==null)){if(process.platform==="win32"){ve.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++,r&&process.platform!=="win32"&&d===1)return;if(d>1){h(d===2?"SIGTERM":"SIGKILL");return}}h(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):(setInterval(()=>{},1e3),process.kill(process.pid,a))})}F.exports=K;require.main===F&&K(process.argv.slice(2))});var S=N(),qe=X();module.exports=S;module.exports.config=S.config;module.exports.configDotenv=S.configDotenv;module.exports.parse=S.parse;module.exports.populate=S.populate;require.main===module&&qe(process.argv.slice(2));