@dotenvx/dotenvx
Advanced tools
Comparing version 1.24.5 to 1.25.0
@@ -5,4 +5,20 @@ # Changelog | ||
[Unreleased](https://github.com/dotenvx/dotenvx/compare/v1.24.5...main) | ||
[Unreleased](https://github.com/dotenvx/dotenvx/compare/v1.25.0...main) | ||
## [1.25.0](https://github.com/dotenvx/dotenvx/compare/v1.24.5...v1.25.0) | ||
### Added | ||
* add `run --strict` flag to exit with code `1` if any errors are encountered - like a missing `.env` file or decryption failure ([#460](https://github.com/dotenvx/dotenvx/pull/460)) | ||
* add `get --strict` flag to exit with code `1` if any errors are encountered - like a missing `.env` file or decryption failure ([#461](https://github.com/dotenvx/dotenvx/pull/461)) | ||
* add `strict` option to `config()` to throw for any errors ([#459](https://github.com/dotenvx/dotenvx/pull/459)) | ||
### Changed | ||
* log `MISSING_ENV_FILE` and `DECRYPTION_FAILED` errors to stderr (prior was stdout as a warning) ([#459](https://github.com/dotenvx/dotenvx/pull/459)) | ||
### Removed | ||
* remove `dotenvx.get()` function from `lib/main.js`. (`parse` already historically exists for this purpose) ([#461](https://github.com/dotenvx/dotenvx/pull/461)) | ||
## [1.24.5](https://github.com/dotenvx/dotenvx/compare/v1.24.4...v1.24.5) | ||
@@ -9,0 +25,0 @@ |
{ | ||
"version": "1.24.5", | ||
"version": "1.25.0", | ||
"name": "@dotenvx/dotenvx", | ||
@@ -4,0 +4,0 @@ "description": "a better dotenv–from the creator of `dotenv`", |
129
README.md
@@ -972,2 +972,17 @@ [![dotenvx](https://dotenvx.com/better-banner.png)](https://dotenvx.com) | ||
</details> | ||
* <details><summary>`run --strict`</summary><br> | ||
Exit with code `1` if any errors are encountered - like a missing .env file or decryption failure. | ||
```sh | ||
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js | ||
$ dotenvx run -f .env.missing --strict -- node index.js | ||
[MISSING_ENV_FILE] missing .env.missing file (/path/to/.env.missing) | ||
[MISSING_ENV_FILE] ? add one with [echo "HELLO=World" > .env.missing] | ||
``` | ||
This can be useful in `ci` scripts where you want to fail the ci if your `.env` file could not be decrypted at runtime. | ||
</details> | ||
* <details><summary>`run --convention=nextjs`</summary><br> | ||
@@ -1041,2 +1056,12 @@ | ||
</details> | ||
* <details><summary>`get KEY --strict`</summary><br> | ||
Exit with code `1` if any errors are encountered - like a missing key, missing .env file, or decryption failure. | ||
```sh | ||
$ dotenvx get DOES_NOT_EXIST --strict | ||
[MISSING_KEY] missing DOES_NOT_EXIST key | ||
``` | ||
</details> | ||
* <details><summary>`get KEY --convention=nextjs`</summary><br> | ||
@@ -1612,2 +1637,106 @@ | ||
### config() 📦 | ||
* <details><summary>`config()`</summary><br> | ||
Use directly in node.js code. | ||
```ini | ||
# .env | ||
HELLO="World" | ||
``` | ||
```js | ||
// index.js | ||
require('@dotenvx/dotenvx').config() | ||
console.log(`Hello ${process.env.HELLO}`) | ||
``` | ||
```sh | ||
$ node index.js | ||
[dotenvx@1.24.5] injecting env (1) from .env | ||
Hello World | ||
``` | ||
</details> | ||
* <details><summary>`config(path: ['.env.local', '.env'])` - multiple files</summary><br> | ||
Specify path(s) to multiple .env files. | ||
```ini | ||
# .env.local | ||
HELLO="Me" | ||
``` | ||
```ini | ||
# .env | ||
HELLO="World" | ||
``` | ||
```js | ||
// index.js | ||
require('@dotenvx/dotenvx').config({path: ['.env.local', '.env']}) | ||
console.log(`Hello ${process.env.HELLO}`) | ||
``` | ||
```sh | ||
$ node index.js | ||
[dotenvx@1.24.5] injecting env (1) from .env.local, .env | ||
Hello Me | ||
``` | ||
</details> | ||
* <details><summary>`config(overload: true)` - overload</summary><br> | ||
User `overload` to overwrite the prior set value. | ||
```ini | ||
# .env.local | ||
HELLO="Me" | ||
``` | ||
```ini | ||
# .env | ||
HELLO="World" | ||
``` | ||
```js | ||
// index.js | ||
require('@dotenvx/dotenvx').config({path: ['.env.local', '.env'], overload: true}) | ||
console.log(`Hello ${process.env.HELLO}`) | ||
``` | ||
```sh | ||
$ node index.js | ||
[dotenvx@1.24.5] injecting env (1) from .env.local, .env | ||
Hello World | ||
``` | ||
</details> | ||
* <details><summary>`config(strict: true)` - strict</summary><br> | ||
Use `strict` to throw if an error is encountered - like a missing .env file. | ||
```ini | ||
# .env | ||
HELLO="World" | ||
``` | ||
```js | ||
// index.js | ||
require('@dotenvx/dotenvx').config({path: ['.env.missing', '.env'], strict: true}) | ||
console.log(`Hello ${process.env.HELLO}`) | ||
``` | ||
```sh | ||
$ node index.js | ||
Error: [MISSING_ENV_FILE] missing .env.missing file (/path/to/.env.missing) | ||
``` | ||
</details> | ||
### Extensions 🔌 | ||
@@ -1614,0 +1743,0 @@ |
@@ -51,6 +51,6 @@ const fsx = require('./../../lib/helpers/fsx') | ||
if (processedEnv.error.code === 'MISSING_ENV_FILE') { | ||
logger.error(processedEnv.error.message) | ||
console.error(processedEnv.error.message) | ||
logger.help(`? add one with [echo "HELLO=World" > ${processedEnv.envFilepath}] and re-run [dotenvx decrypt]`) | ||
} else { | ||
logger.error(processedEnv.error.message) | ||
console.error(processedEnv.error.message) | ||
} | ||
@@ -57,0 +57,0 @@ } else if (processedEnv.changed) { |
@@ -29,3 +29,3 @@ const fsx = require('./../../../lib/helpers/fsx') | ||
} catch (error) { | ||
logger.error(error.message) | ||
console.error(error.message) | ||
if (error.help) { | ||
@@ -32,0 +32,0 @@ logger.help(error.help) |
@@ -13,3 +13,3 @@ const childProcess = require('child_process') | ||
} catch (error) { | ||
logger.error('gitleaks: command not found') | ||
console.error('gitleaks: command not found') | ||
logger.help('? install gitleaks: [brew install gitleaks]') | ||
@@ -25,3 +25,3 @@ logger.help2('? other install options: [https://github.com/gitleaks/gitleaks]') | ||
} catch (error) { | ||
logger.error(error.message) | ||
console.error(error.message) | ||
@@ -28,0 +28,0 @@ process.exit(1) |
@@ -6,3 +6,3 @@ const { logger } = require('./../../shared/logger') | ||
const main = require('./../../lib/main') | ||
const Get = require('./../../lib/services/get') | ||
@@ -25,36 +25,53 @@ function get (key) { | ||
const results = main.get(key, envs, options.overload, process.env.DOTENV_KEY, options.all) | ||
try { | ||
const { parsed, errors } = new Get(key, envs, options.overload, process.env.DOTENV_KEY, options.all).run() | ||
if (typeof results === 'object' && results !== null) { | ||
if (options.format === 'eval') { | ||
let inline = '' | ||
for (const [key, value] of Object.entries(results)) { | ||
inline += `${key}=${escape(value)}\n` | ||
for (const error of errors || []) { | ||
if (options.strict) throw error // throw immediately if strict | ||
console.error(error.message) | ||
if (error.help) { | ||
console.error(error.help) | ||
} | ||
inline = inline.trim() | ||
} | ||
console.log(inline) | ||
} else if (options.format === 'shell') { | ||
let inline = '' | ||
for (const [key, value] of Object.entries(results)) { | ||
inline += `${key}=${value} ` | ||
if (key) { | ||
const single = parsed[key] | ||
if (single === undefined) { | ||
console.log('') | ||
} else { | ||
console.log(single) | ||
} | ||
inline = inline.trim() | ||
} else { | ||
if (options.format === 'eval') { | ||
let inline = '' | ||
for (const [key, value] of Object.entries(parsed)) { | ||
inline += `${key}=${escape(value)}\n` | ||
} | ||
inline = inline.trim() | ||
console.log(inline) | ||
} else { | ||
let space = 0 | ||
if (options.prettyPrint) { | ||
space = 2 | ||
console.log(inline) | ||
} else if (options.format === 'shell') { | ||
let inline = '' | ||
for (const [key, value] of Object.entries(parsed)) { | ||
inline += `${key}=${value} ` | ||
} | ||
inline = inline.trim() | ||
console.log(inline) | ||
} else { | ||
let space = 0 | ||
if (options.prettyPrint) { | ||
space = 2 | ||
} | ||
console.log(JSON.stringify(parsed, null, space)) | ||
} | ||
console.log(JSON.stringify(results, null, space)) | ||
} | ||
} else { | ||
if (results === undefined) { | ||
console.log('') | ||
process.exit(1) | ||
} else { | ||
console.log(results) | ||
} catch (error) { | ||
console.error(error.message) | ||
if (error.help) { | ||
console.error(error.help) | ||
} | ||
process.exit(1) | ||
} | ||
@@ -61,0 +78,0 @@ } |
@@ -8,2 +8,3 @@ const path = require('path') | ||
const conventions = require('./../../lib/helpers/conventions') | ||
const DeprecationNotice = require('./../../lib/helpers/deprecationNotice') | ||
@@ -39,7 +40,3 @@ async function run () { | ||
if (process.env.DOTENV_KEY) { | ||
logger.warn('DEPRECATION NOTICE: Setting DOTENV_KEY with .env.vault is deprecated.') | ||
logger.warn('DEPRECATION NOTICE: Run [dotenvx ext vault migrate] for instructions on converting your .env.vault file to encrypted .env files (using public key encryption algorithm secp256k1)') | ||
logger.warn('DEPRECATION NOTICE: Read more at [https://github.com/dotenvx/dotenvx/blob/main/CHANGELOG.md#0380]') | ||
} | ||
new DeprecationNotice().dotenvKey() // DEPRECATION NOTICE | ||
@@ -67,39 +64,33 @@ const { | ||
if (processedEnv.error) { | ||
if (processedEnv.error.code === 'MISSING_ENV_FILE') { | ||
// do not warn for conventions (too noisy) | ||
if (!options.convention) { | ||
logger.warnv(processedEnv.error.message) | ||
logger.help(`? add one with [echo "HELLO=World" > ${processedEnv.filepath}] and re-run [dotenvx run -- ${commandArgs.join(' ')}]`) | ||
for (const error of processedEnv.errors || []) { | ||
if (options.strict) throw error // throw immediately if strict | ||
if (error.code === 'MISSING_ENV_FILE') { | ||
if (!options.convention) { // do not output error for conventions (too noisy) | ||
console.error(error.message) | ||
if (error.help) { | ||
console.error(`${error.help} and re-run [dotenvx run -- ${commandArgs.join(' ')}]`) | ||
} | ||
} | ||
} else { | ||
logger.warnv(processedEnv.error.message) | ||
} | ||
} else { | ||
if (processedEnv.warnings) { | ||
for (const warning of processedEnv.warnings) { | ||
logger.warn(warning.message) | ||
if (warning.help) { | ||
logger.help(warning.help) | ||
} | ||
console.error(error.message) | ||
if (error.help) { | ||
console.error(error.help) | ||
} | ||
} | ||
} | ||
// debug parsed | ||
const parsed = processedEnv.parsed | ||
logger.debug(parsed) | ||
// debug parsed | ||
logger.debug(processedEnv.parsed) | ||
// verbose/debug injected key/value | ||
const injected = processedEnv.injected | ||
for (const [key, value] of Object.entries(injected)) { | ||
logger.verbose(`${key} set`) | ||
logger.debug(`${key} set to ${value}`) | ||
} | ||
// verbose/debug injected key/value | ||
for (const [key, value] of Object.entries(processedEnv.injected || {})) { | ||
logger.verbose(`${key} set`) | ||
logger.debug(`${key} set to ${value}`) | ||
} | ||
// verbose/debug preExisted key/value | ||
const preExisted = processedEnv.preExisted | ||
for (const [key, value] of Object.entries(preExisted)) { | ||
logger.verbose(`${key} pre-exists (protip: use --overload to override)`) | ||
logger.debug(`${key} pre-exists as ${value} (protip: use --overload to override)`) | ||
} | ||
// verbose/debug preExisted key/value | ||
for (const [key, value] of Object.entries(processedEnv.preExisted || {})) { | ||
logger.verbose(`${key} pre-exists (protip: use --overload to override)`) | ||
logger.debug(`${key} pre-exists as ${value} (protip: use --overload to override)`) | ||
} | ||
@@ -119,6 +110,7 @@ } | ||
} catch (error) { | ||
logger.error(error.message) | ||
console.error(error.message) | ||
if (error.help) { | ||
logger.help(error.help) | ||
console.error(error.help) | ||
} | ||
process.exit(1) | ||
} | ||
@@ -125,0 +117,0 @@ |
@@ -61,2 +61,3 @@ #!/usr/bin/env node | ||
.option('-o, --overload', 'override existing env variables') | ||
.option('--strict', 'process.exit(1) on any errors (default: false)', false) | ||
.option('--convention <name>', 'load a .env convention (available conventions: [\'nextjs\'])') | ||
@@ -78,2 +79,3 @@ .action(function (...args) { | ||
.option('-o, --overload', 'override existing env variables') | ||
.option('--strict', 'process.exit(1) on any errors (default: false)', false) | ||
.option('--convention <name>', 'load a .env convention (available conventions: [\'nextjs\'])') | ||
@@ -80,0 +82,0 @@ .option('-a, --all', 'include all machine envs as well') |
const { logger } = require('./../../shared/logger') | ||
function catchAndLog (error) { | ||
logger.error(error.message) | ||
console.error(error.message) | ||
if (error.help) { | ||
@@ -6,0 +6,0 @@ logger.help(error.help) |
@@ -19,3 +19,3 @@ const chomp = require('./chomp') | ||
this.injected = {} | ||
this.warnings = [] | ||
this.errors = [] | ||
@@ -46,3 +46,3 @@ // for use with progressive expansion | ||
} catch (e) { | ||
this.warnings.push(this.warning(e, key)) | ||
this.errors.push(this.error(e, key)) | ||
} | ||
@@ -83,4 +83,4 @@ | ||
injected: this.injected, | ||
warnings: this.warnings, | ||
preExisted: this.preExisted | ||
preExisted: this.preExisted, | ||
errors: this.errors | ||
} | ||
@@ -214,8 +214,8 @@ } | ||
warning (e, key) { | ||
const warning = new Error(`[${e.code}] could not decrypt ${key} using private key '${truncate(this.privateKey)}'`) | ||
warning.code = e.code | ||
warning.help = `[${e.code}] ? ${e.message}` | ||
error (e, key) { | ||
const error = new Error(`[${e.code}] could not decrypt ${key} using private key '${truncate(this.privateKey)}'`) | ||
error.code = e.code | ||
error.help = `[${e.code}] ? ${e.message}` | ||
return warning | ||
return error | ||
} | ||
@@ -222,0 +222,0 @@ } |
@@ -10,3 +10,2 @@ // @ts-check | ||
const Ls = require('./services/ls') | ||
const Get = require('./services/get') | ||
const Run = require('./services/run') | ||
@@ -20,2 +19,3 @@ const Keypair = require('./services/keypair') | ||
const Parse = require('./helpers/parse') | ||
const DeprecationNotice = require('./helpers/deprecationNotice') | ||
@@ -33,3 +33,6 @@ /** @type {import('./main').config} */ | ||
// DOTENV_KEY | ||
// strict | ||
const strict = options.strict | ||
// DOTENV_KEY (DEPRECATED) | ||
let DOTENV_KEY = process.env.DOTENV_KEY | ||
@@ -52,7 +55,3 @@ if (options && options.DOTENV_KEY) { | ||
if (process.env.DOTENV_KEY) { | ||
logger.warn('DEPRECATION NOTICE: Setting DOTENV_KEY with .env.vault is deprecated.') | ||
logger.warn('DEPRECATION NOTICE: Run [dotenvx ext vault migrate] for instructions on converting your .env.vault file to encrypted .env files (using public key encryption algorithm secp256k1)') | ||
logger.warn('DEPRECATION NOTICE: Read more at [https://github.com/dotenvx/dotenvx/blob/main/CHANGELOG.md#0380]') | ||
} | ||
new DeprecationNotice({ DOTENV_KEY }).dotenvKey() // DEPRECATION NOTICE | ||
@@ -71,8 +70,7 @@ for (const optionPath of optionPaths) { | ||
const { processedEnvs, readableFilepaths, uniqueInjectedKeys } = new Run( | ||
envs, | ||
overload, | ||
DOTENV_KEY, | ||
processEnv | ||
).run() | ||
const { | ||
processedEnvs, | ||
readableFilepaths, | ||
uniqueInjectedKeys | ||
} = new Run(envs, overload, DOTENV_KEY, processEnv).run() | ||
@@ -85,62 +83,47 @@ let lastError | ||
if (processedEnv.type === 'envVaultFile') { | ||
logger.verbose( | ||
`loading env from encrypted ${processedEnv.filepath} (${path.resolve( | ||
processedEnv.filepath | ||
)})` | ||
) | ||
logger.debug( | ||
`decrypting encrypted env from ${ | ||
processedEnv.filepath | ||
} (${path.resolve(processedEnv.filepath)})` | ||
) | ||
logger.verbose(`loading env from encrypted ${processedEnv.filepath} (${path.resolve(processedEnv.filepath)})`) | ||
logger.debug(`decrypting encrypted env from ${processedEnv.filepath} (${path.resolve(processedEnv.filepath)})`) | ||
} | ||
if (processedEnv.type === 'envFile') { | ||
logger.verbose( | ||
`loading env from ${processedEnv.filepath} (${path.resolve( | ||
processedEnv.filepath | ||
)})` | ||
) | ||
logger.verbose(`loading env from ${processedEnv.filepath} (${path.resolve(processedEnv.filepath)})`) | ||
} | ||
if (processedEnv.error) { | ||
lastError = processedEnv.error | ||
for (const error of processedEnv.errors || []) { | ||
if (strict) throw error // throw immediately if strict | ||
if (processedEnv.error.code === 'MISSING_ENV_FILE') { | ||
// do not warn for conventions (too noisy) | ||
if (!options.convention) { | ||
logger.warnv(processedEnv.error.message) | ||
logger.help( | ||
`? add one with [echo "HELLO=World" > ${processedEnv.filepath}] and re-run [dotenvx run -- yourcommand]` | ||
) | ||
lastError = error // surface later in { error } | ||
if (error.code === 'MISSING_ENV_FILE') { | ||
if (!options.convention) { // do not output error for conventions (too noisy) | ||
console.error(error.message) | ||
if (error.help) { | ||
logger.help(error.help) | ||
} | ||
} | ||
} else { | ||
logger.warnv(processedEnv.error.message) | ||
console.error(error.message) | ||
if (error.help) { | ||
logger.help(error.help) | ||
} | ||
} | ||
} else { | ||
Object.assign(parsedAll, processedEnv.injected) | ||
Object.assign(parsedAll, processedEnv.preExisted) // preExisted 'wins' | ||
} | ||
// debug parsed | ||
const parsed = processedEnv.parsed | ||
logger.debug(parsed) | ||
Object.assign(parsedAll, processedEnv.injected || {}) | ||
Object.assign(parsedAll, processedEnv.preExisted || {}) // preExisted 'wins' | ||
// verbose/debug injected key/value | ||
const injected = processedEnv.injected | ||
for (const [key, value] of Object.entries(injected)) { | ||
logger.verbose(`${key} set`) | ||
logger.debug(`${key} set to ${value}`) | ||
} | ||
// debug parsed | ||
logger.debug(processedEnv.parsed) | ||
// verbose/debug preExisted key/value | ||
const preExisted = processedEnv.preExisted | ||
for (const [key, value] of Object.entries(preExisted)) { | ||
logger.verbose( | ||
`${key} pre-exists (protip: use --overload to override)` | ||
) | ||
logger.debug( | ||
`${key} pre-exists as ${value} (protip: use --overload to override)` | ||
) | ||
} | ||
// verbose/debug injected key/value | ||
for (const [key, value] of Object.entries(processedEnv.injected || {})) { | ||
logger.verbose(`${key} set`) | ||
logger.debug(`${key} set to ${value}`) | ||
} | ||
// verbose/debug preExisted key/value | ||
for (const [key, value] of Object.entries(processedEnv.preExisted || {})) { | ||
logger.verbose(`${key} pre-exists (protip: use --overload to override)`) | ||
logger.debug(`${key} pre-exists as ${value} (protip: use --overload to override)`) | ||
} | ||
} | ||
@@ -160,2 +143,4 @@ | ||
} catch (error) { | ||
if (strict) throw error // throw immediately if strict | ||
logger.error(error.message) | ||
@@ -199,13 +184,2 @@ if (error.help) { | ||
/** @type {import('./main').get} */ | ||
const get = function ( | ||
key, | ||
envs = [], | ||
overload = false, | ||
DOTENV_KEY = '', | ||
all = false | ||
) { | ||
return new Get(key, envs, overload, DOTENV_KEY, all).run() | ||
} | ||
/** @type {import('./main').keypair} */ | ||
@@ -222,3 +196,2 @@ const keypair = function (envFile, key) { | ||
ls, | ||
get, | ||
keypair, | ||
@@ -225,0 +198,0 @@ genexample, |
@@ -8,2 +8,3 @@ const fsx = require('./../helpers/fsx') | ||
const Errors = require('./../helpers/errors') | ||
const guessPrivateKeyName = require('./../helpers/guessPrivateKeyName') | ||
@@ -105,6 +106,3 @@ const findPrivateKey = require('./../helpers/findPrivateKey') | ||
if (e.code === 'ENOENT') { | ||
const error = new Error(`missing ${envFilepath} file (${filepath})`) | ||
error.code = 'MISSING_ENV_FILE' | ||
row.error = error | ||
row.error = new Errors({ envFilepath, filepath }).missingEnvFile() | ||
} else { | ||
@@ -111,0 +109,0 @@ row.error = e |
@@ -8,2 +8,3 @@ const fsx = require('./../helpers/fsx') | ||
const Errors = require('./../helpers/errors') | ||
const guessPrivateKeyName = require('./../helpers/guessPrivateKeyName') | ||
@@ -185,6 +186,3 @@ const guessPublicKeyName = require('./../helpers/guessPublicKeyName') | ||
if (e.code === 'ENOENT') { | ||
const error = new Error(`missing ${envFilepath} file (${filepath})`) | ||
error.code = 'MISSING_ENV_FILE' | ||
row.error = error | ||
row.error = new Errors({ envFilepath, filepath }).missingEnvFile() | ||
} else { | ||
@@ -191,0 +189,0 @@ row.error = e |
@@ -5,2 +5,3 @@ const fsx = require('./../helpers/fsx') | ||
const Errors = require('../helpers/errors') | ||
const findEnvFiles = require('../helpers/findEnvFiles') | ||
@@ -43,9 +44,4 @@ const replace = require('../helpers/replace') | ||
if (!fsx.existsSync(filepath)) { | ||
const code = 'MISSING_ENV_FILE' | ||
const message = `file does not exist at [${filepath}]` | ||
const help = `? add it with [echo "HELLO=World" > ${envFilepath}] and then run [dotenvx genexample]` | ||
const error = new Error(message) | ||
error.code = code | ||
error.help = help | ||
const error = new Errors({ envFilepath, filepath }).missingEnvFile() | ||
error.help = `? add it with [echo "HELLO=World" > ${envFilepath}] and then run [dotenvx genexample]` | ||
throw error | ||
@@ -52,0 +48,0 @@ } |
const Run = require('./run') | ||
const Errors = require('./../helpers/errors') | ||
class Get { | ||
constructor (key, envs = [], overload = false, DOTENV_KEY = '', all = false) { | ||
constructor (key, envs = [], overload = false, DOTENV_KEY = '', all = false, strict = false) { | ||
this.key = key | ||
@@ -10,2 +11,3 @@ this.envs = envs | ||
this.all = all | ||
this.strict = strict | ||
} | ||
@@ -17,6 +19,23 @@ | ||
if (!this.key) { | ||
const errors = [] | ||
for (const processedEnv of processedEnvs) { | ||
for (const error of processedEnv.errors) { | ||
errors.push(error) | ||
} | ||
} | ||
if (this.key) { | ||
const parsed = {} | ||
const value = processEnv[this.key] | ||
parsed[this.key] = value | ||
if (value === undefined) { | ||
errors.push(new Errors({ key: this.key }).missingKey()) | ||
} | ||
return { parsed, errors } | ||
} else { | ||
// if user wants to return ALL envs (even prior set on machine) | ||
if (this.all) { | ||
return processEnv | ||
return { parsed: processEnv, errors } | ||
} | ||
@@ -27,3 +46,3 @@ | ||
/** @type {Record<string, string>} */ | ||
const result = {} | ||
const parsed = {} | ||
for (const processedEnv of processedEnvs) { | ||
@@ -33,3 +52,3 @@ // parsed means we saw the key in a file or --env flag. this effectively filters out any preset machine envs - while still respecting complex evaluating, expansion, and overload. in other words, the value might be the machine value because the key was displayed in a .env file | ||
for (const key of Object.keys(processedEnv.parsed)) { | ||
result[key] = processEnv[key] | ||
parsed[key] = processEnv[key] | ||
} | ||
@@ -39,6 +58,4 @@ } | ||
return result | ||
return { parsed, errors } | ||
} | ||
return processEnv[this.key] | ||
} | ||
@@ -45,0 +62,0 @@ } |
@@ -11,2 +11,3 @@ const fsx = require('./../helpers/fsx') | ||
const Parse = require('./../helpers/parse') | ||
const Errors = require('./../helpers/errors') | ||
const parseEnvironmentFromDotenvKey = require('./../helpers/parseEnvironmentFromDotenvKey') | ||
@@ -63,5 +64,5 @@ const detectEncoding = require('./../helpers/detectEncoding') | ||
try { | ||
const { parsed, warnings, injected, preExisted } = new Parse(env, null, this.processEnv, this.overload).run() | ||
const { parsed, errors, injected, preExisted } = new Parse(env, null, this.processEnv, this.overload).run() | ||
row.parsed = parsed | ||
row.warnings = warnings | ||
row.errors = errors | ||
row.injected = injected | ||
@@ -78,3 +79,3 @@ row.preExisted = preExisted | ||
} catch (e) { | ||
row.error = e | ||
row.errors = [e] | ||
} | ||
@@ -97,6 +98,6 @@ | ||
const privateKey = findPrivateKey(envFilepath) | ||
const { parsed, warnings, injected, preExisted } = new Parse(src, privateKey, this.processEnv, this.overload).run() | ||
const { parsed, errors, injected, preExisted } = new Parse(src, privateKey, this.processEnv, this.overload).run() | ||
row.parsed = parsed | ||
row.warnings = warnings | ||
row.errors = errors | ||
row.injected = injected | ||
@@ -111,9 +112,6 @@ row.preExisted = preExisted | ||
} catch (e) { | ||
if (e.code === 'ENOENT') { | ||
const error = new Error(`missing ${envFilepath} file (${filepath})`) | ||
error.code = 'MISSING_ENV_FILE' | ||
row.error = error | ||
if (e.code === 'ENOENT' || e.code === 'EISDIR') { | ||
row.errors = [new Errors({ envFilepath, filepath }).missingEnvFile()] | ||
} else { | ||
row.error = e | ||
row.errors = [e] | ||
} | ||
@@ -170,5 +168,5 @@ } | ||
// parse this. it's the equivalent of the .env file | ||
const { parsed, warnings, injected, preExisted } = new Parse(decrypted, null, this.processEnv, this.overload).run() | ||
const { parsed, errors, injected, preExisted } = new Parse(decrypted, null, this.processEnv, this.overload).run() | ||
row.parsed = parsed | ||
row.warnings = warnings | ||
row.errors = errors | ||
row.injected = injected | ||
@@ -183,3 +181,3 @@ row.preExisted = preExisted | ||
} catch (e) { | ||
row.error = e | ||
row.errors = [e] | ||
} | ||
@@ -186,0 +184,0 @@ |
@@ -7,2 +7,3 @@ const fsx = require('./../helpers/fsx') | ||
const Errors = require('./../helpers/errors') | ||
const guessPrivateKeyName = require('./../helpers/guessPrivateKeyName') | ||
@@ -173,6 +174,3 @@ const guessPublicKeyName = require('./../helpers/guessPublicKeyName') | ||
if (e.code === 'ENOENT') { | ||
const error = new Error(`missing ${envFilepath} file (${filepath})`) | ||
error.code = 'MISSING_ENV_FILE' | ||
row.error = error | ||
row.error = new Errors({ envFilepath, filepath }).missingEnvFile() | ||
} else { | ||
@@ -179,0 +177,0 @@ row.error = e |
222785
84
3684
1933
29