Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

app-etc

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

app-etc - npm Package Compare versions

Comparing version 0.0.0 to 0.0.1

lib/argv.js

15

lib/defaults.json
{
"etc": "./etc",
"local": "./etc",
"defaultsFile": "defaults",
"etc": "/etc",
"etcFile": "",
"env": "dev",
"envFile": "env",
"argvFile": "argv",
"user": "",
"userFile": "",
"env": "dev",
"envFile": "env",
"order": [
"defaults",
"app",
"local",
"user",
"app",
"env"
"env",
"argv"
]
}

@@ -5,5 +5,3 @@ 'use strict';

var deepSet = require( 'utils-deep-set' ),
parseJSON = require( 'utils-json-parse' ),
isnan = require( 'validate.io-nan' ),
var env2obj = require( 'env-to-object' ),
load = require( './load.js' );

@@ -23,64 +21,7 @@

function env( dir, basename ) {
var keys,
obj,
out,
len,
val,
o,
i;
obj = load( dir, basename );
if ( obj === null ) {
var map = load( dir, basename );
if ( map === null ) {
return null;
}
keys = Object.keys( obj );
len = keys.length;
out = {};
for ( i = 0; i < len; i++ ) {
o = obj[ keys[ i ] ];
val = process.env[ keys[ i ] ];
if ( val === void 0 ) {
continue;
}
if ( o.type === 'number' ) {
val = parseFloat( val );
if ( isnan( val ) ) {
throw new TypeError( 'invalid value. ' + keys[ i ] + ' environment variable must be a number. Value: `' + val + '`.' );
}
}
else if ( o.type === 'boolean' ) {
if (
val === 'true' ||
val === 'TRUE' ||
val === 'True' ||
val === 'T' ||
val === 't'
) {
val = true;
}
else if (
val === 'false' ||
val === 'FALSE' ||
val === 'False' ||
val === 'F' ||
val === 'f'
) {
val = false;
}
else {
throw new TypeError( 'invalid value. ' + keys[ i ] + ' environment variable must be a boolean. Value: `' + val + '`.' );
}
}
else if ( o.type === 'object' ) {
val = parseJSON( val );
if ( val instanceof Error ) {
throw new TypeError( 'invalid value. ' + keys[ i ] + ' environment variable must be a valid JSON object. Value: `' + val + '`.' );
}
}
deepSet( out, o.keypath, val, {
'create': true,
'sep': '.'
});
}
return out;
return env2obj( map );
} // end FUNCTION env()

@@ -87,0 +28,0 @@

@@ -5,10 +5,13 @@ 'use strict';

var path = require( 'path' ),
var debug = require( 'debug' )( 'app-etc:main' ),
path = require( 'path' ),
merge = require( 'utils-merge2' )(),
root = require( 'resolve-app-path' )(),
pkg = require( 'resolve-app-pkginfo' ).sync(),
configdir = require( 'utils-configdir' ),
createConfig = require( 'app-etc-config' ),
validate = require( './validate.js' ),
load = require( './load.js' ),
envVars = require( './env.js' );
envVars = require( './env.js' ),
argv = require( './argv.js' );

@@ -28,9 +31,12 @@

* @param {Object} [options] - function options
* @param {String} [options.etc='./etc'] - application configuration directory
* @param {String} [options.defaultsFile='defaults'] - basename of a file within the application configuration directory which contains default application settings
* @param {String} [options.local='./etc'] - local application configuration directory
* @param {String} [options.defaultsFile='defaults'] - basename of a file within the local application configuration directory which contains default application settings
* @param {String} [options.etc="/etc"] - application configuration directory
* @param {String} [options.etcFile] - basename of a file within the application configuration directory which contains application settings
* @param {String} [options.user] - user configuration directory
* @param {String} [options.userFile] - basename of a file within the user configuration directory which contains user application settings
* @param {String} [options.env='dev'] - application runtime environment
* @param {String} [options.envFile='env'] - basename of a file within the application configuration directory which maps environment variables to application settings
* @param {String[]} [options.order=['defaults','user','app','env']] - defines the configuration hierarchy
* @param {String} [options.envFile='env'] - basename of a file within the local application configuration directory which maps environment variables to application settings
* @param {String} [options.argvFile='argv'] - basename of a file within the local application configuration directory which maps command-line arguments to application settings
* @param {String[]} [options.order=['defaults','app','local','user','env','argv']] - defines the configuration hierarchy
* @returns {Config} new Config instance

@@ -41,3 +47,4 @@ */

opts,
cdir,
ldir,
udir,
obj,

@@ -57,8 +64,11 @@ err,

opts = merge( {}, DEFAULTS, opts );
debug( 'Configuration options: %s', JSON.stringify( opts ) );
// Get the current runtime environment:
env = process.env.NODE_ENV || opts.env;
debug( 'Runtime environment: %s', env );
// Resolve the configuration directory:
cdir = path.resolve( root, opts.etc );
// Resolve the local configuration directory:
ldir = path.resolve( root, opts.local );
debug( 'Local configuration directory: %s', ldir );

@@ -69,2 +79,3 @@ // Create a new application configuration:

// Load configuration files...
debug( 'Loading configuration files.' );
for ( i = 0; i < opts.order.length; i++ ) {

@@ -74,18 +85,33 @@ x = opts.order[ i ];

if ( x === 'defaults' ) {
obj = load( cdir, opts.defaultsFile );
debug( 'Attempting to load default configuration settings.' );
obj = load( ldir, opts.defaultsFile );
}
else if ( x === 'app' ) {
debug( 'Attempting to load application-specific configuration settings.' );
obj = load( opts.etc, opts.etcFile || pkg.name );
}
else if ( x === 'local' ) {
debug( 'Attempting to load local application-specific configuration settings.' );
obj = load( ldir, env );
}
else if ( x === 'user' ) {
// TODO: OS specific config dirs
// obj = load( opts.user || null, opts.userFile || pkg.name );
debug( 'Attempting to load user-specific configuration settings.' );
udir = opts.user || configdir();
debug( 'User configuration directory: %s', udir );
obj = load( udir, opts.userFile || pkg.name );
}
else if ( x === 'app' ) {
obj = load( cdir, env );
}
else if ( x === 'env' ) {
obj = envVars( cdir, opts.envFile );
debug( 'Attempting to load environment variables.' );
obj = envVars( ldir, opts.envFile );
}
else if ( x === 'argv' ) {
debug( 'Attempting to load command-line arguments.' );
obj = argv( ldir, opts.argvFile );
}
if ( obj ) {
debug( 'Merging `%s` configuration settings.', x );
config.merge( obj );
}
}
debug( 'Finished loading configuration files.' );
return config;

@@ -92,0 +118,0 @@ } // end FUNCTION etc()

