0.2.3 • Published 7 years ago

confactory v0.2.3

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

confactory

Sugar-interface to create configuration factories quickly

js-standard-style

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

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.10

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago