UNPKG

1.79 kBJavaScriptView Raw
1import _ from 'lodash'
2import fs from 'fs'
3import path from 'path'
4//import Debug from 'debug'
5//const debug = Debug('configurize:get-root-config')
6
7export function getConfig(vent) {
8 let {module, file} = getConfigModule()
9 const rootConf = parseConfig(module)
10 vent.emit('root-config', rootConf, file)
11 return rootConf || {}
12}
13
14const toolsDir = 'tools'
15
16function getConfigModule() {
17 let file
18 let module
19 // TODO(vjpr): Check this don't break nothing!
20 // We might have to add it to webpack config.
21 if (global.__CLIENT__ === true) {
22 // TODO(vjpr): This method is only for server-side.
23 // file = getBrowserConfigPath()
24 // module = require(file)
25 module = require('configurize.browser.js')
26 } else {
27 file = getServerConfigPath()
28 module = fs.existsSync(file) ? require(file) : null
29 }
30 return {file, module}
31}
32
33function parseConfig(module) {
34 const opts = {}
35 return _.isFunction(module) ? module(opts) : module
36}
37
38// TODO(vjpr): Use the strategy that webpackerator and babelator use for finding the config path.
39export function getServerConfigPath() {
40
41 if (global.__CLIENT__ !== true) {
42
43 const envPath = process.env.CONFIGURIZE_CONFIG_PATH
44 if (_.get(process, 'versions.electron')) {
45 // NOTE: require('electron') will only exist in an Electron app.
46 return envPath || path.join(require('electron').app
47 .getAppPath(), toolsDir, '.configurize.js')
48 } else {
49 const cwd = require('cwd')
50 return envPath || cwd(toolsDir, '.configurize.js')
51 }
52
53 }
54
55}
56
57// NOTE: This is for server-side usage for generating our webpack config.
58export function getBrowserConfigPath() {
59 const cwd = require('cwd')
60 return require.resolve(process.env.CONFIGURIZE_BROWSER_CONFIG_PATH || cwd(toolsDir, '.configurize.browser.js'))
61}