@dotenvx/dotenvx
Advanced tools
Comparing version 1.30.1 to 1.31.0
@@ -5,4 +5,11 @@ # Changelog | ||
[Unreleased](https://github.com/dotenvx/dotenvx/compare/v1.30.1...main) | ||
[Unreleased](https://github.com/dotenvx/dotenvx/compare/v1.31.0...main) | ||
## [1.31.0](https://github.com/dotenvx/dotenvx/compare/v1.30.1...v1.31.0) | ||
### Added | ||
* expose `main.set` function ([#492](https://github.com/dotenvx/dotenvx/pull/492)) | ||
* add missing types for `main.config` ([#491](https://github.com/dotenvx/dotenvx/pull/491)) | ||
## [1.30.1](https://github.com/dotenvx/dotenvx/compare/v1.30.0...v1.30.1) | ||
@@ -9,0 +16,0 @@ |
{ | ||
"version": "1.30.1", | ||
"version": "1.31.0", | ||
"name": "@dotenvx/dotenvx", | ||
@@ -31,3 +31,2 @@ "description": "a better dotenv–from the creator of `dotenv`", | ||
"test-coverage": "tap run --show-full-coverage --timeout=60000", | ||
"test-single": "tap run --coverage-report=none tests/cli/actions/decrypt.test.js", | ||
"testshell": "bash shellspec", | ||
@@ -34,0 +33,0 @@ "prerelease": "npm test && npm run testshell", |
@@ -2084,3 +2084,14 @@ [![dotenvx](https://dotenvx.com/better-banner.png)](https://dotenvx.com) | ||
</details> | ||
* <details><summary>`set(KEY, value)`</summary><br> | ||
Programatically set an environment variable. | ||
```js | ||
// index.js | ||
const dotenvx = require('@dotenvx/dotenvx') | ||
dotenvx.set('HELLO', 'World', { path: '.env' }) | ||
``` | ||
</details> | ||
| ||
@@ -2087,0 +2098,0 @@ |
@@ -44,3 +44,3 @@ import type { URL } from 'url'; | ||
* @param src - contents to be parsed. example: `'DB_HOST=localhost'` | ||
* @param options - additional options. example: `{ prcoessEnv: {}, privateKey: '<privateKey>', overload: false }` | ||
* @param options - additional options. example: `{ processEnv: {}, privateKey: '<privateKey>', overload: false }` | ||
* @returns an object with keys and values based on `src`. example: `{ DB_HOST : 'localhost' }` | ||
@@ -87,2 +87,17 @@ */ | ||
/** | ||
* Throw immediately if an error is encountered - like a missing .env file. | ||
* @default false | ||
* @example require('@dotenvx/dotenvx').config({ strict: true }) | ||
*/ | ||
strict?: boolean; | ||
/** | ||
* Suppress specific errors like MISSING_ENV_FILE. The error keys can be found | ||
* in src/lib/helpers/errors.js | ||
* @default [] | ||
* @example require('@dotenvx/dotenvx').config({ ignore: ['MISSING_ENV_FILE'] }) | ||
*/ | ||
ignore?: string[]; | ||
/** | ||
* Specify an object to write your secrets to. Defaults to process.env environment variables. | ||
@@ -96,2 +111,9 @@ * | ||
/** | ||
* Customize the path to your .env.keys file. This is useful with monorepos. | ||
* @default [] | ||
* @example require('@dotenvx/dotenvx').config({ envKeysFile: '../../.env.keys'} }) | ||
*/ | ||
envKeysFile?: string; | ||
/** | ||
* Pass the DOTENV_KEY directly to config options. Defaults to looking for process.env.DOTENV_KEY environment variable. Note this only applies to decrypting .env.vault files. If passed as null or undefined, or not passed at all, dotenv falls back to its traditional job of parsing a .env file. | ||
@@ -164,3 +186,56 @@ * | ||
export interface SetOptions { | ||
/** | ||
* Specify a custom path if your file containing environment variables is located elsewhere. | ||
* Can also be an array of strings, specifying multiple paths. | ||
* | ||
* @default require('path').resolve(process.cwd(), '.env') | ||
* @example require('@dotenvx/dotenvx').set(key, value, { path: '/custom/path/to/.env' }) | ||
* @example require('@dotenvx/dotenvx').set(key, value, { path: ['/path/to/first.env', '/path/to/second.env'] }) | ||
*/ | ||
path?: string | string[] | URL; | ||
/** | ||
* Customize the path to your .env.keys file. This is useful with monorepos. | ||
* @default [] | ||
* @example require('@dotenvx/dotenvx').config(key, value, { envKeysFile: '../../.env.keys'} }) | ||
*/ | ||
envKeysFile?: string; | ||
/** | ||
* Set a .env convention (available conventions: 'nextjs') | ||
*/ | ||
convention?: string; | ||
} | ||
export type SetOutput = { | ||
key: string; | ||
value: string; | ||
filepath: string; | ||
envFilepath: string; | ||
envSrc: string; | ||
changed: boolean; | ||
encryptedValue?: string; | ||
publicKey?: string; | ||
privateKey?: string; | ||
privateKeyAdded?: boolean; | ||
privateKeyName?: string; | ||
error?: Error; | ||
}; | ||
/** | ||
* Set a single environment variable. | ||
* | ||
* @see https://dotenvx.com/docs | ||
* @param key - KEY | ||
* @param value - value | ||
* @param options - additional options. example: `{ encrypt: false }` | ||
*/ | ||
export function set( | ||
key: string, | ||
value: string, | ||
options?: SetOptions | ||
): SetOutput; | ||
/** | ||
* List all env files in the current working directory | ||
@@ -167,0 +242,0 @@ * |
@@ -11,2 +11,3 @@ // @ts-check | ||
const Run = require('./services/run') | ||
const Sets = require('./services/sets') | ||
const Keypair = require('./services/keypair') | ||
@@ -16,6 +17,4 @@ const Genexample = require('./services/genexample') | ||
// helpers | ||
const conventions = require('./helpers/conventions') | ||
const dotenvOptionPaths = require('./helpers/dotenvOptionPaths') | ||
const buildEnvs = require('./helpers/buildEnvs') | ||
const Parse = require('./helpers/parse') | ||
const DeprecationNotice = require('./helpers/deprecationNotice') | ||
@@ -50,26 +49,4 @@ /** @type {import('./main').config} */ | ||
// build envs using user set option.path | ||
const optionPaths = dotenvOptionPaths(options) // [ '.env' ] | ||
try { | ||
let envs = [] | ||
// handle shorthand conventions - like --convention=nextjs | ||
if (options.convention) { | ||
envs = conventions(options.convention).concat(envs) | ||
} | ||
new DeprecationNotice({ DOTENV_KEY }).dotenvKey() // DEPRECATION NOTICE | ||
for (const optionPath of optionPaths) { | ||
// if DOTENV_KEY is set then assume we are checking envVaultFile | ||
if (DOTENV_KEY) { | ||
envs.push({ | ||
type: 'envVaultFile', | ||
value: path.join(path.dirname(optionPath), '.env.vault') | ||
}) | ||
} else { | ||
envs.push({ type: 'envFile', value: optionPath }) | ||
} | ||
} | ||
const envs = buildEnvs(options, DOTENV_KEY) | ||
const { | ||
@@ -188,2 +165,21 @@ processedEnvs, | ||
/* @type {import('./main').set} */ | ||
const set = function (key, value, options = {}) { | ||
// encrypt | ||
let encrypt = true | ||
if (options.plain) { | ||
encrypt = false | ||
} else if (options.encrypt === false) { | ||
encrypt = false | ||
} | ||
// envKeysFile | ||
const envKeysFile = options.envKeysFile | ||
// envs | ||
const envs = buildEnvs(options) | ||
return new Sets(key, value, envs, encrypt, envKeysFile).run() | ||
} | ||
/** @type {import('./main').ls} */ | ||
@@ -214,2 +210,3 @@ const ls = function (directory, envFile, excludeEnvFile) { | ||
// actions related | ||
set, | ||
ls, | ||
@@ -216,0 +213,0 @@ keypair, |
240400
86
3862
2166