1 |
|
2 | const GLOBAL_CONF_NAME = 'GLOBAL_CONF_NAME'
|
3 | const _ = require('lodash')
|
4 | const IP = require('../util/IP')
|
5 | const path = require('path')
|
6 |
|
7 | let allConfig = {}
|
8 |
|
9 | let get = (hostname = 'localhost', port) => {
|
10 | const host = port === 443 ? ('https://' + hostname) : ('http://' + hostname + ':' + port).replace(':80', '')
|
11 |
|
12 | if (hostname === IP) {
|
13 | hostname = 'localhost'
|
14 | }
|
15 |
|
16 | const key = `${hostname}:${port}`
|
17 | |
18 |
|
19 |
|
20 | let conf = allConfig[port] || allConfig[key] || allConfig[GLOBAL_CONF_NAME]
|
21 | if (!conf) {
|
22 | const root = _.get(global[GLOBAL_CONF_NAME], ['' + port, hostname]) ||
|
23 | _.get(global[GLOBAL_CONF_NAME], ['' + port, 'localhost']) ||
|
24 | process.cwd()
|
25 | conf = {
|
26 | root,
|
27 | livereload: true,
|
28 | buildFilter: (pathname, data) => !/node_modules|([\\/]|^)\./.test(pathname),
|
29 | page_404: path.join(__dirname, '../../pages/404.html'),
|
30 | page_50x: path.join(__dirname, '../../pages/50x.html'),
|
31 | page_dir: path.join(__dirname, '../../pages/dir.html'),
|
32 | app: 'memory-tree'
|
33 | }
|
34 | Object.assign(conf, require('./conf')({
|
35 | root,
|
36 | host,
|
37 | port
|
38 | }))
|
39 |
|
40 | if (_.isString(conf.app)) {
|
41 | conf.app = require(`../apps/${conf.app}`)(conf)
|
42 | }
|
43 | if (conf.no_host) {
|
44 | allConfig[port] = conf
|
45 | } else {
|
46 | allConfig[key] = conf
|
47 | }
|
48 | }
|
49 | return conf
|
50 | }
|
51 | get.GLOBAL_CONF_NAME = GLOBAL_CONF_NAME
|
52 | module.exports = get
|