Completed
Push — master ( 7ee597...82641a )
by greg
01:56
created

src/cli/core/config/config.js   B

Complexity

Total Complexity 39
Complexity/F 3.9

Size

Lines of Code 162
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
wmc 39
c 0
b 0
f 0
nc 24
mnd 5
bc 33
fnc 10
dl 0
loc 162
rs 8.2857
bpm 3.3
cpm 3.9
noi 5

1 Function

Rating   Name   Duplication   Size   Complexity  
C config.js ➔ ??? 0 28 8
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)
0 ignored issues
show
Unused Code introduced by
The assignment to variable result seems to be never used. Consider removing it.
Loading history...
26
          }
27
        }catch(e) {
28
          if (!hintAbeJson) {
29
            hintAbeJson = true
30
            console.log(
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
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]
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
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
  var confPath = path.join(result.root,'abe.json')
114
  fse.writeJsonSync(confPath, json, { space: 2, encoding: 'utf-8' })
115
}
116
117
result.getConfigByWebsite = () => {
118
  var defaultConfig = extend(true, {}, result)
119
  var configBySite = {
120
    default: {
121
			
122
    }
123
  }
124
	
125
  var localConfig = extend(true, {}, defaultConfig)
126
  for(var item in localConfig) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
127
    switch(item) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
128
    case 'intlData':
129
      configBySite.default.intlData = localConfig[item]
130
      break
131
    case 'templates':
132
      configBySite.default.templates = localConfig[item]
133
      break
134
    case 'structure':
135
      configBySite.default.structure = localConfig[item]
136
      break
137
    case 'data':
138
      configBySite.default.data = localConfig[item]
139
      break
140
    case 'draft':
141
      configBySite.default.draft = localConfig[item]
142
      break
143
    case 'publish':
144
      configBySite.default.publish = localConfig[item]
145
      break
146
    case 'files':
147
      configBySite.default.files = {
148
        templates: {
149
          extension: localConfig[item].templates.extension
150
        }
151
      }
152
      break
153
    case 'upload':
154
      configBySite.default.upload = localConfig[item]
155
      break
156
    }
157
  }
158
	
159
  return configBySite
160
}
161
162
export default result