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

Complexity

Total Complexity 39
Complexity/F 3.9

Size

Lines of Code 163
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 24
dl 0
loc 163
rs 8.2857
c 0
b 0
f 0
wmc 39
mnd 5
bc 34
fnc 10
bpm 3.4
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
  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) {
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...
131
    switch(item) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
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