dotenv-mono
Advanced tools
Comparing version 1.1.4 to 1.2.0
@@ -17,2 +17,14 @@ /// <reference types="node" /> | ||
/** | ||
* Dotenv matcher result. | ||
* @example `{ foundDotenv: './', foundDotenv: './.env' }` | ||
*/ | ||
export type DotenvMatcherResult = { | ||
foundPath: string | null | undefined; | ||
foundDotenv: string | null | undefined; | ||
}; | ||
/** | ||
* Dotenv matcher. | ||
*/ | ||
export type DotenvMatcher = (dotenv: string | null | undefined, cwd: string) => DotenvMatcherResult; | ||
/** | ||
* Configuration settings. | ||
@@ -22,3 +34,3 @@ */ | ||
/** | ||
* Specify the current working directory | ||
* Specify the current working directory. | ||
* @defaultValue `process.cwd()` | ||
@@ -29,3 +41,3 @@ * @example `require('dotenv-mono').load({ cwd: 'latin1' })` | ||
/** | ||
* Turn on/off logging to help debug why certain keys or values are not being set as you expect | ||
* Turn on/off logging to help debug why certain keys or values are not being set as you expect. | ||
* @defaultValue `false` | ||
@@ -36,3 +48,9 @@ * @example `require('dotenv-mono').load({ debug: true })` | ||
/** | ||
* Specify the max depth to reach finding up the folder from the children directory | ||
* Specify the defaults dotenv filename. | ||
* @defaultValue `.env.defaults` | ||
* @example `require('dotenv-mono').load({ defaults: '.env.default' })` | ||
*/ | ||
defaults?: string; | ||
/** | ||
* Specify the max depth to reach finding up the folder from the children directory. | ||
* @defaultValue `4` | ||
@@ -43,3 +61,3 @@ * @example `require('dotenv-mono').load({ depth: 3 })` | ||
/** | ||
* Specify the encoding of your file containing environment variables | ||
* Specify the encoding of your file containing environment variables. | ||
* @defaultValue `utf8` | ||
@@ -50,3 +68,3 @@ * @example `require('dotenv-mono').load({ encoding: 'latin1' })` | ||
/** | ||
* Turn on/off the dotenv-expand plugin | ||
* Turn on/off the dotenv-expand plugin. | ||
* @defaultValue `true` | ||
@@ -57,3 +75,3 @@ * @example `require('dotenv-mono').load({ expand: false })` | ||
/** | ||
* Specify to load specific dotenv file used only on specific apps/packages (ex. .env.server) | ||
* Specify to load specific dotenv file used only on specific apps/packages (ex. .env.server). | ||
* @example `require('dotenv-mono').load({ extension: 'server' })` | ||
@@ -63,3 +81,3 @@ */ | ||
/** | ||
* Override any environment variables that have already been set on your machine with values from your .env file | ||
* Override any environment variables that have already been set on your machine with values from your .env file. | ||
* @defaultValue `false` | ||
@@ -70,3 +88,3 @@ * @example `require('dotenv-mono').load({ override: true })` | ||
/** | ||
* Specify a custom path if your file containing environment variables is located elsewhere | ||
* Specify a custom path if your file containing environment variables is located elsewhere. | ||
* @example `require('dotenv-mono').load({ path: '../../configs/.env' })` | ||
@@ -76,3 +94,3 @@ */ | ||
/** | ||
* Specify the criteria of the filename priority to load as dotenv file | ||
* Specify the criteria of the filename priority to load as dotenv file. | ||
* @see https://github.com/marcocesarato/dotenv-mono | ||
@@ -85,7 +103,7 @@ * @defaultValue `{ '.env': 1, '.env.$(NODE_ENV)': 25, '.env.local': 50, '.env.$(NODE_ENV).local': 75 }` | ||
/** | ||
* Dotenv controller | ||
* Dotenv controller. | ||
*/ | ||
export declare class Dotenv { | ||
#private; | ||
config: DotenvConfigOutput | undefined; | ||
config: DotenvConfigOutput; | ||
env: DotenvData; | ||
@@ -97,17 +115,17 @@ extension: string | undefined; | ||
* Constructor. | ||
* @param cwd - Current Working Directory | ||
* @param debug - Turn on/off debugging | ||
* @param depth - Max walking up depth | ||
* @param encoding - File encoding | ||
* @param expand - Turn on/off dotenv-expand plugin | ||
* @param extension - Add dotenv extension | ||
* @param override - Override process variables | ||
* @param path - Dotenv path | ||
* @param priorities - Priorities | ||
* @param cwd - current Working Directory | ||
* @param debug - turn on/off debugging | ||
* @param depth - max walking up depth | ||
* @param encoding - file encoding | ||
* @param expand - turn on/off dotenv-expand plugin | ||
* @param extension - add dotenv extension | ||
* @param override - override process variables | ||
* @param path - dotenv path | ||
* @param priorities - priorities | ||
*/ | ||
constructor({ cwd, debug, depth, encoding, expand, extension, override, path, priorities, }?: DotenvConfig); | ||
constructor({ cwd, debug, defaults, depth, encoding, expand, extension, override, path, priorities, }?: DotenvConfig); | ||
/** | ||
* Get debugging. | ||
*/ | ||
get debug(): boolean | undefined; | ||
get debug(): boolean; | ||
/** | ||
@@ -119,2 +137,11 @@ * Set debugging. | ||
/** | ||
* Get defaults filename. | ||
*/ | ||
get defaults(): string; | ||
/** | ||
* Set defaults filename. | ||
* @param value | ||
*/ | ||
set defaults(value: string | undefined); | ||
/** | ||
* Get encoding. | ||
@@ -131,3 +158,3 @@ */ | ||
*/ | ||
get expand(): boolean | undefined; | ||
get expand(): boolean; | ||
/** | ||
@@ -149,3 +176,3 @@ * Turn on/off dotenv-expand plugin. | ||
*/ | ||
get depth(): number | undefined; | ||
get depth(): number; | ||
/** | ||
@@ -159,3 +186,3 @@ * Set depth. | ||
*/ | ||
get override(): boolean | undefined; | ||
get override(): boolean; | ||
/** | ||
@@ -182,3 +209,3 @@ * Set override. | ||
/** | ||
* Loads `.env` file contents. | ||
* Loads `.env` and default file contents. | ||
* @param loadOnProcess - load contents inside process | ||
@@ -189,2 +216,16 @@ * @returns current instance | ||
/** | ||
* Load with dotenv package and set parsed and plain content into the instance. | ||
* @private | ||
* @param file - path to dotenv | ||
* @param loadOnProcess - load contents inside process | ||
* @param defaults - is the default dotenv | ||
*/ | ||
private loadDotenv; | ||
/** | ||
* Merge dotenv package configs. | ||
* @private | ||
* @param config - dotenv config | ||
*/ | ||
private mergeDotenvConfig; | ||
/** | ||
* Loads `.env` file contents. | ||
@@ -198,4 +239,20 @@ * @returns current instance | ||
*/ | ||
find(): string | undefined; | ||
find(matcher?: DotenvMatcher): string | null | undefined; | ||
/** | ||
* Dotenv matcher. | ||
* @private | ||
* @param dotenv - dotenv result | ||
* @param cwd - current working directory | ||
* @returns paths found | ||
*/ | ||
private dotenvMatcher; | ||
/** | ||
* Defaults dotenv matcher. | ||
* @private | ||
* @param dotenv - dotenv result | ||
* @param cwd - current working directory | ||
* @returns paths found | ||
*/ | ||
private dotenvDefaultsMatcher; | ||
/** | ||
* Save `.env` file contents. | ||
@@ -207,3 +264,3 @@ * @param changes - data to change on the dotenv | ||
/** | ||
* Escape regex | ||
* Escape regex. | ||
* @param string - string to escape | ||
@@ -215,4 +272,4 @@ * @returns escaped string | ||
/** | ||
* Load dotenv on process and return instance of Dotenv | ||
* @param props - Configuration | ||
* Load dotenv on process and return instance of Dotenv. | ||
* @param props - configuration | ||
* @returns Dotenv instance | ||
@@ -226,4 +283,4 @@ */ | ||
/** | ||
* Load dotenv on process and return the dotenv output | ||
* @param props - Configuration | ||
* Load dotenv on process and return the dotenv output. | ||
* @param props - configuration | ||
* @returns DotenvConfigOutput | ||
@@ -230,0 +287,0 @@ */ |
@@ -16,3 +16,3 @@ "use strict"; | ||
}; | ||
var _Dotenv__cwd, _Dotenv__debug, _Dotenv__depth, _Dotenv__encoding, _Dotenv__expand, _Dotenv__override, _Dotenv__priorities, _Dotenv__nodeEnv; | ||
var _Dotenv__cwd, _Dotenv__debug, _Dotenv__defaults, _Dotenv__depth, _Dotenv__encoding, _Dotenv__expand, _Dotenv__override, _Dotenv__priorities, _Dotenv__nodeEnv; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -25,3 +25,3 @@ exports.config = exports.dotenvConfig = exports.load = exports.dotenvLoad = exports.Dotenv = void 0; | ||
/** | ||
* Dotenv controller | ||
* Dotenv controller. | ||
*/ | ||
@@ -31,13 +31,15 @@ class Dotenv { | ||
* Constructor. | ||
* @param cwd - Current Working Directory | ||
* @param debug - Turn on/off debugging | ||
* @param depth - Max walking up depth | ||
* @param encoding - File encoding | ||
* @param expand - Turn on/off dotenv-expand plugin | ||
* @param extension - Add dotenv extension | ||
* @param override - Override process variables | ||
* @param path - Dotenv path | ||
* @param priorities - Priorities | ||
* @param cwd - current Working Directory | ||
* @param debug - turn on/off debugging | ||
* @param depth - max walking up depth | ||
* @param encoding - file encoding | ||
* @param expand - turn on/off dotenv-expand plugin | ||
* @param extension - add dotenv extension | ||
* @param override - override process variables | ||
* @param path - dotenv path | ||
* @param priorities - priorities | ||
*/ | ||
constructor({ cwd, debug, depth, encoding, expand, extension, override, path, priorities, } = {}) { | ||
constructor({ cwd, debug, defaults, depth, encoding, expand, extension, override, path, priorities, } = {}) { | ||
// Public config properties | ||
this.config = {}; | ||
this.env = {}; | ||
@@ -48,2 +50,3 @@ this.plain = ""; | ||
_Dotenv__debug.set(this, false); | ||
_Dotenv__defaults.set(this, ".env.defaults"); | ||
_Dotenv__depth.set(this, 4); | ||
@@ -57,2 +60,3 @@ _Dotenv__encoding.set(this, "utf8"); | ||
this.debug = debug; | ||
this.defaults = defaults; | ||
this.depth = depth; | ||
@@ -65,2 +69,5 @@ this.encoding = encoding; | ||
this.priorities = priorities; | ||
// Auto-bind matchers | ||
this.dotenvDefaultsMatcher = this.dotenvDefaultsMatcher.bind(this); | ||
this.dotenvMatcher = this.dotenvMatcher.bind(this); | ||
} | ||
@@ -82,2 +89,16 @@ /** | ||
/** | ||
* Get defaults filename. | ||
*/ | ||
get defaults() { | ||
return __classPrivateFieldGet(this, _Dotenv__defaults, "f"); | ||
} | ||
/** | ||
* Set defaults filename. | ||
* @param value | ||
*/ | ||
set defaults(value) { | ||
if (value != null) | ||
__classPrivateFieldSet(this, _Dotenv__defaults, value, "f"); | ||
} | ||
/** | ||
* Get encoding. | ||
@@ -184,3 +205,3 @@ */ | ||
/** | ||
* Loads `.env` file contents. | ||
* Loads `.env` and default file contents. | ||
* @param loadOnProcess - load contents inside process | ||
@@ -190,22 +211,52 @@ * @returns current instance | ||
load(loadOnProcess = true) { | ||
var _a, _b, _c; | ||
var _a; | ||
// Reset | ||
this.env = {}; | ||
this.config = {}; | ||
// Load dotenv source file | ||
const file = (_a = this.path) !== null && _a !== void 0 ? _a : this.find(); | ||
if (fs_1.default.existsSync(file)) { | ||
if (loadOnProcess) { | ||
this.config = dotenv_1.default.config({ | ||
path: file, | ||
debug: this.debug, | ||
encoding: this.encoding, | ||
override: this.override, | ||
}); | ||
if (this.expand) { | ||
this.config = dotenv_expand_1.default.expand(this.config); | ||
} | ||
this.env = (_c = (_b = this.config) === null || _b === void 0 ? void 0 : _b.parsed) !== null && _c !== void 0 ? _c : {}; | ||
} | ||
this.plain = fs_1.default.readFileSync(file, { encoding: this.encoding, flag: "r" }); | ||
} | ||
this.loadDotenv(file, loadOnProcess); | ||
// Load default without override the source file | ||
const defaultFile = this.find(this.dotenvDefaultsMatcher); | ||
this.loadDotenv(defaultFile, loadOnProcess, true); | ||
return this; | ||
} | ||
/** | ||
* Load with dotenv package and set parsed and plain content into the instance. | ||
* @private | ||
* @param file - path to dotenv | ||
* @param loadOnProcess - load contents inside process | ||
* @param defaults - is the default dotenv | ||
*/ | ||
loadDotenv(file, loadOnProcess, defaults = false) { | ||
if (!file || !fs_1.default.existsSync(file)) | ||
return; | ||
if (loadOnProcess) { | ||
let config = dotenv_1.default.config({ | ||
path: file, | ||
debug: this.debug, | ||
encoding: this.encoding, | ||
override: !defaults && this.override, | ||
}); | ||
if (this.expand) | ||
config = dotenv_expand_1.default.expand(config); | ||
this.mergeDotenvConfig(config); | ||
} | ||
if (!defaults) | ||
this.plain = fs_1.default.readFileSync(file, { encoding: this.encoding, flag: "r" }); | ||
} | ||
/** | ||
* Merge dotenv package configs. | ||
* @private | ||
* @param config - dotenv config | ||
*/ | ||
mergeDotenvConfig(config) { | ||
var _a, _b, _c, _d, _e; | ||
this.config = { | ||
parsed: Object.assign(Object.assign({}, ((_a = this.config.parsed) !== null && _a !== void 0 ? _a : {})), ((_b = config.parsed) !== null && _b !== void 0 ? _b : {})), | ||
error: (_d = (_c = this.config.error) !== null && _c !== void 0 ? _c : config.error) !== null && _d !== void 0 ? _d : undefined, | ||
}; | ||
this.env = Object.assign(Object.assign({}, this.env), ((_e = this.config.parsed) !== null && _e !== void 0 ? _e : {})); | ||
} | ||
/** | ||
* Loads `.env` file contents. | ||
@@ -222,25 +273,8 @@ * @returns current instance | ||
*/ | ||
find() { | ||
let dotenv; | ||
find(matcher) { | ||
if (!matcher) | ||
matcher = this.dotenvMatcher; | ||
let dotenv = null; | ||
let directory = path_1.default.resolve(this.cwd); | ||
const { root } = path_1.default.parse(directory); | ||
const matcher = (cwd) => { | ||
const priority = -1; | ||
Object.keys(this.priorities).forEach((fileName) => { | ||
if (this.priorities[fileName] > priority && | ||
fs_1.default.existsSync(path_1.default.join(cwd, fileName))) { | ||
dotenv = path_1.default.join(cwd, fileName); | ||
} | ||
}); | ||
const foundPath = dotenv != null ? cwd : null; | ||
if (typeof foundPath === "string") { | ||
try { | ||
const stat = fs_1.default.statSync(path_1.default.resolve(cwd, foundPath)); | ||
if (stat.isDirectory()) | ||
return foundPath; | ||
} | ||
catch (_a) { } | ||
} | ||
return foundPath; | ||
}; | ||
let depth = 0; | ||
@@ -250,3 +284,4 @@ let match = false; | ||
depth++; | ||
const foundPath = matcher(directory); | ||
const { foundPath, foundDotenv } = matcher(dotenv, directory); | ||
dotenv = foundDotenv; | ||
if (match) | ||
@@ -263,2 +298,49 @@ break; | ||
/** | ||
* Dotenv matcher. | ||
* @private | ||
* @param dotenv - dotenv result | ||
* @param cwd - current working directory | ||
* @returns paths found | ||
*/ | ||
dotenvMatcher(dotenv, cwd) { | ||
const priority = -1; | ||
Object.keys(this.priorities).forEach((fileName) => { | ||
if (this.priorities[fileName] > priority && fs_1.default.existsSync(path_1.default.join(cwd, fileName))) { | ||
dotenv = path_1.default.join(cwd, fileName); | ||
} | ||
}); | ||
const foundPath = dotenv != null ? cwd : null; | ||
if (typeof foundPath === "string") { | ||
try { | ||
const stat = fs_1.default.statSync(path_1.default.resolve(cwd, foundPath)); | ||
if (stat.isDirectory()) | ||
return { foundPath, foundDotenv: dotenv }; | ||
} | ||
catch (_a) { } | ||
} | ||
return { foundPath, foundDotenv: dotenv }; | ||
} | ||
/** | ||
* Defaults dotenv matcher. | ||
* @private | ||
* @param dotenv - dotenv result | ||
* @param cwd - current working directory | ||
* @returns paths found | ||
*/ | ||
dotenvDefaultsMatcher(dotenv, cwd) { | ||
if (fs_1.default.existsSync(path_1.default.join(cwd, this.defaults))) { | ||
dotenv = path_1.default.join(cwd, this.defaults); | ||
} | ||
const foundPath = dotenv != null ? cwd : null; | ||
if (typeof foundPath === "string") { | ||
try { | ||
const stat = fs_1.default.statSync(path_1.default.resolve(cwd, foundPath)); | ||
if (stat.isDirectory()) | ||
return { foundPath, foundDotenv: dotenv }; | ||
} | ||
catch (_a) { } | ||
} | ||
return { foundPath, foundDotenv: dotenv }; | ||
} | ||
/** | ||
* Save `.env` file contents. | ||
@@ -323,3 +405,3 @@ * @param changes - data to change on the dotenv | ||
/** | ||
* Escape regex | ||
* Escape regex. | ||
* @param string - string to escape | ||
@@ -333,6 +415,6 @@ * @returns escaped string | ||
exports.Dotenv = Dotenv; | ||
_Dotenv__cwd = new WeakMap(), _Dotenv__debug = new WeakMap(), _Dotenv__depth = new WeakMap(), _Dotenv__encoding = new WeakMap(), _Dotenv__expand = new WeakMap(), _Dotenv__override = new WeakMap(), _Dotenv__priorities = new WeakMap(), _Dotenv__nodeEnv = new WeakMap(); | ||
_Dotenv__cwd = new WeakMap(), _Dotenv__debug = new WeakMap(), _Dotenv__defaults = new WeakMap(), _Dotenv__depth = new WeakMap(), _Dotenv__encoding = new WeakMap(), _Dotenv__expand = new WeakMap(), _Dotenv__override = new WeakMap(), _Dotenv__priorities = new WeakMap(), _Dotenv__nodeEnv = new WeakMap(); | ||
/** | ||
* Load dotenv on process and return instance of Dotenv | ||
* @param props - Configuration | ||
* Load dotenv on process and return instance of Dotenv. | ||
* @param props - configuration | ||
* @returns Dotenv instance | ||
@@ -350,4 +432,4 @@ */ | ||
/** | ||
* Load dotenv on process and return the dotenv output | ||
* @param props - Configuration | ||
* Load dotenv on process and return the dotenv output. | ||
* @param props - configuration | ||
* @returns DotenvConfigOutput | ||
@@ -354,0 +436,0 @@ */ |
{ | ||
"name": "dotenv-mono", | ||
"version": "1.1.4", | ||
"version": "1.2.0", | ||
"main": "./dist/index.js", | ||
@@ -69,2 +69,5 @@ "module": "./dist/index.js", | ||
"dotenv-expand", | ||
"expand", | ||
"vars", | ||
"variables", | ||
"load", | ||
@@ -97,5 +100,8 @@ "loader", | ||
"turborepo", | ||
"storybook", | ||
"shared", | ||
"share" | ||
"share", | ||
"default", | ||
"defaults" | ||
] | ||
} |
@@ -26,3 +26,3 @@ <div align="center"> | ||
It also includes some extra features such as manipulation and saving of changes to the dotenv file. | ||
It also includes some extra features such as manipulation and saving of changes to the dotenv file, a default centralized file, and a file loader with ordering and priorities. | ||
@@ -35,2 +35,4 @@ The plugin [dotenv-expand](https://www.npmjs.com/package/dotenv-expand) is enabled by default. | ||
βββ .env | ||
βββ .env.production | ||
βββ .env.defaults | ||
βββ packages | ||
@@ -41,2 +43,3 @@ β βββ ui-library | ||
β βββ web | ||
β β βββ .storybook | ||
β βββ docs | ||
@@ -135,2 +138,24 @@ ``` | ||
### Install on Storybook | ||
The main configuration file is `.storybook/main.js`. This file controls the Storybook server's behavior, so you must restart Storybookβs process when you change it. | ||
Add the following lines on the file: | ||
```js | ||
const dotenv = require("dotenv-mono").load(); | ||
const config = { | ||
/* config options here */ | ||
env: (config) => { | ||
return { | ||
...config, | ||
...dotenv.env, | ||
}; | ||
}, | ||
}; | ||
module.exports = config; | ||
``` | ||
## π» Usage | ||
@@ -194,2 +219,8 @@ | ||
### Change default filename | ||
```js | ||
load({defaults: ".env.def"}); | ||
``` | ||
### Change priorities | ||
@@ -231,2 +262,3 @@ | ||
| `debug` | Turn on/off logging to help debug why certain keys or values are not being set as you expect | `false` | | ||
| `defaults` | Specify the defaults dotenv filename (it **can't** override any environment variables) | `.env.defaults` | | ||
| `depth` | Specify the max depth to reach finding up the folder from the children directory | `4` | | ||
@@ -246,3 +278,3 @@ | `encoding` | Specify the encoding of your file containing environment variables | `utf8` | | ||
> NOTE: This method differs from the previous `load` **function**. | ||
> Note: This method differs from the previous `load` **function**. | ||
> In that it requires the configuration to be loaded on the class instance via the constructor. | ||
@@ -249,0 +281,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49434
703
315