configurate
Set up workflow to load existing or default config, allow user to edit it and then serializes it to disk.
simple.js
var path = rmrf = configurate = log = promfig = ; var properties = user : 'Please enter your username : ' password : 'Please enter your password : ' '@secret' : 'password'; // see: https://github.com/thlorenz/promfigvar edit = promfig; var defaultConfig = path configDir = path configFile = 'configurate.js' ;
default config - cjs-partial
:
moduleexports = default : true id : 1 numberOfDefaults : 2;
output:
➝ node examples/simple.js
info configurator created config dir at: /Users/thlorenz/dev/js/projects/configurate/examples/config
Please enter your username : thlorenz
Please enter your password : ********************************************
info done config:
info done { default: true,
info done id: 1,
info done numberOfDefaults: 2,
info done user: 'thlorenz',
info done password: 'superlongandsecretpasswordthatiuseeverywhere' }
info done stored at: /Users/thlorenz/dev/js/projects/configurate/examples/config/configurate.js
Installation
npm install configurate
API
function configurate (opts:Object, cb:Function) -> EventEmitter
-
opts allows overriding invoked functions and other options
- paths
configDir {String}
directory in which the config file resides (default$HOME
) if it is relative, it is created relative to$HOME
configFile {String}
name of the config file which is combined with the configDir to build full config file pathdefaultConfig {String}
full path to default config to load in case the config is not found at the config path
- functions
- each function may take a second parameter (a callback) in order to process asynchronously
load {Function}
called with full path to config, default isrequire('..')
edit {Function}
called with loaded config object, by default config is not editedserialize {Function}
called with edited config object, default creates'module.exports = { ... }'
- paths
-
cb function (err:Error, config:Object, configPath:String)
err
is set if something went wrongconfig
object that was saved including all edits, etc.configPath
full path to file into which the config was saved
-
EventEmitter emits the following events:
created-configdir
with the path to the created dircopied-default
with the path to the default config that was copied and the path to which it was copied toloaded-config
with the loaded confignotfound-config
with the object used to configure from scratchedited-config
with the edited config objectserialized-config
with the serialized configwrote-config
with the path that the config was written to
Possible configuration workflows
Workflows are demonstrated in the provided examples and tests.
config exists
- mkdir
- load config
- edit loaded config
- serialize edited config
- write config
config doesn't exist and default config is given
- mkdir
- copy default config
- load copied config
- edit loaded config
- serialize edited config
- write config
config doesn't exist and no default config is given
- mkdir
- edit config starting from scratch ( {} )
- serialize edited config
- write config
License
MIT