environment-override
Advanced tools
Comparing version
173
bin/show.js
#!/usr/bin/env node | ||
'use strict'; | ||
/** | ||
* @fileOverview | ||
* Command line based helper to see what values you can override using this | ||
* module from a json file. | ||
*/ | ||
var _fs = require('fs'); | ||
var _fs2 = _interopRequireDefault(_fs); | ||
/* ADD MODULES */ | ||
var _path = require('path'); | ||
var override = require('../'); | ||
var _path2 = _interopRequireDefault(_path); | ||
var fs = require('fs'); | ||
var util = require('util'); | ||
var _cli = require('cli'); | ||
var jsdiff = require('diff'); | ||
require('colors'); | ||
var _cli2 = _interopRequireDefault(_cli); | ||
var _diff = require('diff'); | ||
console.info(); | ||
var _colors = require('colors'); | ||
/* BASIC COMMAND LINE TOOL VALIDAITON / HELP */ | ||
var _colors2 = _interopRequireDefault(_colors); | ||
if (process.argv[2] == 'help' || process.argv[2] == '--help' || | ||
process.argv.lenght > 4 || process.argv.lenght < 3) { | ||
console.info('This script will output the environment variable names you can use to override the values in the provided json file.'); | ||
console.info('Script expects upto 3 arguments,'); | ||
console.info(' argument 1: relative path to json file to check [required]'); | ||
console.info(' argument 2: a prefix for the environment variable [optional]'); | ||
console.info(' argument 3: output mode (all/diff/original/current) [optional]'); | ||
console.info('e.g.'); | ||
console.info('\tnode show.js test.json foo diff'); | ||
process.exit(); | ||
} | ||
var _environmentOverride = require('../dist/environment-override'); | ||
var file = process.argv[2]; | ||
var prefix = (process.argv[3] ? process.argv[3] : ''); | ||
var output = process.argv[4]; | ||
var _environmentOverride2 = _interopRequireDefault(_environmentOverride); | ||
if (!fs.existsSync(file)) { | ||
console.info('Expecting 1st argument to be a path to a json file'); | ||
process.exit(); | ||
} | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* @fileOverview | ||
* Command line based helper to see what values you can override using this | ||
* module from a json file. | ||
*/ | ||
/* SETUP / GET VALUES */ | ||
var showDiff = function showDiff(diffManifest) { | ||
diffManifest.forEach(function (part) { | ||
var prefix = ''; | ||
var color = 'grey'; | ||
console.info('Loading file : ' + process.cwd() + '/' + file); | ||
if (part.added) { | ||
prefix = '++'; | ||
color = 'green'; | ||
} else if (part.removed) { | ||
prefix = '--'; | ||
color = 'red'; | ||
} | ||
var json = require(process.cwd() + '/' + file); | ||
process.stdout.write(_colors2.default[color]('' + prefix + part.value)); | ||
}); | ||
}; | ||
// hack to deep clone without needing another dependency. | ||
var original = JSON.parse(JSON.stringify(json)); | ||
var clone = function clone(obj) { | ||
return JSON.parse(JSON.stringify(obj)); | ||
}; | ||
console.info("\n"+ 'Using the prefix "'+ prefix.toUpperCase() +'" you have the following overrides:'+"\n"); | ||
_cli2.default.enable('version', 'status'); | ||
_cli2.default.setApp(_path2.default.join(__dirname, '/../package.json')); | ||
_cli2.default.parse({ | ||
prefix: ['p', 'A prefix for the environment variable', 'string', ''], | ||
output: ['o', 'The output mode: all/diff/original/current', 'string', ''] | ||
}); | ||
/* DO IT */ | ||
_cli2.default.main(function (args, options) { | ||
if (!args.length) { | ||
_cli2.default.getUsage(); | ||
} | ||
override(json, prefix, true); | ||
var file = args[0]; | ||
if (!_fs2.default.existsSync(file)) { | ||
_cli2.default.error(file + ' is not a path to a json file.'); | ||
process.exit(); | ||
} | ||
var fileContents = _fs2.default.readFileSync(file); | ||
var manifest = void 0; | ||
/* GIVE ADDIITONAL OUTPUT IF REQUESTED */ | ||
try { | ||
manifest = JSON.parse(fileContents); | ||
} catch (e) { | ||
_cli2.default.error(file + ' is not in json format.'); | ||
process.exit(); | ||
} | ||
if (output) { | ||
console.info("\nOuput requested : " + output); | ||
if (options.prefix) { | ||
_cli2.default.info('Using the prefix "' + options.prefix.toUpperCase() + '" you have the following overrides:'); | ||
} else { | ||
_cli2.default.info('You have the following overrides:'); | ||
} | ||
var originalStringify = JSON.stringify(original, null, ' '); | ||
var jsonStringify = JSON.stringify(json, null, ' '); | ||
var diff = jsdiff.diffLines(originalStringify, jsonStringify); | ||
var originalManifest = clone(manifest); | ||
(0, _environmentOverride2.default)(manifest, options.prefix, true); | ||
var _showDiff = function _showDiff(diff) { | ||
console.info("\nDiff"); | ||
diff.forEach(function diffEach(part){ | ||
var prefix = part.added ? '++' : | ||
part.removed ? '--' : ''; | ||
// green for additions, red for deletions | ||
// grey for common parts | ||
var color = part.added ? 'green' : | ||
part.removed ? 'red' : 'grey'; | ||
if (options.output) { | ||
var output = options.output.toLowerCase(); | ||
_cli2.default.info('Ouput requested: ' + output); | ||
process.stdout.write(prefix + part.value[color]); | ||
}); | ||
}; | ||
var originalStringify = JSON.stringify(originalManifest, null, ' '); | ||
var overridenStringify = JSON.stringify(manifest, null, ' '); | ||
var diffManifest = (0, _diff.diffLines)(originalStringify, overridenStringify); | ||
switch (output) { | ||
case 'all': | ||
console.info("\nOriginal\n", originalStringify); | ||
console.info("\nOverridden\n", jsonStringify); | ||
_showDiff(diff); | ||
break; | ||
case 'diff': | ||
_showDiff(diff); | ||
break; | ||
case 'original': | ||
console.info("\nOriginal\n", originalStringify); | ||
break; | ||
case 'current': | ||
console.info("\nOverridden\n", jsonStringify); | ||
break; | ||
} | ||
} | ||
switch (output) { | ||
case 'all': | ||
_cli2.default.info('Original'); | ||
_cli2.default.info(originalStringify); | ||
_cli2.default.info('Overridden'); | ||
_cli2.default.info(overridenStringify); | ||
console.info("\n\n" +'Done'); | ||
process.exit(); | ||
showDiff(diffManifest); | ||
break; | ||
case 'diff': | ||
_cli2.default.info('Diff'); | ||
showDiff(diffManifest); | ||
break; | ||
case 'original': | ||
_cli2.default.info('Original'); | ||
_cli2.default.info(originalStringify); | ||
break; | ||
case 'current': | ||
_cli2.default.info('Overridden'); | ||
_cli2.default.info(overridenStringify); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
}); |
{ | ||
"name": "environment-override", | ||
"version": "0.0.4", | ||
"version": "1.0.0", | ||
"description": "Takes a JSON object and checks the environment variables for overrides to its values. Great for HAPIjs manifests and other configuration.", | ||
"main": "index.js", | ||
"main": "./dist/environment-override.js", | ||
"scripts": { | ||
"test": "istanbul cover _mocha -- --no-timeouts -R dot test/tests/", | ||
"check-coverage": "istanbul check-coverage --statements 100 --functions 90 --lines 100 --branches 100" | ||
"prebuild": "rimraf dist && rimraf bin && mkdir dist && mkdir bin", | ||
"build": "babel --copy-files --out-file dist/environment-override.js src/environment-override.js && babel --out-file bin/show.js src/show.js", | ||
"postbuild": "chmod +x bin/show.js", | ||
"eslint": "eslint src", | ||
"test": "mocha src/*.test.js --compilers js:babel-core/register", | ||
"test:watch": "npm t -- -w", | ||
"cover": "nyc --reporter=lcov --reporter=text --require babel-core/register npm t", | ||
"check-coverage": "nyc check-coverage --statements 100 --branches 100 --functions 100 --lines 100", | ||
"report-coverage": "cat ./coverage/lcov.info | codecov" | ||
}, | ||
@@ -15,10 +22,22 @@ "bin": { | ||
"dependencies": { | ||
"diff": "~2.2.0", | ||
"colors": "~1.1.2" | ||
"cli": "^1.0.1", | ||
"colors": "^1.1.2", | ||
"diff": "^3.2.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "~3.4.1", | ||
"mocha": "~2.3.3", | ||
"istanbul": "~0.4.0", | ||
"sinon": "~1.17.2" | ||
"babel-cli": "^6.23.0", | ||
"babel-core": "^6.23.1", | ||
"babel-polyfill": "^6.23.0", | ||
"babel-preset-env": "^1.1.10", | ||
"chai": "^3.5.0", | ||
"codecov": "^1.0.1", | ||
"eslint": "^3.16.1", | ||
"eslint-config-airbnb": "^14.1.0", | ||
"eslint-plugin-import": "^2.2.0", | ||
"eslint-plugin-jsx-a11y": "^4.0.0", | ||
"eslint-plugin-react": "^6.10.0", | ||
"mocha": "^3.2.0", | ||
"nyc": "^10.1.2", | ||
"rimraf": "^2.6.1", | ||
"sinon": "^1.17.7" | ||
}, | ||
@@ -25,0 +44,0 @@ "directories": { |
101
README.md
@@ -7,61 +7,90 @@ Environment Override | ||
[](https://david-dm.org/a-c-m/environment-override) | ||
[](https://travis-ci.org/BrandedEntertainmentNetwork/environment-override) | ||
[](https://codecov.io/gh/BrandedEntertainmentNetwork/environment-override) | ||
[](https://opensource.org/licenses/MIT) | ||
> Takes a object and checks the environment variables for overrides to its values. Great for manifests and other configuration. | ||
> Takes an object holding configuration and checks the environment variables for overrides to its values. Great for hapi manifests and other types of configuration. | ||
How to use | ||
---------- | ||
# How to use | ||
The code is currently synchronous as its expected to only be run once as part | ||
of the startup of the app. | ||
The code is currently synchronous as its expected to only be run once as part of the startup of the app. | ||
In code : | ||
## In code | ||
var override = require('environment-override'); | ||
var json = require('./some.json'); | ||
override(json, 'PREFIX_'); | ||
var manifest = require('./manifest.json'); | ||
override(manifest, 'PREFIX_'); | ||
## Command line | ||
Or you can run it from the command line to see the variables you need to set : | ||
`environment-override` ships with an utility that can show you all the variables from a json file that can be overridden via environment variables: | ||
node ./node_modules/environment-override/bin/show test.json | ||
Use the following to see other options : | ||
To avoid polluting the environment you can set a prefix for the variables: | ||
node ./node_modules/environment-override/bin/show --help | ||
node ./node_modules/environment-override/bin/show test.json -p PREFIX_ | ||
To override, simply set the environment variables you want to override, you can | ||
remove by setting the entry to be equal `OVERRIDE_REMOVE_DATA`. | ||
It can also show the original and overridden versions: | ||
To override whole structures use stringified JSON objects in variables. ie: | ||
node ./node_modules/environment-override/bin/show test.json -o diff | ||
`` | ||
exports PREFIX_VARIABLE='{"key1": "value1", "key2": "value2"}' | ||
`` | ||
The output can be: | ||
* `diff` - Shows the diff between the two versions. | ||
* `all` - Shows both the original and the overridden versions. | ||
* `original` - Shows the original version. | ||
* `current` - Shows the overridden version. | ||
Code coverage | ||
-------------- | ||
> | ||
> npm test | ||
> | ||
> environment-override@0.1.3 test /.../environment-override | ||
> istanbul cover _mocha --dir ../coverage/environment -- --no-timeouts -R dot test/tests/ | ||
## Removing values | ||
To remove a value set the entry to `OVERRIDE_REMOVE_DATA` in the environment. | ||
## Whole structures | ||
․․․․․․․․․․ | ||
To override whole structures use a stringified JSON object in the variable: | ||
10 passing (14ms) | ||
exports PREFIX_VARIABLE='{"key1": "value1", "key2": "value2"}' | ||
============================================================================= | ||
Writing coverage object [./coverage/coverage.json] | ||
Writing coverage reports at [./coverage] | ||
============================================================================= | ||
=============================== Coverage summary =============================== | ||
Statements : 100% ( 21/21 ) | ||
Branches : 100% ( 12/12 ) | ||
Functions : 100% ( 1/1 ) | ||
Lines : 100% ( 21/21 ) | ||
================================================================================ | ||
# Example | ||
Assuming we have the following `test.json` file: | ||
{ | ||
"field1": "value1", | ||
"field2": { | ||
"s1": "v1", | ||
"s2": "v2" | ||
}, | ||
"field3": "value3" | ||
} | ||
We can see all the variables by running `show.js`: | ||
./bin/show.js test.json -p APP_ | ||
INFO: Using the prefix "APP_" you have the following overrides: | ||
APP_FIELD1 | ||
APP_FIELD2 | ||
APP_FIELD2_S1 | ||
APP_FIELD2_S2 | ||
APP_FIELD3 | ||
We can override them by setting various values: | ||
export APP_FIELD1 = 'updated1' | ||
export APP_FIELD2 = '{"s1": "updated_f2_value1", "s2": "updated_f2_value2"}'; | ||
export APP_FIELD3 = 'OVERRIDE_REMOVE_DATA' | ||
Running `show.js` again will inform us of which variables were overridden: | ||
./bin/show.js test.json -p APP_ | ||
INFO: Using the prefix "APP_" you have the following overrides: | ||
overridden APP_FIELD1 | ||
overridden APP_FIELD2 | ||
removed APP_FIELD3 | ||
We can also get a diff: | ||
./bin/show.js test.json -p APP_ -o diff |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
22501
47.87%12
71.43%465
60.34%0
-100%96
43.28%11
-8.33%3
50%15
275%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
Updated
Updated