Configue Little Demo

node v8.9.4
version: 1.0.1
endpointsharetweet
Here is a demo of the Configue library:
const Configue = require('configue'); undefined
We just required the library, before to use it in this toolkit we are going to modify the env, and argv to mimick some config we could have in real life
process.argv.push('--greeting=Hey'); process.env.TARGET='You'; process.env.GREETING='Salut';
Now we are all set, let's get our configue!
let configue = new Configue(); console.log(`${configue.get('greeting', 'Hello')} ${configue.getFirst(['target', 'TARGET'], 'World')}`); const {greeting,target} = configue.getObject('greeting', 'target');
You can also use the template fonction to populate a template string with values returned by keys
configue.t`This is a ${'greeting'} for you`;
Now with a more in depth examples showcasing some parsing, normalisationand nesting features of configue. Indeed configue, can uniformemize the name between the argv variable and env variable to a common case.
First I set up a bit more complex environment and argv parameters. This is only for doing a demo in RunKit.
/* argv and environment setup */ process.argv.push('--mongodb-url=mongodb://mongo-cloud:27017'); process.env.PORT=3003; process.env.MONGODB_URL='mongodb://localhost:27017' process.env.SLACK__BOT_TOKEN='azertyazertysquare' process.argv.push('--slack--api-token=qwertyqwertytwice'); // And far more in practice
Then I configure Configue to have access to the feature I want. (to do so, I use the fluent builder) And start to access them:
configue = Configue.parse(true) .normalize('camelCase') .separator(/__|--/) .get(); configue.get('port');
See the number as output of configue.get('port') and the use of 'port' key while env variable is PORT. This is because of the normalize and parse feature tha got activated. The same feature would unify the --mongodb-url command line flag with the env variable MONGODB_URL to the camelCase key mongodbUrl. In practice as you see command line flag taking precedence. over env variable.
configue.get('mongodbUrl')
Separator will split the key making key as object. In your example with have two of them with the slack prefix
configue.get('slack')
You can also access to a nested key using : or .
configue.get('slack:apiToken')
configue.get('slack.botToken')
This is just a sample of what can be done with Configue. For more details, have a look to the README at https://github.com/AdrieanKhisbe/configue =)
Loading…

no comments

    sign in to comment