Comparing version 1.1.0 to 1.1.1
{ | ||
"name": "confme", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Opinionated config library that allows you to have complex config and behaves according to Twelve Factor App rules", | ||
@@ -10,3 +10,5 @@ "main": "src/confme.js", | ||
"scripts": { | ||
"test": "ava" | ||
"test": "ava", | ||
"coverage": "c8 ava", | ||
"lint": "eslint **/*.js" | ||
}, | ||
@@ -45,10 +47,21 @@ "repository": { | ||
"dependencies": { | ||
"dotenv-defaults": "^1.1.1", | ||
"json5": "^2.1.3", | ||
"livr": "^2.3.1", | ||
"dotenv-defaults": "^3.0.0", | ||
"json5": "^2.2.0", | ||
"livr": "^2.3.3", | ||
"livr-extra-rules": "^1.2.1" | ||
}, | ||
"devDependencies": { | ||
"ava": "^3.5.0" | ||
"ava": "^3.15.0", | ||
"babel-eslint": "^10.1.0", | ||
"c8": "^7.9.0", | ||
"eslint": "^7.32.0", | ||
"eslint-config-webbylab": "^5.4.1", | ||
"eslint-plugin-fetch": "0.0.1", | ||
"eslint-plugin-import": "^2.24.2", | ||
"eslint-plugin-more": "^1.0.3", | ||
"eslint-plugin-no-require-lodash": "^1.1.0", | ||
"eslint-plugin-prefer-spread": "^1.0.3", | ||
"eslint-plugin-react": "^7.26.1", | ||
"eslint-plugin-security": "^1.4.0" | ||
} | ||
} |
## confme | ||
Opinionated config library that allows you to have complex config and behaves according to "Twelve Factor App" rules. | ||
Opinionated config library that allows you to have complex config, and behaves according to "Twelve Factor App" rules. | ||
@@ -97,11 +97,12 @@ - It is build on top of [dotenv-defaults](https://www.npmjs.com/package/dotenv-defaults) | ||
According to [Twelve Factor App](https://12factor.net/config) your config should be passed in envrironment variables. If you are not familiar with ideas of "Twelve Factor App" you definetely should read it. | ||
According to [Twelve Factor App](https://12factor.net/config), your config should be passed in envrironment variables. If you are not familiar with ideas of "Twelve Factor App," you should definitely read it. | ||
Having all config variables in env variables is very flexible. You can run your app with docker and without docker. Moreover, you can reuse the same builds across all environments. For example, you can build an image, test it on QA and then run the same image well-tested imaged on production. | ||
But passing the conf in environment variables is not very convenient. So, there a popular library called [dotenv](dotenv) which allows you to store environment variables in ".env" file. But you should not commit it and you should have a sample in repository (like ".env.sample" which will be copied to ".env" on deployments without docker). | ||
But passing the conf in environment variables is not very convenient. So, there a popular library called [dotenv](dotenv) which allows you to store environment variables in ".env" files. But you should not commit them and you should have a sample in repository (like ".env.sample" which will be copied to ".env" on deployments without docker). | ||
You can use [dotenv-defaults](https://www.npmjs.com/package/dotenv-defaults) which allows you to have file ".env.defaults" with default values commited to your repository. | ||
But in real life you have rather complex configs and you do not want to define all of the values in ENV, you want to use your config as a template and build final config based on this template. It is very common approach for ansible users. **confme** allowes to do that. | ||
But in real life, if you have rather complex configs and you do not want to define all of the values in ENV, you want to use your config as a template and build final config based on this template. It is very common approach for ansible users. **confme** allows you to do that. | ||
Moreover, **confme** allowes you to define [LIVR](http://livr-spec.org/) schema to validate config. It can be heplful if you have complex configs with a lot of options but I prefer to use validation schema even with small configs. | ||
Moreover, **confme** allows you to define [LIVR](http://livr-spec.org/) schema to validate configurations. It can be helpful if you have complex configs with a lot of options but I prefer to use validation schema even with small configs. |
@@ -1,57 +0,63 @@ | ||
require("dotenv-defaults").config(); | ||
require('dotenv-defaults').config(); | ||
const JSON5 = require("json5") | ||
const fs = require("fs"); | ||
const fs = require('fs'); | ||
// eslint-disable-next-line more/no-numeric-endings-for-variables | ||
const JSON5 = require('json5'); | ||
function confme(configPath, livrSchemaPath) { | ||
const template = fs.readFileSync(configPath).toString(); | ||
const configStr = replace(template, process.env); | ||
// eslint-disable-next-line no-sync | ||
const template = fs.readFileSync(configPath).toString(); | ||
const configStr = replace(template, process.env); | ||
let config = {}; | ||
let config = {}; | ||
try { | ||
config = JSON5.parse(configStr); | ||
} catch (error) { | ||
console.error("CANNOT PARSE JSON5:", configStr); | ||
throw error; | ||
} | ||
try { | ||
config = JSON5.parse(configStr); | ||
} catch (error) { | ||
console.error('CANNOT PARSE JSON5:', configStr); | ||
throw error; | ||
} | ||
if (livrSchemaPath) { | ||
config = validateConfig(config, livrSchemaPath); | ||
} | ||
if (livrSchemaPath) { | ||
config = validateConfig(config, livrSchemaPath); | ||
} | ||
return config; | ||
return config; | ||
} | ||
function validateConfig(config, livrSchemaPath) { | ||
const LIVR = require("livr"); | ||
const livrExtraRules = require("livr-extra-rules"); | ||
const LIVR = require('livr'); | ||
const livrExtraRules = require('livr-extra-rules'); | ||
const livrRules = JSON5.parse(fs.readFileSync(livrSchemaPath).toString()); | ||
const validator = new LIVR.Validator(livrRules, true); | ||
validator.registerRules(livrExtraRules); | ||
// eslint-disable-next-line no-sync | ||
const livrRules = JSON5.parse(fs.readFileSync(livrSchemaPath).toString()); | ||
const validator = new LIVR.Validator(livrRules, true); | ||
const validConfig = validator.validate(config); | ||
validator.registerRules(livrExtraRules); | ||
if (!validConfig) { | ||
const error = { | ||
FAILED_CONFIG: config, | ||
ERRORS: validator.getErrors() | ||
}; | ||
throw new Error(JSON.stringify(error, null, 2)); | ||
} | ||
const validConfig = validator.validate(config); | ||
return validConfig; | ||
if (!validConfig) { | ||
const error = { | ||
FAILED_CONFIG : config, | ||
ERRORS : validator.getErrors() | ||
}; | ||
// eslint-disable-next-line no-magic-numbers | ||
throw new Error(JSON.stringify(error, null, 2)); | ||
} | ||
return validConfig; | ||
} | ||
function replace(template, vars) { | ||
return template.replace(/\{\{\s*(.+?)\s*\}\}/g, (match, p1) => { | ||
if (vars.hasOwnProperty(p1)) { | ||
return vars[p1]; | ||
} else { | ||
throw new Error(`Variable "${p1}" not set!`); | ||
} | ||
}); | ||
return template.replace(/\{\{\s*(.+?)\s*\}\}/g, (match, p1) => { | ||
if (vars.hasOwnProperty(p1)) { | ||
return vars[p1]; | ||
} | ||
throw new Error(`Variable "${p1}" not set!`); | ||
}); | ||
} | ||
module.exports = confme; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
12653
561
15
142
108
12
4
+ Addeddotenv@10.0.0(transitive)
+ Addeddotenv-defaults@3.0.2(transitive)
- Removeddotenv@6.2.0(transitive)
- Removeddotenv-defaults@1.1.1(transitive)
Updateddotenv-defaults@^3.0.0
Updatedjson5@^2.2.0
Updatedlivr@^2.3.3