Skip to content

Commit

Permalink
Convert all values to strings (#32)
Browse files Browse the repository at this point in the history
* Convert all values to strings

* Proper cleanup

* Add migration

* Version bump: 3.0.0
  • Loading branch information
dpikt authored Jun 25, 2019
1 parent 1148c62 commit eb65d8d
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ Full module API and usage info can be found in [docs.md](docs.md).


## Migration Guides
- [v2.0.0](migration-guides/v2.0.0.md)
- [v2.0.0](migration-guides/v2.0.0.md)
- [v3.0.0](migration-guides/v3.0.0.md)
2 changes: 1 addition & 1 deletion migration-guides/v2.0.0.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## v2.0.0 Migration Guide

The only breaking change in this version is that `Figaro` now throws an exception when loading invalid or malformed files. This shouldn't require any migrations, but in the (unlikely) case that behavior was being depended on, you can catch the error:
The only breaking change in this version is that `figaro-js` now throws an exception when loading invalid or malformed files. This shouldn't require any migrations, but in the (unlikely) case that behavior was being depended on, you can catch the error:
```js
try {
Figaro.load()
Expand Down
21 changes: 21 additions & 0 deletions migration-guides/v3.0.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## v2.0.0 Migration Guide

This version reverts the functionality where `figaro-js` casts values to primitives. In this version, `figaro-js` will read all values as strings. In order to emulate the former functionality, you can use a function to do it manually:

```js
const Figaro = require('figaro-js')
const { mapValues } = require('lodash')

// Converts strings to primitive values if possible
// Eg '5' -> 5, 'true' -> true, 'string' -> 'string'

function coerce(str) {
try {
return JSON.parse(str)
} catch (e) {
return str
}
}

const myEnv = mapValues(Figaro.read(), coerce)
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "figaro-js",
"version": "2.0.0",
"version": "3.0.0",
"description": "Env variable management in JS",
"main": "lib/index.js",
"bin": {
Expand Down
3 changes: 2 additions & 1 deletion src/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import {
omitObjectValues,
readYamlFile,
toStringValues,
} from './utils'

/**
Expand Down Expand Up @@ -53,7 +54,7 @@ function read ({
const sharedVars = omitObjectValues(allVars)
// Get environment vars
const environmentVars = allVars[environment] || {}
return { ...sharedVars, ...environmentVars }
return toStringValues({ ...sharedVars, ...environmentVars })
}

export default read
3 changes: 2 additions & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export findRoot from 'find-root'
// Local

export omitObjectValues from './omitObjectValues'
export readYamlFile from './readYamlFile'
export readYamlFile from './readYamlFile'
export toStringValues from './toStringValues'
2 changes: 1 addition & 1 deletion src/utils/readYamlFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const NOT_FOUND_CODE = 'ENOENT'

function readYamlFile (filePath) {
try {
return yaml.load(fs.readFileSync(filePath, 'utf8')) || {}
return yaml.safeLoad(fs.readFileSync(filePath, 'utf8')) || {}
} catch (e) {
// Throw all errors except for file-not-found errors
if (e.code !== NOT_FOUND_CODE) throw e
Expand Down
9 changes: 9 additions & 0 deletions src/utils/toStringValues.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { mapValues } from 'lodash'

// Casts values to strings using the String constructor

function toStringValues (obj) {
return mapValues(obj, String)
}

export default toStringValues
4 changes: 2 additions & 2 deletions test/cli/heroku.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ function callCLI (command) {
describe('heroku cli', () => {
test('builds upload command correctly', () => {
const executedCommand = callCLI(`heroku:set --app TEST --path ${ CONFIG_FILE_PATH }`)
expect(executedCommand).toEqual('heroku config:set SHARED_VAR=shared_var OVERRIDE_VAR=override_var --app=TEST')
expect(executedCommand).toEqual('heroku config:set SHARED_VAR=shared_var OVERRIDE_VAR=override_var BOOLEAN_VAR=true NUMBER_VAR=1 --app=TEST')
})
test('gets correct env when specified', () => {
const executedCommand = callCLI(`heroku:set -e production --path ${ CONFIG_FILE_PATH }`)
expect(executedCommand).toEqual('heroku config:set SHARED_VAR=shared_var OVERRIDE_VAR=production_override_var PRODUCTION_VAR=production_var')
expect(executedCommand).toEqual('heroku config:set SHARED_VAR=shared_var OVERRIDE_VAR=production_override_var BOOLEAN_VAR=true NUMBER_VAR=1 PRODUCTION_VAR=production_var')
})
})
2 changes: 2 additions & 0 deletions test/fixtures/application.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"SHARED_VAR": "shared_var",
"OVERRIDE_VAR": "override_var",
"BOOLEAN_VAR": true,
"NUMBER_VAR": 1,
"development": {
"DEVELOPMENT_VAR": "development_var"
},
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/application.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
SHARED_VAR: "shared_var"
OVERRIDE_VAR: "override_var"
BOOLEAN_VAR: true
NUMBER_VAR: 1

development:
DEVELOPMENT_VAR: "development_var"
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ export const DEVELOPMENT_ENV = {
SHARED_VAR: 'shared_var',
DEVELOPMENT_VAR: 'development_var',
OVERRIDE_VAR: 'override_var',
BOOLEAN_VAR: 'true',
NUMBER_VAR: '1',
}

export const PRODUCTION_ENV = {
SHARED_VAR: 'shared_var',
PRODUCTION_VAR: 'production_var',
OVERRIDE_VAR: 'production_override_var'
OVERRIDE_VAR: 'production_override_var',
BOOLEAN_VAR: 'true',
NUMBER_VAR: '1',
}

10 changes: 10 additions & 0 deletions test/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,14 @@ test('can read json file', () => {
})
expect(env).toEqual(DEVELOPMENT_ENV)
process.env.NODE_ENV = 'test'
})

test('parses all values as strings', () => {
process.env.NODE_ENV = 'development'
const env = read({
path: CONFIG_FILE_PATH
})
expect(typeof env.BOOLEAN_VAR).toBe('string')
expect(typeof env.NUMBER_VAR).toBe('string')
process.env.NODE_ENV = 'test'
})

0 comments on commit eb65d8d

Please sign in to comment.