UNPKG

1.63 kBJavaScriptView Raw
1// @ts-check
2const GLOBAL_CONF_NAME = 'GLOBAL_CONF_NAME'
3const _ = require('lodash')
4const IP = require('../util/IP')
5const path = require('path')
6
7let allConfig = {}
8
9let 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 * @type {import('../../index').F2EConfig & { app: string }}
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}
51get.GLOBAL_CONF_NAME = GLOBAL_CONF_NAME
52module.exports = get