Comparing version 1.0.5 to 1.0.6
{ | ||
"name": "configly", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Provides an easy way to set up environmental configuration", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
184
README.md
@@ -8,6 +8,6 @@ # configly | ||
A simple configuration management module used for [node](http://nodejs.org/) | ||
A simple configuration management module used for [node.js](http://nodejs.org/) | ||
projects. | ||
# Installation | ||
## Installation | ||
@@ -18,54 +18,83 @@ ```bash | ||
# Usage | ||
## Usage | ||
```javascript | ||
var config = require('configly'); | ||
``` | ||
1. After installing the `configly` package (see above), Create a directory in | ||
the root of your project called `config`. | ||
Create a directory in the root of your project called `config`. | ||
1. Put configuration files in your newly created `config` directory | ||
* Filenames should be all lowercase letters with words separated by `-` | ||
* The configuration files should be a `.json` file or `.js` file | ||
* `.js` files should `module.exports = ` the config object | ||
* Environment config files should be named as such: | ||
`env.[environmentName].json` or `env.[environmentName].js`. | ||
* The default environment is `development`. | ||
* To use a different environment config file, start your node app like this: | ||
```bash | ||
NODE_ENV=production node app | ||
``` | ||
replacing `production` with the environment you wish be in. | ||
Every `.js` and `.json` in that directory will be included in the config object | ||
returned. | ||
1. Include the config object in your files: | ||
```javascript | ||
var config = require('configly'); | ||
``` | ||
For example: if you create a file called `lib.json` with these contents... | ||
The config object return will reflect the data put into your configuration | ||
files. | ||
```javascript | ||
{ | ||
"foo": "bar" | ||
} | ||
Each of the files is a property attached to the `config` object. The | ||
property name is a camelCased version of the name (e.g. filename = | ||
`foo-bar.json`, config = `{ fooBar: ... }` see example below). | ||
The environment config gets put in as the `env` property. | ||
## Example | ||
<!-- | ||
│ | ||
└ | ||
├ | ||
─ | ||
--> | ||
Imagine a directory structure like this: | ||
``` | ||
project/ | ||
├─ config/ | ||
│ ├─ env.development.json | ||
│ ├─ env.production.json | ||
│ ├─ user-permissions.json | ||
│ └─ email.js | ||
├─ node_modules/ | ||
│ └─ configly/... | ||
├─ package.json | ||
└─ app.js | ||
``` | ||
...the final config object will look like this... | ||
`config/env.development.json` | ||
```javascript | ||
{ | ||
lib: { | ||
foo: 'bar' | ||
} | ||
"port": "3000", | ||
"cachAge": 0 | ||
} | ||
``` | ||
If you use a `.js` file instead of a `.json` file, then you just need to make | ||
sure you put stuff in the `module.exports` object. The above example in `.js` | ||
form... | ||
`config/env.production.json` | ||
```javascript | ||
module.exports = { | ||
lib: { | ||
foo: 'bar | ||
} | ||
}; | ||
{ | ||
"port": "80", | ||
"cacheAge": 86000 | ||
} | ||
``` | ||
If you have a multi-word filename, only use dashes and underscores to separate | ||
the words. This is because the filename is converted to camelCase. | ||
`config/user-permissions.json` | ||
For example: if you create a file called `user-permissions.json` with these | ||
content... | ||
```javascript | ||
{ | ||
"/": [ | ||
"anonymous", | ||
"admin" | ||
"admin", | ||
"anonymous" | ||
], | ||
@@ -78,39 +107,80 @@ "/admin": [ | ||
...the final config object will look like this... | ||
`config/email.js` | ||
```javascript | ||
{ | ||
userPermissions: { | ||
'/': [ | ||
'anonymous', | ||
'admin' | ||
], | ||
'/admin': [ | ||
'admin' | ||
] | ||
} | ||
} | ||
'use strict'; | ||
var emailConfig = {}; | ||
emailConfig.user = 'email@email.com'; | ||
emailConfig.password = 'my super secure password'; | ||
module.exports = emailConfig; | ||
``` | ||
## Environment | ||
`app.js` | ||
To signify that a config file is an environment config file, use this naming | ||
convention: | ||
```javascript | ||
'use strict'; | ||
var config = require('configly'); | ||
console.log(config); | ||
``` | ||
env.[environment name].js(on) | ||
Alright, now with that setup, we run this command: | ||
```bash | ||
$ node app | ||
``` | ||
The `environment name` should be the name of the environment you want the config | ||
file to be for. | ||
We get this output: | ||
For example, if it's for a development environment, you should name the config | ||
file `env.development.json` or `env.development.js`. | ||
``` | ||
{ email: | ||
{ user: 'email@email.com', | ||
password: 'my super secure password' }, | ||
env: { port: '3000', cachAge: 0 }, | ||
userPermissions: { '/': [ 'admin', 'anonymous' ], '/admin': [ 'admin' ] } } | ||
``` | ||
To change the environment used, you just need to specify the `NODE_ENV` variable | ||
when running your app. E.g. `NODE_ENV=production node app` | ||
But when we run this command: | ||
If no `NODE_ENV` variable is provided, it will default to `development`, so you | ||
should always have an `env.development.json` file | ||
```bash | ||
$ NODE_ENV=production node app | ||
``` | ||
We get this ouput: | ||
``` | ||
{ email: | ||
{ user: 'email@email.com', | ||
password: 'my super secure password' }, | ||
env: { port: '80', cacheAge: 86000 }, | ||
userPermissions: { '/': [ 'admin', 'anonymous' ], '/admin': [ 'admin' ] } } | ||
``` | ||
Notice the only change was in the environment variable. I don't know about you, | ||
but this is super handy, because now deployment becomes a breeze. | ||
Any file you add to the `config` directory will automatically be added to the | ||
config object. No need to include it in some master config file. | ||
Also, no 3rd party dependencies. The only core dependencies it has are `fs` and | ||
`path`. | ||
## Troubleshooting | ||
If there is some behavior that isn't expected, like the config object isn't | ||
in the format you expected, try `console.log` on the config object. | ||
If there is no `config` directory in what is considered your 'current working | ||
directory' (this can be found by `process.cwd()`), then the config object will | ||
be a javascript `Error` object. | ||
If there was an error parsing the `.json` or `.js`, then the property that it | ||
was supposed to be in will be a `SyntaxError` object. | ||
Any other issues, please report to this repo's issues on GitHub. | ||
## Caveats | ||
@@ -117,0 +187,0 @@ |
Sorry, the diff of this file is not supported yet
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
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
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
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 7 instances in 1 package
194
5
1
11948
5
143