load-config-folder
v2.0.0
Published
Load the config in the folder as a plain object.
Downloads
27
Maintainers
Readme
load-config-folder
Load the config from folder as a plain object.
- The config file format can be registered.
- The registered file format order is the search order.
- the virtual file system can be specfied:
loadConfig.setFileSystem(fs)
fs.path = require('path.js/lib/path').path
for the virtual POSIX path.- you must set the path first before call setFileSystem.
The load-config-folder
is similar to the load-config-file.
But it will load the configuration from the folder with a specified file name, you can use addConfig
to set the specified config file name(it's a basename
).
Usage
import loadConfig, { FolderConfig } from 'load-config-folder'
import yaml from 'js-yaml'
import cson from 'cson'
//the config file basename for the folder.
FolderConfig.addConfig(['_config']);
FolderConfig.register(['.yaml', '.yml'], yaml.safeLoad);
FolderConfig.register('.cson', cson.parseCSONString.bind(cson));
FolderConfig.register('.json', JSON.parse);
//Synchronously load config for the folder.
//it will search folder/_config.yaml, folder/_config.yml, folder/_config.cson, folder/_config.json
//the first exist file will be loaded.
//the default encoding is "utf8" if no encoding.
//loadConfig('config', {encoding: 'ascii'})
//the non-enumerable "$cfgPath" property added.
console.log(loadConfig('./folder'));
//Asynchronously load config from file
loadConfig('./folder', function(err, result){
if (err) {
console.log('error:', err);
} else {
console.log(result);
}
})
let result = await loadConfig('./folder', true)
result = await FolderConfig.load('./folder')
result = FolderConfig.loadSync('./folder')
API
import { FolderConfig } from 'load-config-folder'
FolderConfig.setFileSystem(fs)
: set your favour file system. defaults to 'fs'.- the "file system" must implement
readFile(path[, options], done)
andreadFileSync(path[, options])
- the "file system" must implement
load(dir, options, done)
: Asynchronously load config from dir- return the plain object and the
$cfgPath
property added if suceesful.
- return the plain object and the
loadSync(dir, options)
: Synchronously load config from dir- return the plain object and the
$cfgPath
property added if suceesful.
- return the plain object and the
License
MIT