@@ -5,3 +5,4 @@ 'use strict';

var path = require( 'path' ),
var debug = require( 'debug' )( 'app-etc:load' ),
path = require( 'path' ),
extname = require( 'utils-extname' ),

@@ -12,2 +13,31 @@ exists = require( 'utils-fs-exists' ),

// FUNCTIONS //
/**
* FUNCTION: resolve( dir, basename )
* Attempts to resolve a file and its hidden version.
*
* @private
* @param {String} dir - configuration directory
* @param {String} basename - file basename
* @returns {Object|Null} configuration object or null
*/
function resolve( dir, basename ) {
var file;
if ( basename[ 0 ] !== '.' ) {
file = path.resolve( dir, '.'+basename );
if ( exists.sync( file ) ) {
debug( 'Found a configuration file: %s', file );
return load( file );
}
}
file = path.resolve( dir, basename );
if ( exists.sync( file ) ) {
debug( 'Found a configuration file: %s', file );
return load( file );
}
return null;
} // end FUNCTION resolve()
// LOAD FILE //

@@ -32,5 +62,6 @@

if ( ext ) {
file = path.resolve( dir, basename );
if ( exists.sync( file ) ) {
return load( file );
file = resolve( dir, basename );
if ( file !== null ) {
debug( 'Loaded a configuration file from `%s` directory with basename `%s`.', dir, basename );
return file;
}

@@ -42,8 +73,10 @@ } else {

file = basename + exts[ i ];
file = path.resolve( dir, file );
if ( exists.sync( file ) ) {
return load( file );
file = resolve( dir, file );
if ( file !== null ) {
debug( 'Loaded a configuration file from `%s` directory with basename `%s`.', dir, basename );
return file;
}
}
}
debug( 'No configuration file found in directory `%s` with basename `%s`.', dir, basename );
return null;

@@ -50,0 +83,0 @@ } // end FUNCTION loadFile()

