Comparing version 0.2.2 to 1.0.0
102
index.js
@@ -1,65 +0,85 @@ | ||
'use strict'; | ||
(function (global, factory) { | ||
if (typeof define === "function" && define.amd) { | ||
define(["exports"], factory); | ||
} else if (typeof exports !== "undefined") { | ||
factory(exports); | ||
} else { | ||
var mod = { | ||
exports: {} | ||
}; | ||
factory(mod.exports); | ||
global.config = mod.exports; | ||
} | ||
})(this, function (_exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, '__esModule', { | ||
Object.defineProperty(_exports, "__esModule", { | ||
value: true | ||
}); | ||
var env = process.env.NODE_ENV || 'development'; | ||
}); | ||
_exports.default = void 0; | ||
var util = require('util'); | ||
var fileName = util.format('%s.config.js', env); | ||
var config = require('../../config/' + fileName); | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
config.env = env; | ||
populateEachConfig(config); | ||
var env = process.env.NODE_ENV || 'development'; | ||
function replaceTemplateTags(_x) { | ||
var _again = true; | ||
var path = require('path'); | ||
_function: while (_again) { | ||
var key = _x; | ||
_again = false; | ||
var fileName = "".concat(env, ".config.js"); | ||
var filePath = path.resolve(__dirname, "../../config/".concat(fileName)); | ||
if (!key.length) { | ||
return config; | ||
} | ||
if (!Array.isArray(key)) { | ||
_x = key.split('.'); | ||
_again = true; | ||
continue _function; | ||
} | ||
return key.reduce(function (result, i) { | ||
return result && result[i]; | ||
}, config); | ||
var config = require(filePath); | ||
config.env = env; | ||
populateEachConfig(config); | ||
function replaceTemplateTags(key) { | ||
if (!key.length) { | ||
return config; | ||
} | ||
} | ||
function coerce(value) { | ||
if (!Array.isArray(key)) { | ||
return replaceTemplateTags(key.split('.')); | ||
} | ||
return key.reduce(function (result, i) { | ||
return result && result[i]; | ||
}, config); | ||
} | ||
function coerce(value) { | ||
if (typeof value !== 'string') { | ||
return value; | ||
return value; | ||
} | ||
value = value.replace(/\$\(([^\)]+)\)/g, function (_, key) { | ||
return replaceTemplateTags(key); | ||
return replaceTemplateTags(key); | ||
}); | ||
if (/^\d+$/.test(value)) { | ||
return parseInt(value, 10); | ||
return parseInt(value, 10); | ||
} | ||
if (value === 'false') { | ||
return false; | ||
return false; | ||
} | ||
if (value === 'true') { | ||
return true; | ||
return true; | ||
} | ||
return value; | ||
} | ||
} | ||
function populateEachConfig(cfg) { | ||
function populateEachConfig(cfg) { | ||
Object.keys(cfg).forEach(function (key) { | ||
if (typeof cfg[key] === 'object' && cfg[key]) { | ||
return populateEachConfig(cfg[key]); | ||
} | ||
cfg[key] = coerce(cfg[key]); | ||
if (_typeof(cfg[key]) === 'object' && cfg[key]) { | ||
return populateEachConfig(cfg[key]); | ||
} | ||
cfg[key] = coerce(cfg[key]); | ||
}); | ||
} | ||
} | ||
exports['default'] = config; | ||
module.exports = exports['default']; | ||
var _default = config; | ||
_exports.default = _default; | ||
}); |
{ | ||
"name": "c0nfig", | ||
"version": "0.2.2", | ||
"version": "1.0.0", | ||
"description": "require local configs as if it's node_modules", | ||
@@ -8,3 +8,3 @@ "main": "index.js", | ||
"build": "babel src/config.js --out-file index.js", | ||
"prepublish": "npm run build" | ||
"prepublishOnly": "npm run build" | ||
}, | ||
@@ -17,5 +17,8 @@ "repository": { | ||
"config", | ||
"configuration", | ||
"node_modules", | ||
"local", | ||
"require" | ||
"require", | ||
"import", | ||
"umd" | ||
], | ||
@@ -27,3 +30,20 @@ "author": "Dmitri Voronianski <dmitri.voronianski@gmail.com>", | ||
}, | ||
"homepage": "https://github.com/voronianski/c0nfig#readme" | ||
"homepage": "https://github.com/voronianski/c0nfig#readme", | ||
"prettier": { | ||
"singleQuote": true | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "pretty-quick --staged" | ||
} | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.1.2", | ||
"@babel/core": "^7.1.2", | ||
"@babel/preset-env": "^7.1.0", | ||
"husky": "^1.1.2", | ||
"prettier": "^1.14.3", | ||
"pretty-quick": "^1.8.0" | ||
} | ||
} |
@@ -5,6 +5,7 @@ # c0nfig | ||
[![Dependency Status](http://david-dm.org/voronianski/c0nfig.svg)](http://david-dm.org/voronianski/c0nfig) | ||
<!-- [![Download Count](http://img.shields.io/npm/dm/c0nfig.svg?style=flat)](http://www.npmjs.com/package/c0nfig) --> | ||
> Use [node-config-boilerplate](https://github.com/voronianski/node-config-boilerplate) simply by `require('c0nfig')`. | ||
> Require local environment based configs as if they are in `node_modules`. | ||
## Install | ||
```bash | ||
@@ -14,4 +15,58 @@ npm install c0nfig --save | ||
## Usage | ||
Create configs for every app environment just putting the env name as prefix of the file name: | ||
```bash | ||
app-folder/config:~$ ls -a | ||
production.config.js | ||
development.config.js | ||
# etc. | ||
``` | ||
Export some configuration data for every environment like: | ||
```js | ||
// config/development.config.js | ||
module.exports = { | ||
title: 'BLITZKRIEG BOP (STAGING)', | ||
apiUrl: 'https://staging.example.org/api' | ||
}; | ||
``` | ||
```js | ||
// config/production.config.js | ||
module.exports = { | ||
title: 'BLITZKRIEG BOP (PRODUCTION)', | ||
apiUrl: 'https://example.org/api' | ||
}; | ||
``` | ||
Start your app with proper `NODE_ENV` (if not provided it will grab `development.config.js` by default), then require/import `c0nfig` in your source code and use the data: | ||
```js | ||
// src/app.js | ||
const config = require('c0nfig'); | ||
const request = require('superagent'); | ||
request.get(config.apiUrl).then(res => { ... }); | ||
``` | ||
### Template tags | ||
You are able to use template tags like `$(configProperty.childProperty)` to point to specific properties of config: | ||
```js | ||
module.exports = { | ||
title: 'BLITZKRIEG BOP!', | ||
http: { | ||
port: process.env.PORT || 8080, | ||
url: process.env.URL || 'http://0.0.0.0:$(http.port)' | ||
} | ||
}; | ||
``` | ||
--- | ||
**MIT Licensed** |
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
4939
67
0
71
6
5