Completed
Push — master ( a10c33...e8fa11 )
by greg
01:59
created

src/cli/cms/templates/abe-get-select-template-keys.js   A

Complexity

Total Complexity 32
Complexity/F 1.78

Size

Lines of Code 152
Function Count 18

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 32
c 1
b 0
f 0
nc 1
mnd 3
bc 24
fnc 18
dl 0
loc 152
rs 9.6
bpm 1.3333
cpm 1.7777
noi 21

5 Functions

Rating   Name   Duplication   Size   Complexity  
A abe-get-select-template-keys.js ➔ findTemplates 0 8 1
B abe-get-select-template-keys.js ➔ getSelectTemplateKeys 0 31 1
B abe-get-select-template-keys.js ➔ traverseFileSystem 0 17 5
B abe-get-select-template-keys.js ➔ recurseWhereVariables 0 27 6
B abe-get-select-template-keys.js ➔ findRequestColumns 0 35 1
1
import path from 'path'
2
import fse from 'fs-extra'
3
import {execFile} from 'child_process'
4
import {
5
  fileUtils,
0 ignored issues
show
Unused Code introduced by
The variable fileUtils seems to be never used. Consider removing it.
Loading history...
6
  FileParser,
0 ignored issues
show
Unused Code introduced by
The variable FileParser seems to be never used. Consider removing it.
Loading history...
7
  Util,
8
  Sql,
9
  cleanSlug,
0 ignored issues
show
Unused Code introduced by
The variable cleanSlug seems to be never used. Consider removing it.
Loading history...
10
  getTemplate,
0 ignored issues
show
Unused Code introduced by
The variable getTemplate seems to be never used. Consider removing it.
Loading history...
11
  save,
0 ignored issues
show
Unused Code introduced by
The variable save seems to be never used. Consider removing it.
Loading history...
12
  config,
13
  Hooks,
0 ignored issues
show
Unused Code introduced by
The variable Hooks seems to be never used. Consider removing it.
Loading history...
14
  removeDuplicateAttr,
0 ignored issues
show
Unused Code introduced by
The variable removeDuplicateAttr seems to be never used. Consider removing it.
Loading history...
15
  Manager
0 ignored issues
show
Unused Code introduced by
The variable Manager seems to be never used. Consider removing it.
Loading history...
16
} from '../../'
17
18
var traverseFileSystem = function (currentPath, arr) {
0 ignored issues
show
Unused Code introduced by
The parameter arr is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
19
  var res = []
20
  var files = fse.readdirSync(currentPath)
21
  for (var i in files) {
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...
22
    var currentFile = currentPath + '/' + files[i]
23
    var stats = fse.statSync(currentFile)
24
    if (stats.isFile()) {
25
      if (currentFile.indexOf(config.files.templates.extension) > -1) {
26
        res.push(currentFile)
27
      }
28
    }
29
    else if (stats.isDirectory()) {
30
      res = res.concat(traverseFileSystem(currentFile))
31
    }
32
  }
33
  return res
34
}
35
36
var findTemplates = function(templatesPath) {
37
  var p = new Promise((resolve, reject) => {
0 ignored issues
show
Unused Code introduced by
The parameter reject is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
38
    let templatesList = traverseFileSystem(templatesPath)
39
    resolve(templatesList)
40
  })
41
42
  return p
43
}
44
45
/**
46
 * Get columns and where.left ids of a select statement
47
 *
48
 * select title, image from ../ where template=""
49
 *
50
 * return [title, image, template]
51
 * 
52
 * @param  {Array} templatesList ["article.html", "other.html"]
53
 * @return {Promise}
54
 */
55
56
var recurseWhereVariables = function (where) {
57
  var ar = []
58
59
  // console.log('* * * * * * * * * * * * * * * * * * * * * * * * * * * * *')
60
  // console.log('where', where.left)
61
  switch(where.operator) {
62
  case 'AND':
63
    var arLeft = recurseWhereVariables(where.left)
64
    var arRight = recurseWhereVariables(where.right)
65
    return arLeft.concat(arRight)
66
    break
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
67
  case 'OR':
68
    var arLeft = recurseWhereVariables(where.left)
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable arLeft already seems to be declared on line 63. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
69
    var arRight = recurseWhereVariables(where.right)
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable arRight already seems to be declared on line 64. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
70
    return arLeft.concat(arRight)
71
    break
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
72
  case 'IN':
73
    break
74
  case 'NOT IN':
75
    break
76
  default:
77
    return [where.left.column]
78
    break
0 ignored issues
show
Unused Code introduced by
This break statement is unnecessary and may be removed.
Loading history...
79
  }
80
81
  return ar
82
}
83
84
var findRequestColumns = function(templatesList) {
85
  var whereKeysCheck = {}
0 ignored issues
show
Unused Code introduced by
The variable whereKeysCheck seems to be never used. Consider removing it.
Loading history...
86
  var whereKeys = []
87
  var p = new Promise((resolve, reject) => {
0 ignored issues
show
Unused Code introduced by
The parameter reject is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
88
    let util = new Util()
89
    Array.prototype.forEach.call(templatesList, (file) => {
90
      var template = fse.readFileSync(file, 'utf8')
91
      var matches = util.dataRequest(template)
92
93
      Array.prototype.forEach.call(matches, (match) => {
94
        var obj = Util.getAllAttributes(match[0], {})
95
        obj = Util.sanitizeSourceAttribute(obj, {})
96
        
97
        var type = Sql.getSourceType(obj.sourceString)
98
99
        switch (type) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
100
        case 'request':
101
          var request = Sql.handleSqlRequest(obj.sourceString, {})
102
          if(typeof request.columns !== 'undefined' && request.columns !== null) {
103
            Array.prototype.forEach.call(request.columns, (column) => {
104
              whereKeys.push(column)
105
            })
106
          }
107
          if(typeof request.where !== 'undefined' && request.where !== null) {
108
            whereKeys = whereKeys.concat(recurseWhereVariables(request.where))
109
          }
110
        }
111
      })
112
    })
113
    whereKeys = whereKeys.filter(function (item, pos) {return whereKeys.indexOf(item) == pos})
114
    resolve(whereKeys)
115
  })
116
117
  return p
118
}
119
120
var getSelectTemplateKeys = function(templatesPath) {
121
  var p = new Promise((resolve, reject) => {
122
    findTemplates(templatesPath)
123
      .then((templatesList) => {
124
125
        findRequestColumns(templatesList)
126
          .then((whereKeys) => {
127
            resolve(whereKeys)
128
          },
129
          () => {
130
            console.log('findRequestColumns reject')
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...
131
            reject()
132
          })
133
          .catch((e) => {
134
            console.error('getSelectTemplateKeys', e)
135
            reject()
136
          })
137
      },
138
      () => {
139
        console.log('findTemplates reject')
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...
140
        reject()
141
      })
142
      .catch((e) => {
143
        console.error('getSelectTemplateKeys', e)
144
        reject()
145
      })
146
147
  })
148
149
  return p
150
}
151
152
export default getSelectTemplateKeys