Completed
Push — master ( 3f06be...7c320c )
by greg
01:42
created

config.js ➔ ... ➔ ???   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 9.2
1
import fse from 'fs-extra'
2
import clc from 'cli-color'
3
import extend from 'extend'
4
5
import config from './config.json'
6
7
var result = config
8
result.root = result.root.replace(/\/$/, '')
9
var hintAbeJson = false
10
11
var loadLocalConfig = (result) => {
12
  var website = result.root.replace(/\/$/, '')
13
  try{
14
    var stat = fse.statSync(website)
15
    if (stat && stat.isDirectory()) {
16
      try{
17
        stat = fse.statSync(website + '/abe.json')
18
        if (stat) {
19
          var json = fse.readJsonSync(website + '/abe.json')
20
          var 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...
21
        }
22
      }catch(e) {
23
        if (!hintAbeJson) {
24
          hintAbeJson = true
25
          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...
26
						clc.green('[ Hint ]'),
27
						'create abe.json to config',
28
						clc.cyan.underline('https://github.com/AdFabConnect/abejs/blob/master/docs/abe-config.md')
29
					)
30
        }
31
      }
32
    }
33
  }catch(e){}
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
34
}
35
36
loadLocalConfig(result)
37
38
result.exist = (conf, json) => {
39
  var c = conf.split('.')
40
  var current = json
41
  if(typeof current !== 'undefined' && current !== null) {
42
    Array.prototype.forEach.call(c, (c) => {
43
      if(current !== false && typeof current[c] !== 'undefined' && current[c] !== null) {
44
        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...
45
      }else {
46
        current = false
47
        return false
48
      }
49
    })
50
    return current
51
  }else {
52
    return false
53
  }
54
}
55
56
result.getDefault = (conf) => {
57
  return result[conf]
58
}
59
60
result.get = (conf, file) => {
61
  return result.exist(conf, result)
62
63
  if(typeof file !== 'undefined' && file !== null && file !== '') {
0 ignored issues
show
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
Bug introduced by
The variable file seems to be never initialized.
Loading history...
64
    var website = file.replace(result.root, '')
65
    website = website.split('/')[0]
66
67
    var websiteConf = result.exist(conf, result.websites[website])
0 ignored issues
show
Bug introduced by
The variable conf seems to be never initialized.
Loading history...
68
    if(websiteConf !== false) {
69
      return websiteConf
70
    }
71
  }
72
73
  return result.exist(conf, result)
0 ignored issues
show
introduced by
This code is unreachable and can thus be removed without consequences.
Loading history...
74
}
75
76
result.set = (json) => {
77
  extend(true, result, json)
78
  loadLocalConfig(result)
79
}
80
81
result.save = (website, json) => {
82
  extend(true, result, json)
83
84
  var confPath = result.root.replace(/\/$/, '') + '/abe.json'
85
  fse.writeJsonSync(confPath, json, { space: 2, encoding: 'utf-8' })
86
}
87
88
result.getConfigByWebsite = () => {
89
  var defaultConfig = extend(true, {}, result)
90
  var configBySite = {
91
    default: {
92
			
93
    }
94
  }
95
	
96
  var localConfig = extend(true, {}, defaultConfig)
97
  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...
98
    switch(item) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
99
    case 'intlData':
100
      configBySite.default.intlData = localConfig[item]
101
      break
102
    case 'templates':
103
      configBySite.default.templates = localConfig[item]
104
      break
105
    case 'structure':
106
      configBySite.default.structure = localConfig[item]
107
      break
108
    case 'data':
109
      configBySite.default.data = localConfig[item]
110
      break
111
    case 'draft':
112
      configBySite.default.draft = localConfig[item]
113
      break
114
    case 'publish':
115
      configBySite.default.publish = localConfig[item]
116
      break
117
    case 'files':
118
      configBySite.default.files = {
119
        templates: {
120
          extension: localConfig[item].templates.extension
121
        }
122
      }
123
      break
124
    case 'upload':
125
      configBySite.default.upload = localConfig[item]
126
      break
127
    }
128
  }
129
	
130
  return configBySite
131
}
132
133
export default result