@qlover/env-loader
Version:
A package for managing environment variables
2 lines (1 loc) • 1.95 kB
JavaScript
;var e=require("dotenv"),o=require("node:fs"),t=require("path"),r=function(){function r(e){this.options=e}return Object.defineProperty(r.prototype,"rootPath",{get:function(){return this.options.rootPath},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"logger",{get:function(){return this.options.logger},enumerable:!1,configurable:!0}),r.searchEnv=function(e){var n=void 0===e?{}:e,i=n.cwd,a=void 0===i?process.cwd():i,l=n.preloadList,c=void 0===l?[".env.local",".env"]:l,s=n.logger,d=n.maxDepth,v=void 0===d?5:d;v=Math.min(v,8);for(var p=new r({rootPath:a,logger:s}),u=a,f="",g=!1,h=0;u!==f;){if(g=c.some((function(e){return o.existsSync(t.resolve(u,e))}))){p.load({preloadList:c,rootPath:u});break}if(++h>=v){null==s||s.warn("Search depth exceeded ".concat(v," levels, stopping search at ").concat(u));break}if(f=u,(u=t.dirname(u))===f){null==s||s.warn("Reached root directory, stopping search");break}}return!g&&s&&s.warn("No environment files (".concat(c.join(", "),") found in directory tree from ").concat(a," to ").concat(u)),p},r.prototype.load=function(r){var n,i,a,l,c,s;void 0===r&&(r={preloadList:[]});var d=r.preloadList,v=r.rootPath;if(d.length){for(var p=v||this.rootPath||t.resolve("./"),u=0,f=d;u<f.length;u++){var g=f[u],h=t.resolve(p,g);if(o.existsSync(h))return e.config({path:h}),void(null===(l=null===(a=this.logger)||void 0===a?void 0:a.debug)||void 0===l||l.call(a,"Loaded `".concat(h,"` file")))}null===(s=null===(c=this.logger)||void 0===c?void 0:c.warn)||void 0===s||s.call(c,"No .env file found")}else null===(i=null===(n=this.logger)||void 0===n?void 0:n.warn)||void 0===i||i.call(n,"Env load preloadList is empty!")},r.prototype.remove=function(e){process.env[e]&&delete process.env[e]},r.prototype.get=function(e){return process.env[e]},r.prototype.set=function(e,o){process.env[e]=o},r.prototype.getDestroy=function(e){var o=process.env[e];return this.remove(e),o},r}();exports.Env=r;