dotenv-mono
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -5,2 +5,9 @@ # Changelog | ||
## [0.2.0](https://github.com/marcocesarato/dotenv-mono/compare/v0.1.2...v0.2.0) (2022-11-11) | ||
### Features | ||
- add encoding, override and debug config ([4707aac](https://github.com/marcocesarato/dotenv-mono/commit/4707aac17131743a3577e973f73d3e247b76c276)) | ||
- add parse method ([6e9800d](https://github.com/marcocesarato/dotenv-mono/commit/6e9800d0964044e700cfa204e8d8f7f6fa2749b3)) | ||
### [0.1.2](https://github.com/marcocesarato/dotenv-mono/compare/v0.1.1...v0.1.2) (2022-11-11) | ||
@@ -7,0 +14,0 @@ |
@@ -10,2 +10,3 @@ export type Data = Record<string, any>; | ||
}; | ||
export class DotEnv { | ||
@@ -17,13 +18,31 @@ public path: string | null | undefined; | ||
public depth: number | null | undefined; | ||
public expand: string | null | undefined; | ||
public expand: boolean | null | undefined; | ||
public env: Data | null | undefined; | ||
public envString: string | null | undefined; | ||
public plain: string | null | undefined; | ||
public encoding: string | null | undefined; | ||
public debug: boolean | null | undefined; | ||
public override: boolean | null | undefined; | ||
private _cwd: string | null | undefined; | ||
private _priorities?: {[key: string]: number} | null | undefined; | ||
private _depth: number | null | undefined; | ||
private _expand: boolean | null | undefined; | ||
private _encoding: string | null | undefined; | ||
private _debug: boolean | null | undefined; | ||
private _override: boolean | null | undefined; | ||
constructor(args?: DontEnvArgs); | ||
public load(loadOnProcess: boolean): DotEnv; | ||
public get(): Data; | ||
public find(): string | null; | ||
public save(changes: Data): DotEnv; | ||
public setPriorities(priorities?: {[key: string]: number}): DotEnv; | ||
public parse<T extends Data = Data>(src: string | Buffer): T; | ||
private escapeRegExp(value: string): string; | ||
} | ||
export function dotenvLoad(ext?: string, path?: string): DotEnv; |
96
index.js
@@ -8,16 +8,75 @@ const fs = require("fs"); | ||
constructor(props = {}) { | ||
this.env = {}; | ||
this.path = props.path; | ||
this.expand = props.expand || false; | ||
this.cwd = props.cwd; | ||
this.extension = props.extension; | ||
this.cwd = props.cwd; | ||
this.depth = props.depth || 4; | ||
this.setPriorities(props.priorities); | ||
this.expand = props.expand; | ||
this.depth = props.depth; | ||
this.priorities = props.priorities; | ||
this.encoding = props.encoding; | ||
this.debug = props.debug; | ||
this.override = props.override; | ||
} | ||
setPriorities(priorities = {}) { | ||
get debug() { | ||
if (this._debug == null) return false; | ||
return this._debug; | ||
} | ||
set debug(value) { | ||
this._debug = value; | ||
} | ||
get encoding() { | ||
if (this._encoding == null) return "utf8"; | ||
return this._encoding; | ||
} | ||
set encoding(value) { | ||
this._encoding = value; | ||
} | ||
get expand() { | ||
if (this._expand == null) return true; | ||
return this._expand; | ||
} | ||
set expand(value) { | ||
this._expand = value; | ||
} | ||
get cwd() { | ||
if (this._cwd == null) return process.cwd() || ""; | ||
return this._cwd; | ||
} | ||
set cwd(value) { | ||
this._cwd = value; | ||
} | ||
get depth() { | ||
if (this._depth == null) return 4; | ||
return this._depth; | ||
} | ||
set depth(value) { | ||
this._depth = value; | ||
} | ||
get override() { | ||
if (this._override == null) return false; | ||
return this._override; | ||
} | ||
set override(value) { | ||
this._override = value; | ||
} | ||
get priorities() { | ||
return this._priorities; | ||
} | ||
set priorities(value) { | ||
this.nodeEnv = process.env.NODE_ENV || "development"; | ||
const ext = this.extension ? `.${this.extension}` : ""; | ||
this.priorities = Object.assign( | ||
this._priorities = Object.assign( | ||
{ | ||
@@ -29,6 +88,8 @@ [`.env${ext}.${this.nodeEnv}.local`]: 75, | ||
}, | ||
priorities || {}, | ||
value || {}, | ||
); | ||
} | ||
return this; | ||
parse() { | ||
return dotenv.parse.apply(this, arguments); | ||
} | ||
@@ -44,2 +105,5 @@ | ||
path: this.path, | ||
debug: this.debug, | ||
encoding: this.encoding, | ||
override: this.override, | ||
}); | ||
@@ -52,3 +116,3 @@ if (this.expand) { | ||
} | ||
this.envString = fs.readFileSync(this.path, {encoding: "utf8", flag: "r"}); | ||
this.plain = fs.readFileSync(this.path, {encoding: this.encoding, flag: "r"}); | ||
} | ||
@@ -65,3 +129,3 @@ return this; | ||
let dotenv = null; | ||
let directory = path.resolve(this.cwd || process.cwd() || ""); | ||
let directory = path.resolve(this.cwd); | ||
const {root} = path.parse(directory); | ||
@@ -101,3 +165,3 @@ const matcher = (cwd) => { | ||
save(changes) { | ||
if (!this.envString) return; | ||
if (!this.plain) return; | ||
@@ -125,3 +189,3 @@ // https://github.com/stevenvachon/edit-dotenv | ||
const safeName = this.escapeRegExp(varname); | ||
const varPattern = new RegExp(`^(${h}*${safeName}${h}*=${h}*).*?(${h}*)$`, flags); | ||
const varPattern = new RegExp(`^(${h}*${safeName}${h}*=${h}*).*?(${h}*)$`, flags); // fixed regex | ||
if (varPattern.test(result)) { | ||
@@ -148,5 +212,5 @@ const safeValue = value.replace(groupPattern, groupReplacement); | ||
} | ||
}, this.envString); | ||
}, this.plain); | ||
fs.writeFileSync(this.path, data, { | ||
encoding: "utf8", | ||
encoding: this.encoding, | ||
}); | ||
@@ -153,0 +217,0 @@ return this; |
{ | ||
"name": "dotenv-mono", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"main": "index.js", | ||
@@ -24,2 +24,3 @@ "module": "index.js", | ||
"devDependencies": { | ||
"@types/node": "^18.11.9", | ||
"eslint": "8.27.0", | ||
@@ -26,0 +27,0 @@ "eslint-config-prettier": "^8.5.0", |
@@ -21,5 +21,6 @@ <div align="center"> | ||
This is a package that permit to load a dotenv even from a children applications or packages of a monorepo. | ||
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. | ||
The package [`dotenv-expand`](https://www.npmjs.com/package/dotenv-expand) is enabled by default. | ||
The plugin [dotenv-expand](https://www.npmjs.com/package/dotenv-expand) is enabled by default. | ||
@@ -68,3 +69,4 @@ ##### Example | ||
They can be customized on the constructor `priorities` property, see the example below on the [usage](#change-priorities) section. | ||
They can be customized on the constructor `priorities` property, see the example below on | ||
the [usage](#change-priorities) section. | ||
@@ -167,11 +169,48 @@ ## 📖 Install | ||
| Setting | Description | Default | | ||
| ------------ | -------------------------------------------------------------------------------------------- | ----------------------------- | | ||
| `expand` | Enable or disable [`dotenv-expand`](https://www.npmjs.com/package/dotenv-expand) plugin | `true` | | ||
| `priorities` | Set the criteria of the filename priority to load as dotenv file | See [Priorities](#priorities) | | ||
| `depth` | Set max depth of folder to search up from the children directory | `4` | | ||
| `cwd` | Set the current working directory | | | ||
| `path` | Set the file to load | | | ||
| `extension` | Used to load specific dotenv file used only on specific apps/packages (ex. `.env.server...`) | | | ||
| Setting | Description | Default | | ||
| ------------ | --------------------------------------------------------------------------------------------------------------- | ----------------------------- | | ||
| `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` | | ||
### Methods | ||
#### Load | ||
It will read your `.env` file following the criteria, parse the contents, assign it to `process.env`. | ||
```ts | ||
public load(loadOnProcess: boolean): DotEnv; | ||
``` | ||
#### LoadFile | ||
It will read your `.env` file following the criteria, parse the contents, ready to be read or changed programmatically. | ||
```ts | ||
public loadFile(): DotEnv; | ||
``` | ||
#### Save | ||
Merge the data on input with the loaded data from `load` or `loadFile`, and save the changes on the original dotenv file. | ||
```ts | ||
public save(changes: Record<string, any>): DotEnv; | ||
``` | ||
#### Parse | ||
See the [dotenv](https://github.com/motdotla/dotenv) documentation [HERE](https://github.com/motdotla/dotenv#parse) | ||
```ts | ||
public parse<T extends Record<string, any> = Record<string, any>>(src: string | Buffer): T; | ||
``` | ||
## 🤔 How to contribute | ||
@@ -178,0 +217,0 @@ |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
23932
252
225
9