1
|
|
|
import fse from 'fs-extra' |
2
|
|
|
import clc from 'cli-color' |
3
|
|
|
import extend from 'extend' |
4
|
|
|
import path from 'path' |
5
|
|
|
|
6
|
|
|
import config from './config.json' |
7
|
|
|
|
8
|
|
|
var result = config |
9
|
|
|
result.root = process.cwd() |
10
|
|
|
if(process.env.ROOT) { |
11
|
|
|
result.root = process.env.ROOT.replace(/\/$/, '') |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
var hintAbeJson = false |
15
|
|
|
|
16
|
|
|
var loadLocalConfig = (result) => { |
17
|
|
|
if(result.root !==''){ |
18
|
|
|
try{ |
19
|
|
|
var stat = fse.statSync(result.root) |
20
|
|
|
if (stat && stat.isDirectory()) { |
21
|
|
|
try{ |
22
|
|
|
stat = fse.statSync(path.join(result.root,'abe.json')) |
23
|
|
|
if (stat) { |
24
|
|
|
var json = fse.readJsonSync(path.join(result.root,'abe.json')) |
25
|
|
|
result = extend(true, result, json) |
|
|
|
|
26
|
|
|
} |
27
|
|
|
}catch(e) { |
28
|
|
|
if (!hintAbeJson) { |
29
|
|
|
hintAbeJson = true |
30
|
|
|
console.log( |
|
|
|
|
31
|
|
|
clc.green('[ Hint ]'), |
32
|
|
|
'you can create a specific config file named abe.json to customize your abe install', |
33
|
|
|
clc.cyan.underline('https://github.com/AdFabConnect/abejs/blob/master/docs/abe-config.md') |
34
|
|
|
) |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
}catch(e){ |
39
|
|
|
console.log('LoadConfig Error') |
40
|
|
|
console.log(e) |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
loadLocalConfig(result) |
46
|
|
|
|
47
|
|
|
result.exist = (conf, json) => { |
48
|
|
|
var c = conf.split('.') |
49
|
|
|
var current = json |
50
|
|
|
if(typeof current !== 'undefined' && current !== null) { |
51
|
|
|
Array.prototype.forEach.call(c, (c) => { |
52
|
|
|
if(current !== false && typeof current[c] !== 'undefined' && current[c] !== null) { |
53
|
|
|
current = current[c] |
|
|
|
|
54
|
|
|
}else { |
55
|
|
|
current = false |
56
|
|
|
return false |
57
|
|
|
} |
58
|
|
|
}) |
59
|
|
|
return current |
60
|
|
|
}else { |
61
|
|
|
return false |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
result.localConfigExist = () => { |
66
|
|
|
if(result.root !==''){ |
67
|
|
|
try{ |
68
|
|
|
var stat = fse.statSync(result.root) |
69
|
|
|
if (stat && stat.isDirectory()) { |
70
|
|
|
try{ |
71
|
|
|
stat = fse.statSync(path.join(result.root,'abe.json')) |
72
|
|
|
if (stat) { |
73
|
|
|
return true |
74
|
|
|
} |
75
|
|
|
}catch(e) { |
76
|
|
|
return false |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
}catch(e){ |
80
|
|
|
|
81
|
|
|
return false |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return false |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
result.getLocalConfig = () => { |
89
|
|
|
if (result.localConfigExist()){ |
90
|
|
|
return fse.readJsonSync(path.join(result.root,'abe.json')) |
91
|
|
|
} else { |
92
|
|
|
return {} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
result.getDefault = (conf) => { |
97
|
|
|
return result[conf] |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
result.get = (conf) => { |
101
|
|
|
|
102
|
|
|
return result.exist(conf, result) |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
result.set = (json) => { |
106
|
|
|
extend(true, result, json) |
107
|
|
|
loadLocalConfig(result) |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
result.save = (json) => { |
111
|
|
|
// extend(true, result, json) |
112
|
|
|
|
113
|
|
|
if (result.localConfigExist()){ |
114
|
|
|
// var abeJson = fse.readJsonSync(path.join(result.root,'abe.json')) |
115
|
|
|
// extend(true, abeJson, json) |
116
|
|
|
var confPath = path.join(result.root,'abe.json') |
117
|
|
|
fse.writeJsonSync(confPath, json, { space: 2, encoding: 'utf-8' }) |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
result.getConfigByWebsite = () => { |
122
|
|
|
var defaultConfig = extend(true, {}, result) |
123
|
|
|
var configBySite = { |
124
|
|
|
default: { |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
var localConfig = extend(true, {}, defaultConfig) |
130
|
|
|
for(var item in localConfig) { |
|
|
|
|
131
|
|
|
switch(item) { |
|
|
|
|
132
|
|
|
case 'intlData': |
133
|
|
|
configBySite.default.intlData = localConfig[item] |
134
|
|
|
break |
135
|
|
|
case 'templates': |
136
|
|
|
configBySite.default.templates = localConfig[item] |
137
|
|
|
break |
138
|
|
|
case 'structure': |
139
|
|
|
configBySite.default.structure = localConfig[item] |
140
|
|
|
break |
141
|
|
|
case 'data': |
142
|
|
|
configBySite.default.data = localConfig[item] |
143
|
|
|
break |
144
|
|
|
case 'publish': |
145
|
|
|
configBySite.default.publish = localConfig[item] |
146
|
|
|
break |
147
|
|
|
case 'files': |
148
|
|
|
configBySite.default.files = { |
149
|
|
|
templates: { |
150
|
|
|
extension: localConfig[item].templates.extension |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
break |
154
|
|
|
case 'upload': |
155
|
|
|
configBySite.default.upload = localConfig[item] |
156
|
|
|
break |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return configBySite |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
export default result |