UNPKG

@qlover/env-loader

Version:

A package for managing environment variables

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