-
Notifications
You must be signed in to change notification settings - Fork 48
/
config.js
93 lines (74 loc) · 2.09 KB
/
config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var path = require('path')
var url = require('url')
var _ = require('lodash')
var log = require('npmlog')
var mkdirp = require('mkdirp')
var ports = require('ports')
var randomString = require('random-string')
module.exports = function (options) {
var projectPath = options.path || process.cwd()
var pkg = require(path.join(projectPath, 'package.json'))
var config = {
name: pkg.name,
plugins: require('./plugins/load')(
_.get(pkg, 'hoodie.plugins') || [],
projectPath
),
paths: {
data: options.data || path.join(projectPath, 'data'),
project: projectPath,
www: options.www || path.join(projectPath, 'www')
}
}
config.hooks = require('./hooks')(config.plugins)
_.each(_.values(config.paths), _.ary(mkdirp.sync, 1))
var bindAddress = options.bindAddress || '127.0.0.1'
var configStore = require('./config-store')(config.paths.data)
var id
if (options.id) {
id = options.id
configStore.set('id', options.id)
} else {
id = configStore.get('id')
}
if (!id) {
id = randomString()
configStore.set('id', id)
}
config.app = {
hostname: bindAddress,
port: options.port || ports.getPort(id + '-app'),
protocol: 'http'
}
config.admin = {
password: options.adminPassword
}
if (options.dbUrl) {
config.db = url.parse(options.dbUrl)
return config
}
config.db = {
hostname: bindAddress,
port: options.dbPort || ports.getPort(id + '-db'),
protocol: 'http',
start: true,
inMemory: options.inMemory
}
config.db.auth = configStore.get('dbAuth')
if (config.db.auth && options.dbPassword) {
log.warn('config', 'Database password already set')
}
if (!config.db.auth) {
config.db.password = options.dbPassword || randomString()
config.db.auth = '_hoodie:' + config.db.password
configStore.set('dbAuth', config.db.auth)
}
if (!config.db.auth) {
throw new Error('Authentication details missing from database URL')
}
if (!config.db.password) {
config.db.password = config.db.auth.split(':')[1]
}
require('deep-freeze')(config)
return config
}