1
|
|
|
import path from 'path' |
2
|
|
|
import fse from 'fs-extra' |
3
|
|
|
import {execFile} from 'child_process' |
4
|
|
|
import { |
5
|
|
|
fileUtils, |
|
|
|
|
6
|
|
|
FileParser, |
|
|
|
|
7
|
|
|
Util, |
8
|
|
|
Sql, |
9
|
|
|
cleanSlug, |
|
|
|
|
10
|
|
|
getTemplate, |
|
|
|
|
11
|
|
|
save, |
|
|
|
|
12
|
|
|
config, |
13
|
|
|
Hooks, |
|
|
|
|
14
|
|
|
removeDuplicateAttr, |
|
|
|
|
15
|
|
|
Manager |
|
|
|
|
16
|
|
|
} from '../../' |
17
|
|
|
|
18
|
|
|
var traverseFileSystem = function (currentPath, arr) { |
|
|
|
|
19
|
|
|
var res = [] |
20
|
|
|
var files = fse.readdirSync(currentPath) |
21
|
|
|
for (var i in files) { |
|
|
|
|
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) => { |
|
|
|
|
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 |
|
|
|
|
67
|
|
|
case 'OR': |
68
|
|
|
var arLeft = recurseWhereVariables(where.left) |
|
|
|
|
69
|
|
|
var arRight = recurseWhereVariables(where.right) |
|
|
|
|
70
|
|
|
return arLeft.concat(arRight) |
71
|
|
|
break |
|
|
|
|
72
|
|
|
case 'IN': |
73
|
|
|
break |
74
|
|
|
case 'NOT IN': |
75
|
|
|
break |
76
|
|
|
default: |
77
|
|
|
return [where.left.column] |
78
|
|
|
break |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return ar |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
var findRequestColumns = function(templatesList) { |
85
|
|
|
var whereKeysCheck = {} |
|
|
|
|
86
|
|
|
var whereKeys = [] |
87
|
|
|
var p = new Promise((resolve, reject) => { |
|
|
|
|
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) { |
|
|
|
|
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') |
|
|
|
|
131
|
|
|
reject() |
132
|
|
|
}) |
133
|
|
|
.catch((e) => { |
134
|
|
|
console.error('getSelectTemplateKeys', e) |
135
|
|
|
reject() |
136
|
|
|
}) |
137
|
|
|
}, |
138
|
|
|
() => { |
139
|
|
|
console.log('findTemplates reject') |
|
|
|
|
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 |