-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.js
128 lines (111 loc) · 2.81 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const program = require('commander')
const fs = require('fs')
const _ = require('lodash')
const path = require('path')
const uuid = require('short-uuid')
const memCache = require('./utils/mem_cache')
const { getDataFromEnvFile } = require('./utils/envs')
const taskfile = require('./taskfile')
const RC_FILE_NAME = '.flamebirdrc'
const RC_FILE_PATH = path.resolve(__dirname, `../${RC_FILE_NAME}`)
const DEFAULT_CONFIG = {
configs: [],
}
const getRC = () => {
let config = {}
try {
config = JSON.parse(fs.readFileSync(RC_FILE_PATH))
if (!config) {
throw new Error("Wrong flamebird rc file. Let's create new")
}
} catch (e) {
config = DEFAULT_CONFIG
updateRC(config)
}
return config
}
const updateRC = rc => {
// console.log('updateRC', rc)
fs.writeFileSync(RC_FILE_PATH, JSON.stringify(rc))
}
const refreshRC = () => {
console.log('refreshRC')
const rc = getRC()
memCache.set('rc-snapshot', rc)
return rc
}
const createConfig = (
{
ignoreTrs,
name,
port,
tasks,
taskRunner,
package: useOnlyPackageJson,
withoutBrowser,
sortByName,
},
isWeb
) => {
const rc = getRC()
console.log('createConfig')
const config = {
main: !rc.configs || !rc.configs.length,
id: memCache.set('id', uuid.generate()),
pid: process.pid,
path: path.resolve(),
withoutTaskRunner: !!ignoreTrs,
name: name,
port: +port,
tasks: _.compact(_.split(tasks, /,/g)),
taskRunner: taskRunner,
useOnlyPackageJson: !!useOnlyPackageJson,
web: !!isWeb,
withoutBrowser: !!withoutBrowser,
sortByName: !!sortByName,
envs: getDataFromEnvFile(program.env),
}
if (config.web) {
const rcSnapshot = memCache.set('rc-snapshot', {
configs: [
...rc.configs,
_.merge(config, {
commands: taskfile.load(config, program.procfile),
}),
],
})
updateRC({
configs: _.map(rcSnapshot.configs, config => ({
...config,
commands: _.map(config.commands, command =>
_.merge(command, { logs: [] })
),
})),
})
}
return config
}
const findConfig = findHandler =>
_.find(_.get(memCache.get('rc-snapshot'), 'configs', []), findHandler) || null
const getConfig = id => (id ? findConfig({ id }) : getMainConfig())
const getMainConfig = () => findConfig({ main: true })
const isMainConfig = () => memCache.get('config').main
const removeConfig = configId => {
const rcSnapshot = memCache.get('rc-snapshot')
const configIndex = rcSnapshot.configs.findIndex(
config => configId === config.id
)
if (configIndex > -1) rcSnapshot.configs.splice(configIndex, 1)
memCache.set('rc-snapshot', rcSnapshot)
updateRC(rcSnapshot)
}
module.exports = {
getRC,
refreshRC,
getMainConfig,
getConfig,
isMainConfig,
createConfig,
removeConfig,
updateRC,
}