Completed
Push — master ( 7e9900...dec7f5 )
by
unknown
01:54
created

abe-get-select-template-keys.js ➔ ... ➔ ???   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 10
nop 1
dl 0
loc 27
rs 8.439
c 0
b 0
f 0
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
  log,
14
  Hooks,
0 ignored issues
show
Unused Code introduced by
The variable Hooks seems to be never used. Consider removing it.
Loading history...
15
  removeDuplicateAttr,
0 ignored issues
show
Unused Code introduced by
The variable removeDuplicateAttr seems to be never used. Consider removing it.
Loading history...
16
  Manager
0 ignored issues
show
Unused Code introduced by
The variable Manager seems to be never used. Consider removing it.
Loading history...
17
} from '../../cli'
18
19
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...
20
  var res = []
21
  var files = fse.readdirSync(currentPath)
22
  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...
23
    var currentFile = currentPath + '/' + files[i]
24
    var stats = fse.statSync(currentFile)
25
    if (stats.isFile()) {
26
      if (currentFile.indexOf(config.files.templates.extension) > -1) {
27
        res.push(currentFile)
28
      }
29
    }
30
    else if (stats.isDirectory()) {
31
      res = res.concat(traverseFileSystem(currentFile))
32
    }
33
  }
34
  return res
35
}
36
37
var findTemplates = function(templatesPath) {
38
  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...
39
    let templatesList = traverseFileSystem(templatesPath)
40
    resolve(templatesList)
41
  })
42
43
  return p
44
}
45
46
/**
47
 * Get columns and where.left ids of a select statement
48
 *
49
 * select title, image from ../ where template=""
50
 *
51
 * return [title, image, template]
52
 * 
53
 * @param  {Array} templatesList ["article.html", "other.html"]
54
 * @return {Promise}
55
 */
56
var findRequestColumns = function(templatesList) {
57
  var whereKeysCheck = {}
58
  var whereKeys = []
59
  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...
60
    let util = new Util()
61
    Array.prototype.forEach.call(templatesList, (file) => {
62
      var template = fse.readFileSync(file, 'utf8')
63
      var matches = util.dataRequest(template)
64
65
      Array.prototype.forEach.call(matches, (match) => {
66
        var obj = Util.getAllAttributes(match[0], {})
67
        obj = Util.sanitizeSourceAttribute(obj, {})
68
        
69
        var type = Sql.getSourceType(obj.sourceString)
70
71
        switch (type) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
72
        case 'request':
73
          var request = Sql.handleSqlRequest(obj.sourceString, {})
74
          if(typeof request.columns !== 'undefined' && request.columns !== null) {
75
            Array.prototype.forEach.call(request.columns, (column) => {
76
              if(typeof whereKeysCheck[column] === 'undefined' || whereKeysCheck[column] === null) {
77
                whereKeysCheck[column] = true
78
                whereKeys.push(column)
79
              }
80
            })
81
          }
82
          if(typeof request.where !== 'undefined' && request.where !== null) {
83
            Array.prototype.forEach.call(request.where, (where) => {
84
              if(typeof whereKeysCheck[where.left] === 'undefined' || whereKeysCheck[where.left] === null) {
85
                whereKeysCheck[where.left] = true
86
                whereKeys.push(where.left)
87
              }
88
            })
89
          }
90
        }
91
      })
92
    })
93
    resolve(whereKeys)
94
  })
95
96
  return p
97
}
98
99
var getSelectTemplateKeys = function(templatesPath) {
100
  var p = new Promise((resolve, reject) => {
101
    findTemplates(templatesPath)
102
      .then((templatesList) => {
103
104
        findRequestColumns(templatesList)
105
          .then((whereKeys) => {
106
            resolve(whereKeys)
107
          },
108
          () => {
109
            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...
110
            reject()
111
          })
112
          .catch((e) => {
113
            console.error('getSelectTemplateKeys', e)
114
            reject()
115
          })
116
      },
117
      () => {
118
        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...
119
        reject()
120
      })
121
      .catch((e) => {
122
        console.error('getSelectTemplateKeys', e)
123
        reject()
124
      })
125
126
  })
127
128
  return p
129
}
130
131
export default getSelectTemplateKeys