@@ -18,4 +18,6 @@ 'use strict';

* @param {Object} options - options to validate
* @param {String} [options.local] - local application configuration directory
* @param {String} [options.defaultsFile] - basename of a file within the local application configuration directory which contains default application settings
* @param {String} [options.etc] - application configuration directory
* @param {String} [options.defaultsFile] - basename of a file within the application configuration directory which contains default application settings
* @param {String} [options.etcFile] - basename of a file within the application configuration directory which contains application settings
* @param {String} [options.user] - user configuration directory

@@ -25,2 +27,3 @@ * @param {String} [options.userFile] - basename of a file within the user configuration directory which contains user application settings

* @param {String} [options.envFile] - basename of a file within the application configuration directory which maps environment variables to application settings
* @param {String} [options.argvFile] - basename of a file within the local application configuration directory which maps command-line arguments to application settings
* @param {String[]} [options.order] - defines the configuration hierarchy

@@ -33,6 +36,6 @@ * @returns {Error|Null} error or null

}
if ( options.hasOwnProperty( 'etc' ) ) {
opts.etc = options.etc;
if ( !isString( opts.etc ) ) {
return new TypeError( 'invalid option. `etc` option must be a primitive string. Option: `' + opts.etc + '`.' );
if ( options.hasOwnProperty( 'local' ) ) {
opts.local = options.local;
if ( !isString( opts.local ) ) {
return new TypeError( 'invalid option. `local` option must be a primitive string. Option: `' + opts.local + '`.' );
}

@@ -46,2 +49,14 @@ }

}
if ( options.hasOwnProperty( 'etc' ) ) {
opts.etc = options.etc;
if ( !isString( opts.etc ) ) {
return new TypeError( 'invalid option. `etc` option must be a primitive string. Option: `' + opts.etc + '`.' );
}
}
if ( options.hasOwnProperty( 'etcFile' ) ) {
opts.etcFile = options.etcFile;
if ( !isString( opts.etcFile ) ) {
return new TypeError( 'invalid option. `etcFile` option must be a primitive string. Option: `' + opts.etcFile + '`.' );
}
}
if ( options.hasOwnProperty( 'user' ) ) {

@@ -71,2 +86,8 @@ opts.user = options.user;

}
if ( options.hasOwnProperty( 'argvFile' ) ) {
opts.argvFile = options.argvFile;
if ( !isString( opts.argvFile ) ) {
return new TypeError( 'invalid option. `argvFile` option must be a primitive string. Option: `' + opts.argvFile + '`.' );
}
}
if ( options.hasOwnProperty( 'order' ) ) {

@@ -73,0 +94,0 @@ opts.order = options.order;

{
"name": "app-etc",
"version": "0.0.0",
"version": "0.0.1",
"description": "Application configuration.",

@@ -28,8 +28,13 @@ "author": {

"application",
"server",
"service",
"microservice",
"etc",
"conf",
"config",
"configure",
"configuration",
"settings",
"env",
"environment",
"env",
"load",

@@ -45,11 +50,12 @@ "parse",

"app-etc-config": "0.0.0",
"app-etc-load": "0.0.0",
"app-etc-load": "0.0.2",
"argv-to-object": "1.x.x",
"debug": "^2.2.0",
"env-to-object": "1.x.x",
"resolve-app-path": "^1.0.2",
"resolve-app-pkginfo": "^1.0.0",
"utils-deep-set": "^1.0.1",
"utils-configdir": "^1.0.0",
"utils-extname": "^1.0.0",
"utils-fs-exists": "^1.0.1",
"utils-json-parse": "^1.0.0",
"utils-merge2": "^1.0.0",
"validate.io-nan": "^1.0.3",
"validate.io-object": "^1.0.4",

@@ -56,0 +62,0 @@ "validate.io-string-array": "^1.0.0",

@@ -31,22 +31,28 @@ etc

* __etc__: application configuration directory. Default: `./etc`.
* __defaultsFile__: basename of a file within the application configuration directory which contains *default* application settings. Default: `defaults`.
* __user__: user configuration directory. The default value is determined according to the host OS.
* __local__: local application configuration directory. Default: [`./etc`](https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard).
* __defaultsFile__: basename of a file within the *local* application configuration directory which contains *default* application settings. Default: `defaults`.
* __etc__: application configuration directory. Default: [`/etc`](https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard).
* __etcFile__: basename of a file within an application configuration directory which contains application settings. The default value is the application [name](https://github.com/kgryte/resolve-app-pkginfo).
* __user__: user configuration directory. The [default value](https://github.com/kgryte/utils-configdir) is determined according to the host OS.
* __userFile__: basename of a file within the user configuration directory which contains *user* application settings. The default value is the application [name](https://github.com/kgryte/resolve-app-pkginfo).
* __env__: application runtime environment. Default: `dev`.
* __envFile__: basename of a file within the application configuration directory which maps environment variables to application settings. Default: `env`.
* __order__: defines the configuration hierarchy. Default: `['defaults','user','app','env']`.
* __envFile__: basename of a file within the *local* application configuration directory which [maps](https://github.com/kgryte/node-env-to-object) environment variables to application settings. Default: `env`.
* __argvFile__: basename of a file within the *local* application configuration directory which [maps](https://github.com/kgryte/node-argv-to-object) command-line arguments to application settings. Default: `argv`.
* __order__: defines the configuration hierarchy. Default: `['defaults','app','local','user','env','argv']`.
__Note__: if a file extension is omitted when specifying file basenames, this module will search for the first file having the basename and a supported extension. For supported extensions, see [app-etc-load](https://github.com/kgryte/node-app-etc-load).
__Notes__:
* If a file extension is omitted when specifying file basenames, this module will search for the first file having the basename and a supported extension. For supported extensions, see [app-etc-load](https://github.com/kgryte/node-app-etc-load).
* If a file basename does __not__ begin with a `.`, this module will search for both hidden and non-hidden files. This also applies for inferred basenames; e.g., __env__. If `env` is `dev`, this module will search for and load either an `.dev.<ext>` or a `dev.<ext>` file.
##### Configuration Directory
By default, the application configuration directory is a directory named `etc` located in the application's [root](https://github.com/kgryte/resolve-app-path) directory. This directory may contain default configuration settings, mappings between environment variables and configuration settings, various application-specific configuration files tailored for different runtime environments, and more. To specify a different directory, set the `etc` option:
##### Local Configuration Directory
By default, the __local__ application configuration directory is a directory named [`etc`](https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard) located in the application's [root](https://github.com/kgryte/resolve-app-path) directory. This directory may contain default configuration settings, mappings between environment variables and configuration settings, various application-specific configuration files tailored for different runtime environments, and more. To specify a different directory, set the `local` option:
``` javascript
var config = etc({
'etc': './config'
'local': './config'
});

@@ -67,9 +73,28 @@ ```

##### Application Configuration
The __etc__ directory option specifies the location of a directory containing application-specific configuration files. The default value is [`/etc`](https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard), but this may __not__ apply for all operating systems (e.g., [Windows](http://stackoverflow.com/questions/2359338/windows-equivalent-of-system-configuration-directory)). To specify a different directory, set the `etc` option:
``` javascript
var config = etc({
'etc': '/config'
});
```
An __etc__ file should contain application-specific configuration settings. By default, this module searches for a file having a basename equal to the application name. To specify a different basename, set the `etcFile` option:
``` javascript
var config = etc({
'etcFile': 'appname/config.alce'
});
```
##### User Configuration
The __user__ directory option specifies the location of a directory containing [user-specific](http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html) configuration files. The location of this directory is typically OS specific. To specify a directory, set the `user` option:
The __user__ directory option specifies the location of a directory containing [user-specific](https://github.com/kgryte/utils-configdir) configuration files. The location of this directory is typically OS specific. To specify a directory, set the `user` option:
``` javascript
var config = etc({
'user': '/Users/<name>/Library/Preferences'
'user': '~/'
});

@@ -82,3 +107,3 @@ ```

var config = etc({
'userFile': 'configgie.json'
'userFile': '.appname.json'
});

@@ -90,3 +115,3 @@ ```

Often different runtime environments require different application configurations. For example, in `development`, the application may connect to local resources; whereas, in `production`, the application may connect to various remote endpoints. To handle the different runtimes, applications will utilize environment specific configuration files; e.g., `production.json`, `development.json`, `test.json`, `local.json`, etc. This module sets the default runtime environment to `dev` and looks for a corresponding configuration file of the same name in the application configuration directory. To override this option, either set the `NODE_ENV` [environment variable](https://en.wikipedia.org/wiki/Environment_variable) or set the `env` option:
Often different runtime environments require different application configurations. For example, in `development`, the application may connect to local resources; whereas, in `production`, the application may connect to various remote endpoints. To handle the different runtimes, applications will utilize environment specific configuration files; e.g., `production.json`, `development.json`, `test.json`, `local.json`, etc. This module sets the default runtime environment to `dev` and looks for a corresponding configuration file of the same name in the __local__ application configuration directory. To override this option, either set the `NODE_ENV` [environment variable](https://en.wikipedia.org/wiki/Environment_variable) or set the `env` option:

@@ -99,3 +124,3 @@ ``` javascript

Runtime environments (e.g., [containers](https://docs.docker.com/reference/run/#env-environment-variables)) frequently use [environment variables](https://en.wikipedia.org/wiki/Environment_variable) for configuration. To map [environment variables](https://en.wikipedia.org/wiki/Environment_variable) to configuration settings, this module searches the application configuration directory for a file which maps each [environment variable](https://en.wikipedia.org/wiki/Environment_variable) to a particular setting. By default, this module looks for a file having the basename `env`. To specify a different basename, set the `envFile` option:
Runtime environments (e.g., [containers](https://docs.docker.com/reference/run/#env-environment-variables)) frequently use [environment variables](https://en.wikipedia.org/wiki/Environment_variable) for configuration. To map [environment variables](https://en.wikipedia.org/wiki/Environment_variable) to configuration settings, this module searches the __local__ application configuration directory for a file which maps each [environment variable](https://en.wikipedia.org/wiki/Environment_variable) to a particular setting. By default, this module looks for a file having the basename `env`. To specify a different basename, set the `envFile` option:

@@ -108,3 +133,3 @@ ``` javascript

The file contents should include each relevant [environment variable](https://en.wikipedia.org/wiki/Environment_variable) and a corresponding setting. For example, a JSON mapping file:
The file contents should include each relevant [environment variable](https://en.wikipedia.org/wiki/Environment_variable) and a corresponding setting. For example, a JSON [mapping](https://github.com/kgryte/node-env-to-object) file:

@@ -133,3 +158,3 @@ ``` javascript

A TOML mapping file:
A TOML [mapping](https://github.com/kgryte/node-env-to-object) file:

@@ -160,26 +185,60 @@ ``` toml

__Notes__:
See [env-to-object](https://github.com/kgryte/node-env-to-object) for more information. Note that, if an [environment variable](https://en.wikipedia.org/wiki/Environment_variable) cannot be cast as a specified type, the module __will__ throw an `error`.
* A configuration setting is specified by a `keypath`.
* Nested configuration setting `keypaths` __must__ be `.` separated.
* A configuration setting type may be specified by providing a `type`. Possible `types `include:
- `string` (default)
- `number`
- `boolean`
- `object`
* If an [environment variable](https://en.wikipedia.org/wiki/Environment_variable) cannot be cast as a specified type, the module __will__ throw an `error`.
* If an [environment variable](https://en.wikipedia.org/wiki/Environment_variable) does __not__ exist, the module __skips__ that variable.
When scripting or running an application from the command-line, command-line arguments are commonly used to configure an application. To [map](https://github.com/kgryte/node-argv-to-object) command-line arguments to configuration settings, this module searches the __local__ application configuration directory for a file which [maps](https://github.com/kgryte/node-argv-to-object) each command-line argument to a particular setting. By default, this module looks for a file having the basename `argv`. To specify a different basename, set the `argvFile` option:
``` javascript
var config = etc({
'argvFile': 'argv_mapping'
});
```
The file contents should include each relevant command-line argument and a corresponding setting. For example, a JSON [mapping](https://github.com/kgryte/node-argv-to-object) file:
``` javascript
{
"api-key": {
"keypath": "gKey"
},
"loglevel": {
"keypath": "logger.level",
"default": "info"
},
"port": {
"keypath": "server.port",
"type": "integer",
"alias": [
"p"
]
},
"ssl": {
"keypath": "server.ssl",
"type": "boolean"
},
"key": {
"keypath": "server.key",
"type": "string"
},
"cert": {
"keypath": "server.cert",
"type": "string"
}
```
See [argv-to-object](https://github.com/kgryte/node-argv-to-object) for more information. Note that, if a command-line argument cannot be cast as a specified type, the module __will__ throw an `error`.
##### Configuration Hierarchy
Configuration sources are many; e.g., user-specific, application-specific, [environment variables](https://en.wikipedia.org/wiki/Environment_variable), and more. The following sources are supported:
Configuration sources are many; e.g., user-specific, application-specific, runtime-specific, [environment variables](https://en.wikipedia.org/wiki/Environment_variable), command-line arguments, and more. The following sources are supported:
* __defaults__: default application settings
* __app__ : application-specific settings
* __local__: local application-specific settings
* __user__: user-specific settings
* __app__ : application-specific settings
* __env__: [environment variable](https://en.wikipedia.org/wiki/Environment_variable) runtime settings
* __argv__: command-line arguments
The `order` option exists to impose a configuration hierarchy. By default, the hierarchy is
The `order` option exists to impose a configuration hierarchy. By default, the hierarchy is biased toward Linux systems:

@@ -189,5 +248,7 @@ ``` javascript

'defaults', // read first
'app',
'local',
'user',
'app',
'env' // read last
'env',
'argv' // read last
]

@@ -199,6 +260,6 @@ ```

``` javascript
// Only use application and environment variables as configuration sources...
// Only use local application configuration and environment variables as configuration sources...
var config = etc({
'order': [
'app',
'local',
'env'

@@ -250,3 +311,3 @@ ]

var config = etc({
'etc': path.join( __dirname, 'etc' )
'local': path.join( __dirname, 'etc' )
});

@@ -259,3 +320,3 @@ console.dir( config.get() );

'address': '127.0.0.1',
'ssl': false,
'ssl': true,
'key': '',

@@ -275,3 +336,3 @@ 'cert': ''

``` bash
$ NODE_ENV=dev PORT=8080 node ./examples/index.js
$ DEBUG=* NODE_ENV=dev PORT=8080 node ./examples/index.js --ssl
```

@@ -278,0 +339,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc