UNPKG

dotenv-manipulator

Version:

Load, add, update or remove variables from both your .env file and process.env at runtime

97 lines (96 loc) 3.71 kB
/** * dotenv Manipulator : * - Load environment variables from a `.env` to `process.env` * - Add, update, or remove variables from both your `.env` file and `process.env` <b>at runtime</b> * @example * // require * const Manipulator = require('dotenv-manipulator').default * @example * // import * import Manipulator from 'dotenv-manipulator' */ declare class Manipulator { env_path: string; env: { [x: string]: string; }; encoding: 'ascii' | 'base64' | 'binary' | 'hex' | 'latin1' | 'ucs-2' | 'ucs2' | 'utf-8' | 'utf8' | 'utf16le'; throwable: boolean; /** * Starts dotenv Manipulator * @param envPath Path to `.env`, default is `process.cwd()`. * @param throwable If set to `true` functions throw errors whenever there's an incorrect input. * @param encoding Specify file encoding, default is `utf8`. * @example * // basic setup * const Manipulator = require('dotenv-manipulator') * const dotenvM = new Manipulator() * @example * // complete setup * const Manipulator = require('dotenv-manipulator') * const dotenvM = new Manipulator('/path/to/project', false, 'utf-8') * @example * // undefined = default value * const Manipulator = require('dotenv-manipulator') * const dotenvM = new Manipulator(undefined, true, 'latin1') */ constructor(envPath?: string, throwable?: boolean, encoding?: 'ascii' | 'base64' | 'binary' | 'hex' | 'latin1' | 'ucs-2' | 'ucs2' | 'utf-8' | 'utf8' | 'utf16le'); private __readSync; /** * Add object keys and values to `.env` file and `process.env` * @param obj Object to add to the environment * @param force If you try to add a key/value pair that is already in the environment : `add(obj, true)` would update said variables and `add(obj)` would just ignore your input * @example * const obj = { REMOTE: '95.81.123.228', PORT: 3000 } * dotenvM.add(obj) * @example * // without force argument * console.log(process.env.REMOTE_PORT) //=> '3000' * dotenvM.add({ REMOTE_PORT: '2000' }) * console.log(process.env.REMOTE_PORT) //=> '3000' * * // with force argument set to true * dotenvM.add({ REMOTE_PORT: '2000' }, true) * console.log(process.env.REMOTE_PORT) //=> '2000' * @example * // DEBUGGING #1 * dotenvM.throwable = false * * let debug = dotenvM.add(['wrong', 'type', 'of', 'data']) * let debug_1 = dotenvM.add({ GOOOD: 'type', OF: 'data' }) * console.log(debug.message) //=> [ADD_ERROR]: object must be [object Object] but received [object Array] * console.log(debug_1) //=> undefined * * // DEBUGGING #2 * dotenvM.throwable = true * * try { * dotenvM.add(['wrong', 'type', 'of', 'data']) * } catch(e) { * console.log(e.message) //=> [ADD_ERROR]: object must be [object Object] but received [object Array] * } */ add(obj: { [x: string]: string; }, force?: boolean): TypeError | undefined; /** * Remove variable from `.env` file and `process.env` * @param obj Key(s) you want to remove * @example * // #1 * dotenvM.remove('REMOTE') * dotenvM.remove('PORT') * * // #2 * dotenvM.remove( { REMOTE: 'value that dotenvM wont read', PORT: 'no.. really it doesnt care' } ) * * // #3 * dotenvM.remove(['REMOTE', 'PORT']) */ remove(obj: { [x: string]: string; } | string | string[]): TypeError | undefined; private __sort; private __save; } export default Manipulator;