confactory
v0.2.3
Published
Sugar-interface to create configuration factories quickly
Downloads
24
Maintainers
Readme
confactory
Sugar-interface to create configuration factories quickly
Installation
npm install confactory --save
Example usage
// ----------------
// configFactory.js
// ----------------
const confactory = require('confactory')
const f = confactory()
.$target('dev', {prod: false})
.$target('prod', {prod: true})
.$target('client', {target: 'client'})
.$target('server', {target: 'server'})
.$helper('net.host')
.$helper('net.port')
f.net.host('localhost')
f.prod.client.net.host('client-host') // alias: f.client.prod.net.host()
f.prod.server.net.host('server-host') // alias: f.server.prod.net.host()
f.client.net.port(1337)
f.client.prod.net.port(443) // alias: f.prod.client.net.port()
// or in single delaration:
// f.client.prod.net.port(443, 1337)
f.server.net.port(3000)
f.prod.server.net.port(8080) // alias: f.server.prod.net.port()
// or in single declaration:
// f.prod.server.net.port(8080, 3000)
f.$hook((context, buildMeta, config) => {
config.mode = buildMeta.prod ? 'production' : 'development'
config.target = buildMeta.target
})
f.$hook((context, buildMeta, config) => {
const {net} = config
const protocol = net.port === 443 ? 'https' : 'http'
let url = `${protocol}://${net.host}`
if (net.port !== 443) {
url += ':' + net.port
}
config.url = url
})
f.$setWith('secret', (context, buildMeta, config) => {
return context.secret
})
module.exports = f.$clone
// ----------------
// main.js
// ----------------
const f = require('./configFactory.js')()
const show = console.log
show(f.client.$build({secret: 'foobar!'}))
// { net: { host: 'localhost', port: 1337 },
// mode: 'development',
// target: 'client',
// url: 'http://localhost:1337',
// secret: 'foobar!' }
show(f.client.prod.$build({secret: 'foobar!'}))
// { net: { host: 'client-host', port: 443 },
// mode: 'production',
// target: 'client',
// url: 'https://client-host',
// secret: 'foobar!' }
show(f.server.$build({secret: 'foobar!'}))
// { net: { host: 'localhost', port: 3000 },
// mode: 'development',
// target: 'server',
// url: 'http://localhost:3000',
// secret: 'foobar!' }
show(f.server.prod.$build({secret: 'foobar!'}))
// { net: { host: 'server-host', port: 8080 },
// mode: 'production',
// target: 'server',
// url: 'http://server-host:8080',
// secret: 'foobar!' }
License
MIT