1 | import _ from 'lodash'
|
2 | import fs from 'fs'
|
3 | import path from 'path'
|
4 |
|
5 |
|
6 |
|
7 | export 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 |
|
14 | const toolsDir = 'tools'
|
15 |
|
16 | function getConfigModule() {
|
17 | let file
|
18 | let module
|
19 |
|
20 |
|
21 | if (global.__CLIENT__ === true) {
|
22 |
|
23 |
|
24 |
|
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 |
|
33 | function parseConfig(module) {
|
34 | const opts = {}
|
35 | return _.isFunction(module) ? module(opts) : module
|
36 | }
|
37 |
|
38 |
|
39 | export function getServerConfigPath() {
|
40 |
|
41 | if (global.__CLIENT__ !== true) {
|
42 |
|
43 | const envPath = process.env.CONFIGURIZE_CONFIG_PATH
|
44 | if (_.get(process, 'versions.electron')) {
|
45 |
|
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 |
|
58 | export function getBrowserConfigPath() {
|
59 | const cwd = require('cwd')
|
60 | return require.resolve(process.env.CONFIGURIZE_BROWSER_CONFIG_PATH || cwd(toolsDir, '.configurize.browser.js'))
|
61 | }
|