dotenv-mono
Advanced tools
Comparing version 1.0.1 to 1.1.0
/// <reference types="node" /> | ||
import { DotenvParseOutput } from "dotenv"; | ||
import { DotenvConfigOutput, DotenvParseOutput } from "dotenv"; | ||
/** | ||
* Environment variables list | ||
* @example `{ EXAMPLE: "1", EXAMPLE_2: "2" }` | ||
*/ | ||
export declare type DotenvData = Record<string, any>; | ||
/** | ||
* Criteria of the filename priority to load as dotenv file | ||
* @see https://github.com/marcocesarato/dotenv-mono | ||
* @example `{ '.env': 1, '.env.$(NODE_ENV)': 25, '.env.local': 50, '.env.$(NODE_ENV).local': 75 }` | ||
*/ | ||
export declare type DotenvPriorities = { | ||
@@ -8,13 +17,61 @@ [key: string]: number; | ||
export declare type DotenvArgs = { | ||
/** | ||
* Specify the current working directory | ||
* @defaultValue `process.cwd()` | ||
* @example `require('dotenv-mono').load({ cwd: 'latin1' })` | ||
*/ | ||
cwd?: string; | ||
/** | ||
* Turn on/off logging to help debug why certain keys or values are not being set as you expect | ||
* @defaultValue `false` | ||
* @example `require('dotenv-mono').load({ debug: true })` | ||
*/ | ||
debug?: boolean; | ||
/** | ||
* Specify the max depth to reach finding up the folder from the children directory | ||
* @defaultValue `4` | ||
* @example `require('dotenv-mono').load({ depth: 3 })` | ||
*/ | ||
depth?: number; | ||
/** | ||
* Specify the encoding of your file containing environment variables | ||
* @defaultValue `utf8` | ||
* @example `require('dotenv-mono').load({ encoding: 'latin1' })` | ||
*/ | ||
encoding?: BufferEncoding; | ||
/** | ||
* Turn on/off the dotenv-expand plugin | ||
* @defaultValue `true` | ||
* @example `require('dotenv-mono').load({ expand: false })` | ||
*/ | ||
expand?: boolean; | ||
/** | ||
* Specify to load specific dotenv file used only on specific apps/packages (ex. .env.server) | ||
* @example `require('dotenv-mono').load({ extension: 'server' })` | ||
*/ | ||
extension?: string; | ||
expand?: boolean; | ||
/** | ||
* Override any environment variables that have already been set on your machine with values from your .env file | ||
* @defaultValue `false` | ||
* @example `require('dotenv-mono').load({ override: true })` | ||
*/ | ||
override?: boolean; | ||
/** | ||
* Specify a custom path if your file containing environment variables is located elsewhere | ||
* @example `require('dotenv-mono').load({ path: '../../configs/.env' })` | ||
*/ | ||
path?: string; | ||
/** | ||
* Specify the criteria of the filename priority to load as dotenv file | ||
* @see https://github.com/marcocesarato/dotenv-mono | ||
* @defaultValue `{ '.env': 1, '.env.$(NODE_ENV)': 25, '.env.local': 50, '.env.$(NODE_ENV).local': 75 }` | ||
* @example `require('dotenv-mono').load({ priorities: { '.env.overwrite': 100 } })` | ||
*/ | ||
priorities?: DotenvPriorities; | ||
}; | ||
/** | ||
* Dotenv controller | ||
*/ | ||
export declare class Dotenv { | ||
config: DotenvConfigOutput | undefined; | ||
env: DotenvData; | ||
@@ -32,3 +89,3 @@ extension: string | undefined; | ||
private _nodeEnv; | ||
constructor({ path, cwd, extension, expand, depth, priorities, encoding, debug, override, }?: DotenvArgs); | ||
constructor({ cwd, debug, depth, encoding, expand, extension, override, path, priorities, }?: DotenvArgs); | ||
get debug(): boolean | undefined; | ||
@@ -48,10 +105,54 @@ set debug(value: boolean | undefined); | ||
set priorities(value: DotenvPriorities | undefined); | ||
/** | ||
* Parses a string or buffer in the .env file format into an object. | ||
* @see https://docs.dotenv.org | ||
* @returns an object with keys and values based on `src`. example: `{ DB_HOST : 'localhost' }` | ||
*/ | ||
parse<T extends DotenvParseOutput = DotenvParseOutput>(): T; | ||
/** | ||
* Loads `.env` file contents. | ||
* @param loadOnProcess - load contents inside process | ||
*/ | ||
load(loadOnProcess?: boolean): this; | ||
/** | ||
* Loads `.env` file contents. | ||
*/ | ||
loadFile(): this; | ||
/** | ||
* Find first `.env` file walking up from cwd directory based on priority criteria. | ||
* @return file matched with higher priority | ||
*/ | ||
find(): string | undefined; | ||
/** | ||
* Save `.env` file contents. | ||
* @param changes - data to change on the dotenv | ||
*/ | ||
save(changes: DotenvData): this; | ||
escapeRegExp(string: string): string; | ||
/** | ||
* Escape regex | ||
* @param string - string to escape | ||
* @return escaped string | ||
*/ | ||
private escapeRegExp; | ||
} | ||
/** | ||
* Load dotenv on process and return instance of Dotenv | ||
* @param DotenvArgs props | ||
* @return Dotenv | ||
*/ | ||
export declare function dotenvLoad(): Dotenv; | ||
/** | ||
* @see dotenvLoad | ||
*/ | ||
export declare const load: typeof dotenvLoad; | ||
/** | ||
* Load dotenv on process and return the dotenv output | ||
* @param DotenvArgs props | ||
* @return Dotenv | ||
*/ | ||
export declare function dotenvConfig(): DotenvConfigOutput; | ||
/** | ||
* @see dotenvConfig | ||
*/ | ||
export declare const config: typeof dotenvConfig; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.dotenvLoad = exports.Dotenv = void 0; | ||
exports.config = exports.dotenvConfig = exports.load = exports.dotenvLoad = exports.Dotenv = void 0; | ||
const fs_1 = __importDefault(require("fs")); | ||
@@ -12,4 +12,7 @@ const path_1 = __importDefault(require("path")); | ||
const dotenv_expand_1 = __importDefault(require("dotenv-expand")); | ||
/** | ||
* Dotenv controller | ||
*/ | ||
class Dotenv { | ||
constructor({ path, cwd, extension, expand, depth, priorities, encoding, debug, override, } = {}) { | ||
constructor({ cwd, debug, depth, encoding, expand, extension, override, path, priorities, } = {}) { | ||
this.env = {}; | ||
@@ -25,11 +28,11 @@ this.plain = ""; | ||
this._nodeEnv = ""; | ||
this.path = path; | ||
this.cwd = cwd; | ||
this.extension = extension; | ||
this.expand = expand; | ||
this.debug = debug; | ||
this.depth = depth; | ||
this.priorities = priorities; | ||
this.encoding = encoding; | ||
this.debug = debug; | ||
this.expand = expand; | ||
this.extension = extension; | ||
this.override = override; | ||
this.path = path; | ||
this.priorities = priorities; | ||
} | ||
@@ -94,2 +97,7 @@ get debug() { | ||
} | ||
/** | ||
* Parses a string or buffer in the .env file format into an object. | ||
* @see https://docs.dotenv.org | ||
* @returns an object with keys and values based on `src`. example: `{ DB_HOST : 'localhost' }` | ||
*/ | ||
parse() { | ||
@@ -99,8 +107,12 @@ // @ts-ignore | ||
} | ||
/** | ||
* Loads `.env` file contents. | ||
* @param loadOnProcess - load contents inside process | ||
*/ | ||
load(loadOnProcess = true) { | ||
var _a, _b, _c, _d; | ||
var _a, _b, _c; | ||
const file = (_a = this.path) !== null && _a !== void 0 ? _a : this.find(); | ||
if (fs_1.default.existsSync(file)) { | ||
if (loadOnProcess) { | ||
const config = dotenv_1.default.config({ | ||
this.config = dotenv_1.default.config({ | ||
path: file, | ||
@@ -112,7 +124,5 @@ debug: this.debug, | ||
if (this.expand) { | ||
this.env = (_c = (_b = dotenv_expand_1.default.expand(config)) === null || _b === void 0 ? void 0 : _b.parsed) !== null && _c !== void 0 ? _c : {}; | ||
this.config = dotenv_expand_1.default.expand(this.config); | ||
} | ||
else { | ||
this.env = (_d = config === null || config === void 0 ? void 0 : config.parsed) !== null && _d !== void 0 ? _d : {}; | ||
} | ||
this.env = (_c = (_b = this.config) === null || _b === void 0 ? void 0 : _b.parsed) !== null && _c !== void 0 ? _c : {}; | ||
} | ||
@@ -123,2 +133,5 @@ this.plain = fs_1.default.readFileSync(file, { encoding: this.encoding, flag: "r" }); | ||
} | ||
/** | ||
* Loads `.env` file contents. | ||
*/ | ||
loadFile() { | ||
@@ -128,2 +141,6 @@ this.load(false); | ||
} | ||
/** | ||
* Find first `.env` file walking up from cwd directory based on priority criteria. | ||
* @return file matched with higher priority | ||
*/ | ||
find() { | ||
@@ -167,2 +184,6 @@ let dotenv; | ||
} | ||
/** | ||
* Save `.env` file contents. | ||
* @param changes - data to change on the dotenv | ||
*/ | ||
save(changes) { | ||
@@ -172,4 +193,2 @@ if (!this.plain || !this.path) | ||
// https://github.com/stevenvachon/edit-dotenv | ||
// Steven Vachon | ||
// @stevenvachon | ||
const EOL = "\r\n"; | ||
@@ -185,9 +204,10 @@ const breakPattern = /\n/g; | ||
let hasAppended = false; | ||
const data = Object.keys(changes).reduce((result, varname) => { | ||
const value = changes[varname] | ||
const data = Object.keys(changes).reduce((result, variable) => { | ||
const value = changes[variable] | ||
.replace(breakPattern, breakReplacement) | ||
.replace(returnPattern, returnReplacement) | ||
.trim(); | ||
const safeName = this.escapeRegExp(varname); | ||
const varPattern = new RegExp(`^(${h}*${safeName}${h}*=${h}*).*?(${h}*)$`, flags); // fixed regex | ||
const safeName = this.escapeRegExp(variable); | ||
// Match all between equal and eol | ||
const varPattern = new RegExp(`^(${h}*${safeName}${h}*=${h}*).*?(${h}*)$`, flags); | ||
if (varPattern.test(result)) { | ||
@@ -199,3 +219,3 @@ const safeValue = value.replace(groupPattern, groupReplacement); | ||
hasAppended = true; | ||
return `${varname}=${value}${EOL}`; | ||
return `${variable}=${value}${EOL}`; | ||
} | ||
@@ -205,7 +225,7 @@ else if (!result.endsWith(EOL) && !hasAppended) { | ||
// Add an extra break between previously defined and newly appended variable | ||
return `${result}${EOL}${EOL}${varname}=${value}`; | ||
return `${result}${EOL}${EOL}${variable}=${value}`; | ||
} | ||
else if (!result.endsWith(EOL)) { | ||
// Add break for appended variable | ||
return `${result}${EOL}${varname}=${value}`; | ||
return `${result}${EOL}${variable}=${value}`; | ||
} | ||
@@ -215,7 +235,7 @@ else if (result.endsWith(EOL) && !hasAppended) { | ||
// Add an extra break between previously defined and newly appended variable | ||
return `${result}${EOL}${varname}=${value}${EOL}`; | ||
return `${result}${EOL}${variable}=${value}${EOL}`; | ||
} | ||
else { | ||
// Add break for appended variable | ||
return `${result}${varname}=${value}${EOL}`; | ||
return `${result}${variable}=${value}${EOL}`; | ||
} | ||
@@ -228,2 +248,7 @@ }, this.plain); | ||
} | ||
/** | ||
* Escape regex | ||
* @param string - string to escape | ||
* @return escaped string | ||
*/ | ||
escapeRegExp(string) { | ||
@@ -234,2 +259,7 @@ return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d"); | ||
exports.Dotenv = Dotenv; | ||
/** | ||
* Load dotenv on process and return instance of Dotenv | ||
* @param DotenvArgs props | ||
* @return Dotenv | ||
*/ | ||
function dotenvLoad() { | ||
@@ -240,2 +270,20 @@ const dotenv = new Dotenv(...arguments); | ||
exports.dotenvLoad = dotenvLoad; | ||
/** | ||
* @see dotenvLoad | ||
*/ | ||
exports.load = dotenvLoad; | ||
/** | ||
* Load dotenv on process and return the dotenv output | ||
* @param DotenvArgs props | ||
* @return Dotenv | ||
*/ | ||
function dotenvConfig() { | ||
const dotenv = new Dotenv(...arguments); | ||
return dotenv.load().config; | ||
} | ||
exports.dotenvConfig = dotenvConfig; | ||
/** | ||
* @see dotenvConfig | ||
*/ | ||
exports.config = dotenvConfig; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "dotenv-mono", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"main": "./dist/index.js", | ||
@@ -5,0 +5,0 @@ "module": "./dist/index.js", |
@@ -10,3 +10,3 @@ <div align="center"> | ||
[![NPM version](http://img.shields.io/npm/v/dotenv-mono.svg?style=for-the-badge)](http://npmjs.org/package/dotenv-mono) | ||
[![js-prittier-style](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=for-the-badge)](https://prettier.io/) | ||
[![js-prettier-style](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=for-the-badge)](https://prettier.io/) | ||
@@ -21,6 +21,9 @@ <!--[![Package Quality](https://npm.packagequality.com/shield/dotenv-mono.svg?style=for-the-badge)](https://packagequality.com/#?package=dotenv-mono)--> | ||
This is a package that permit to load a dotenv even from a children applications or packages of a monorepo. | ||
To prevent code duplication and enhance re-usability, a centralized configuration including all of your environment variables might be handy. | ||
Rather of generating a `.env` file for each package, we may utilize a single `.env` file at the project's root. | ||
This is a package that allows monorepo applications and packages to share and load a centralized dotenv. | ||
It's based over [dotenv](https://github.com/motdotla/dotenv) package. | ||
It contains also some additionals features like manipulations and save of the changes on the dotenv file. | ||
It also includes some extra features such as manipulation and saving of changes to the dotenv file. | ||
@@ -43,15 +46,18 @@ The plugin [dotenv-expand](https://www.npmjs.com/package/dotenv-expand) is enabled by default. | ||
The package search the first `.env` file, matching with some priority criteria, by walking up the parent directories. | ||
##### Priorities | ||
This package find up, starting from the current process directory, the first file, that match the best specific | ||
filename criteria with the higher priority. The depth of the folder, starting from the current process directory, | ||
overwrite the files upper, having a higher priority. | ||
Starting from the current process directory, this package finds the first file that matches the best particular filename criteria with the highest priority. | ||
The greater the depth of the up folder, the lesser its priority. | ||
| Priority | File name | | ||
| -------- | ---------------------------------------- | | ||
| 75 | .env.{development,production,test}.local | | ||
| 50 | .env.local | | ||
| 25 | .env.{development,production,test} | | ||
| 1 | .env | | ||
> Note: The allowed values for `NODE_ENV` are usually `test`, `development` and `production`. | ||
| Priority | File name | | ||
| -------- | ------------------------ | | ||
| 75 | `.env.$(NODE_ENV).local` | | ||
| 50 | `.env.local` | | ||
| 25 | `.env.$(NODE_ENV)` | | ||
| 1 | `.env` | | ||
###### Example | ||
@@ -106,8 +112,21 @@ | ||
#### Standard | ||
```js | ||
require("dotenv-mono").load(); | ||
// or | ||
const {dotenvLoad} = require("dotenv-mono"); | ||
const dotenv = dotenvLoad(); | ||
dotenvLoad(); | ||
// Same as | ||
// or | ||
import {dotenvLoad} from "dotenv-mono"; | ||
dotenvLoad(); | ||
``` | ||
#### Using the class | ||
```js | ||
const {Dotenv} = require("dotenv-mono"); | ||
@@ -118,2 +137,10 @@ const dotenv = new Dotenv(); | ||
#### Having the output | ||
If you want to have back directly the output like [dotenv](https://github.com/motdotla/dotenv) package. | ||
```js | ||
require("dotenv-mono").config(); | ||
``` | ||
### Load file with extension | ||
@@ -168,11 +195,11 @@ | ||
| ------------ | --------------------------------------------------------------------------------------------------------------- | ----------------------------- | | ||
| `cwd` | Specify the current working directory | `process.cwd()` | | ||
| `debug` | Turn on/off logging to help debug why certain keys or values are not being set as you expect | `false` | | ||
| `depth` | Specify the max depth to reach finding up the folder from the children directory | `4` | | ||
| `encoding` | Specify the encoding of your file containing environment variables | `utf8` | | ||
| `expand` | Turn on/off the [`dotenv-expand`](https://www.npmjs.com/package/dotenv-expand) plugin | `true` | | ||
| `priorities` | Specify the criteria of the filename priority to load as dotenv file | See [Priorities](#priorities) | | ||
| `depth` | Specify the max depth to reach finding up the folder from the children directory | `4` | | ||
| `cwd` | Specify the current working directory | `process.cwd()` | | ||
| `path` | Specify a custom path if your file containing environment variables is located elsewhere | | | ||
| `extension` | Specify to load specific dotenv file used only on specific apps/packages (ex. `.env.server...`) | | | ||
| `encoding` | Specify the encoding of your file containing environment variables | `utf8` | | ||
| `debug` | Turn on/off logging to help debug why certain keys or values are not being set as you expect | `false` | | ||
| `override` | Override any environment variables that have already been set on your machine with values from your `.env` file | `false` | | ||
| `path` | Specify a custom path if your file containing environment variables is located elsewhere | | | ||
| `priorities` | Specify the criteria of the filename priority to load as dotenv file | See [Priorities](#priorities) | | ||
@@ -179,0 +206,0 @@ ### Methods |
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
34278
425
246