-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·185 lines (159 loc) · 5.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'use strict'
var fs = require('fs')
var fx = require('node-fs')
var git = require('git-controller')
var util = require('util')
var eventEmitter = require('events').EventEmitter
var dbConnection = require('./dbConnection')
function queryObject(result, config, item, callback) {
var destFolder = config.destFolder
var Param = config.param
var files = []
var getObject = function(result) {
if (result.length <= 0) {
callback(null, files)
} else {
var Object = result.shift()
var Param = config.param
var name = eval("`" + item.Object.Name + "`")
var definition = eval("`" + item.Object.Definition + "`")
var destFile = destFolder + '/' + name.replace(' ', '')
fs.writeFileSync(destFile, definition, 'utf8')
files.push(destFile)
getObject(result)
}
}
getObject(result)
}
function queryObjects(result, item, config, emitter, callback) {
var addFiles = []
var getObject = function(result) {
if (result.length <= 0) {
callback(null, addFiles)
} else {
var Object = result.shift()
var Param = config.param
var query = eval("`" + item.QueryObject + "`")
emitter.emit('Query', query, config)
var conn = dbConnection(config.DB, config.connectionString)
conn.execute(query, function(error, objects) {
if (error) {
callback(error)
} else {
queryObject(
objects, config, item,
function(error, destFile) {
if (error) {
callback(error)
} else {
addFiles = addFiles.concat(destFile)
getObject(result)
}
})
}
})
}
}
getObject(result)
}
var getCallback = function(callback) {
if (callback != null) {
return callback
}
return function(err) {
if (err) {
throw err
}
}
}
function SchemaSync(schemaConfig, gitConfig) {
if ((this instanceof SchemaSync) == false) {
return new SchemaSync(schemaConfig, gitConfig)
}
this.SchemaConfig = schemaConfig
this.GitConfig = gitConfig
eventEmitter.call(this)
}
util.inherits(SchemaSync, eventEmitter)
SchemaSync.prototype.GetSchema = function getSchema(connectionString, schemaPath, param, callback) {
callback = getCallback(callback)
var self = this
var config = JSON.parse(JSON.stringify(self.SchemaConfig))
config.schemaPath = schemaPath
config.connectionString = connectionString
config.param = JSON.parse(JSON.stringify(param))
if (fs.existsSync(config.schemaPath) == false) {
fx.mkdirSync(config.schemaPath, 777, true)
}
var addFiles = []
var getObject = function(schemaList) {
if (schemaList.length <= 0) {
callback(null, addFiles)
} else {
var item = schemaList.shift()
config.destFolder = config.schemaPath + '/' + item.ObjectName
if (fx.existsSync(config.destFolder) == false) {
fx.mkdirSync(config.destFolder, 777, true)
}
var Param = config.param
var query = eval("`" + item.QueryObjects + "`")
self.emit('Query', query, config)
var conn = dbConnection(config.DB, config.connectionString)
conn.execute(query, function(error, result) {
if (error) {
callback(error)
} else {
queryObjects(result, item, config, self,
function(err, destFiles) {
if (err) {
callback(err)
} else {
addFiles = addFiles.concat(destFiles)
getObject(schemaList)
}
})
}
})
}
}
getObject(config.schemaList)
}
SchemaSync.prototype.GitPush = function gitPush(schemaFiles, comment, callback) {
callback = getCallback(callback)
var self = this
var gitRepo = git(self.GitConfig.Path)
var commitGitRepo = function() {
var staged = gitRepo.statusSync().staged.length
if (staged > 0) {
self.emit('Message', `${staged} files to commit: ${comment}.`)
gitRepo.commitSync(comment)
}
if (self.GitConfig.Remote != null && self.GitConfig.Branch != null &&
self.GitConfig.Remote.length > 0 && self.GitConfig.Branch.length > 0) {
gitRepo.push(self.GitConfig.Remote, self.GitConfig.Branch, function(err) {
callback(err)
})
} else {
callback(null)
}
}
var addGitRepo = function(files) {
var subFiles = files
if (files.length > 50) {
subFiles = files.splice(0,50)
}
gitRepo.add(subFiles, function(err) {
if (err) {
callback(err)
} else {
if (subFiles != files) {
addGitRepo(files)
} else {
commitGitRepo()
}
}
})
}
addGitRepo(schemaFiles)
}
module.exports = SchemaSync