Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override #595

Merged
merged 5 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add documentation to README
  • Loading branch information
motdotla committed Jan 17, 2022
commit 59da00212283f5c0ca1a50c241f8367472c17881
26 changes: 18 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ See [examples](https://github.com/dotenv-org/examples) of using dotenv with vari

Dotenv exposes two functions:

* `dotenv.config`
* `dotenv.parse`
* `config`
* `parse`

### `dotenv.config`
### Config

`config` will read your `.env` file, parse the contents, assign it to
[`process.env`](https://nodejs.org/docs/latest/api/process.html#process_process_env),
Expand All @@ -124,7 +124,7 @@ You can additionally, pass options to `config`.

Default: `path.resolve(process.cwd(), '.env')`

You may 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.

```js
require('dotenv').config({ path: '/custom/path/to/.env' })
Expand All @@ -134,7 +134,7 @@ require('dotenv').config({ path: '/custom/path/to/.env' })

Default: `utf8`

You may specify the encoding of your file containing environment variables.
Specify the encoding of your file containing environment variables.

```js
require('dotenv').config({ encoding: 'latin1' })
Expand All @@ -144,13 +144,23 @@ require('dotenv').config({ encoding: 'latin1' })

Default: `false`

You may turn on logging to help debug why certain keys or values are not being set as you expect.
Turn on logging to help debug why certain keys or values are not being set as you expect.

```js
require('dotenv').config({ debug: process.env.DEBUG })
```

### `dotenv.parse`
##### Override

Default: `false`

Override any environment variables that have already been set on your machine with values from your .env file.

```js
require('dotenv').config({ override: true })
```

### Parse

The engine which parses the contents of your file containing environment
variables is available to use. It accepts a String or Buffer and will return
Expand All @@ -169,7 +179,7 @@ console.log(typeof config, config) // object { BASIC : 'basic' }

Default: `false`

You may turn on logging to help debug why certain keys or values are not being set as you expect.
Turn on logging to help debug why certain keys or values are not being set as you expect.

```js
const dotenv = require('dotenv')
Expand Down
33 changes: 22 additions & 11 deletions lib/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@

export interface DotenvParseOptions {
/**
* Default: `false`
*
* Turn on logging to help debug why certain keys or values are not being set as you expect.
* Defaults to false.
*
* example: `dotenv.parse('KEY=value', { debug: true })`
*/
debug?: boolean;

/**
* Override environment variables that have already been set on your machine with values from your .env file.
* Defaults to false.
*/
override?: boolean;
}

export interface DotenvParseOutput {
Expand All @@ -25,7 +22,7 @@ export interface DotenvParseOutput {
* See https://docs.dotenv.org
*
* @param src - contents to be parsed. example: `'DB_HOST=localhost'`
* @param options - additional options. example: `{ debug: true, override: false }`
* @param options - additional options. example: `{ debug: true }`
* @returns an object with keys and values based on `src`. example: `{ DB_HOST : 'localhost' }`
*/
export function parse<T extends DotenvParseOutput = DotenvParseOutput>(
Expand All @@ -35,24 +32,38 @@ export function parse<T extends DotenvParseOutput = DotenvParseOutput>(

export interface DotenvConfigOptions {
/**
* Default: `path.resolve(process.cwd(), '.env')`
*
* Specify a custom path if your file containing environment variables is located elsewhere.
*
* example: `require('dotenv').config({ path: '/custom/path/to/.env' })`
*/
path?: string;

/**
* Default: `utf8`
*
* Specify the encoding of your file containing environment variables.
*
* example: `require('dotenv').config({ encoding: 'latin1' })`
*/
encoding?: string;

/**
* Default: `false`
*
* Turn on logging to help debug why certain keys or values are not being set as you expect.
* Defaults to false.
*
* example: `require('dotenv').config({ debug: process.env.DEBUG })`
*/
debug?: boolean;

/**
* Override environment variables that have already been set on your machine with values from your .env file.
* Defaults to false.
* Default: `false`
*
* Override any environment variables that have already been set on your machine with values from your .env file.
*
* example: `require('dotenv').config({ override: true })`
*/
override?: boolean;
}
Expand Down
1 change: 0 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const NEWLINES_MATCH = /\r\n|\n|\r/
// Parses src into an Object
function parse (src, options) {
const debug = Boolean(options && options.debug)
const override = Boolean(options && options.override)
const obj = {}

// convert Buffers before splitting into lines and processing
Expand Down