Skip to content

Commit

Permalink
Release 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
roydukkey committed Aug 4, 2020
1 parent 03b1dd6 commit 18f758b
Show file tree
Hide file tree
Showing 18 changed files with 1,205 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]
insert_final_newline = true
indent_size = 2
indent_style = tab
tab_width = 2
trim_trailing_whitespace = true

[*.md]
indent_style = space
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# User-specific files
*.env
*.user
*.vs

# Package Managers
node_modules
npm-debug.log

# Build Artifacts
*.lock.json
*-lock.json
*.build
*.cache
*.tgz
.jest
dist
23 changes: 23 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# User-specific files
*.editorconfig
*.env
*.user
*.vs

# Package Managers
node_modules
npm-debug.log

# Build Artifacts
*.lock.json
*-lock.json
*.build
*.cache
*.tgz
.jest
build

# Source Code
src
test
tsconfig.json
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

* Initial release!
137 changes: 137 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# dotenv-dot

<img src="https://roydukkey.github.io/assets/images/dotenv-dot.png" alt="dotenv-dot" align="right" />

Dotenv-dot adds dot-notation variable transformation on top of [dotenv](http://github.com/motdotla/dotenv). If you find yourself needing to condense environment variables into JSON variables, then dotenv-dot is your tool.

[![Release Version](https://img.shields.io/npm/v/dotenv-dot.svg)](https://www.npmjs.com/package/dotenv-dot)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)


## Install

```bash
npm install dotenv --save
npm install dotenv-dot --save
```


## Usage

Dot-notation variables are those which contain a dot (.) in there name. Add dot-notation variables to the `.env` file just as any other variable.

```dosini
MAIL_CONFIG.service=gmail
MAIL_CONFIG.auth.user[email protected]
MAIL_CONFIG.auth.pass=pass1234
```

Once the dotenv-dot transformer is executed these variable will be condensed into a new variable named `MAIL_CONFIG`.

```dosini
MAIL_CONFIG='{"service":"gmail","auth":{"user":"[email protected]","pass":"pass1234"}}'
```

#### Transform `process.env` variables

As early as possible in your application, require dotenv and dotenv-expand, and execute dotenv-dot after dotenv.

```js
const dotenv = require('dotenv');
const dotenvDot = require('dotenv-dot').transform;

const myEnv = dotenv.config();
const output = dotenvDot(); // only updates `process.env`
```

#### Transform the dotenv config and `process.env`

If you want to update the `process.env` and the dotenv config, just include the config as a parameter on the dotenv-dot transformer.

```js
const myEnv = dotenv.config();
const output = dotenvDot(myEnv); // updates `process.env` and `myEnv`
```

#### Transform variables without affect `process.env`

It is possible to use dotenv without adding variable to `process.env`. Dotenv-dot can also do the same.

```js
const parsedOutput = dotenv.parse(`MAIL_CONFIG.service=gmail
[email protected]
MAIL_CONFIG.auth.pass=pass1234`);

const output = dotenvDot(parsedOutput);
```

If you already have parsed output you may use that without using dotenv.

```js
const output = dotenvDot({
'MAIL_CONFIG.service': 'gmail',
'MAIL_CONFIG.auth.user': '[email protected]'
'MAIL_CONFIG.auth.pass': 'pass1234'
});
```


### Options

##### debug

Default: `false`

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

```js
const myEnv = dotenv.config();

require('dotenv-dot').transform(myEnv, {
debug: process.env.DEBUG
});
```

##### ignoreProcessEnv

Default: `false`

You may want to ignore the `process.env` when transforming a dotenv config output. When this option is turned on the `process.env` will not be consulted or altered.

```js
const myEnv = dotenv.config();

require('dotenv-dot').transform(myEnv, {
ignoreProcessEnv: true
});
```


### How do I use dotenv-dot with `import`?

```js
import * as dotenv from 'dotenv';
import dotenvDot from 'dotenv-dot';
```

Or, if only intending to access `process.env`, the two modules should be loading in this order using their auto-imports:

``` js
import 'dotenv/config';
import 'dotenv-dot/transform';
```

More information with the `import` syntax on the [dotenv repository](https://github.com/motdotla/dotenv/blob/master/README.md#how-do-i-use-dotenv-with-import).

*Note:* You may set the `debug` option using the dotenv debug [command line or environment variable](https://github.com/motdotla/dotenv/blob/master/README.md#preload). The `ignoreProcessEnv` option is irrelevant using the latter approach.


### How do I add arrays to the `.env` file?

Arrays are simply added by using numbers to indicate the value's index in the resulting array.

```dosini
THINGS.0='Was eaten by his others'
THINGS.1='Thing One'
THINGS.3='Wayward Thing Two'
```
14 changes: 14 additions & 0 deletions build/package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fs = require('fs');
const package = require('../package.json');

// Backup source file
fs.renameSync('./package.json', './package.json.build');

// Delete keys which are not needed in release package
delete package.eslintConfig;
delete package.babel;
delete package.scripts;
delete package.jest;

// Write changes to package
fs.writeFileSync('./package.json', JSON.stringify(package, null, 2));
170 changes: 170 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
{
"name": "dotenv-dot",
"description": "Transform .env variables into condensed JSON variables for use in nodejs projects.",
"version": "1.0.0",
"author": "roydukkey",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://[email protected]/roydukkey/dotenv-dot.git"
},
"homepage": "https://github.com/roydukkey/dotenv-dot#readme",
"bugs": {
"url": "https://github.com/roydukkey/dotenv-dot/issues"
},
"keywords": [
"dot-notation",
"JSON",
"dotenv",
"env",
".env",
"environment",
"variables",
"config",
"settings"
],
"dependencies": {
"dot-object": "^2.1.3"
},
"peerDependencies": {
"dotenv": ">= 8.2.0"
},
"devDependencies": {
"@babel/core": "^7.10.5",
"@babel/preset-env": "^7.10.4",
"@babel/preset-typescript": "^7.10.4",
"@types/dot-object": "^2.1.2",
"@types/jest": "^26.0.7",
"@types/node": "^14.0.27",
"@typescript-eslint/eslint-plugin": "^3.7.1",
"@typescript-eslint/parser": "^3.7.1",
"babel-jest": "^26.2.2",
"dotenv": "latest",
"eslint": "^7.5.0",
"eslint-plugin-tsdoc": "^0.2.6",
"jest": "^26.2.1",
"terser": "^4.8.0",
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
},
"type": "commonjs",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"lint": "eslint --ext .js,.ts src/ test/",
"build": "tsc",
"minify": "terser --compress --mangle -o ./dist/index.js -- ./dist/index.js",
"pretest": "npm run lint",
"test": "DOTENV_CONFIG_PATH=./test/.env.test jest",
"preparePackageJson": "node ./build/package.js",
"prepack": "npm run build && npm run minify && npm run test -- --coverage=false --verbose=false && npm run preparePackageJson"
},
"jest": {
"setupFiles": [
"./test/setup.ts"
],
"testEnvironment": "node",
"verbose": true,
"collectCoverage": true,
"coverageDirectory": ".jest/coverage",
"cacheDirectory": ".jest/cache"
},
"babel": {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
],
"@babel/preset-typescript"
]
},
"eslintConfig": {
"ignorePatterns": [
"dist/*"
],
"plugins": [
"@typescript-eslint/eslint-plugin",
"eslint-plugin-tsdoc"
],
"extends": [
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"rules": {
"arrow-parens": [
"error",
"always"
],
"arrow-spacing": [
"error",
{
"before": true,
"after": true
}
],
"brace-style": [
"error",
"stroustrup",
{
"allowSingleLine": false
}
],
"comma-spacing": [
"error",
{
"before": false,
"after": true
}
],
"curly": [
"error"
],
"comma-dangle": [
"error",
"never"
],
"key-spacing": [
"error"
],
"object-curly-spacing": [
"error",
"always"
],
"quotes": [
"error",
"single"
],
"semi": [
"error"
],
"sort-imports": [
"error",
{
"ignoreCase": false,
"ignoreDeclarationSort": false,
"ignoreMemberSort": false,
"memberSyntaxSortOrder": [
"single",
"multiple",
"all",
"none"
]
}
],
"space-before-blocks": [
"error",
"always"
],
"space-before-function-paren": [
"error",
"always"
],
"tsdoc/syntax": "warn",
"@typescript-eslint/no-var-requires": "off"
}
}
}
Loading

0 comments on commit 18f758b

Please sign in to comment.