-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
289 lines (282 loc) · 15.2 KB
/
main.ts
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const xpath = require('xpath')
const { tmpdir } = require('node:os')
const dom = require('@xmldom/xmldom').DOMParser
const fs = require('fs/promises')
const mk = require('fs')
const iconv = require('iconv-lite')
const path = require('path')
const archiver = require('archiver')
async function jobArrived(s: Switch, flowElement: FlowElement, job: Job) {
let flowlist:any = []
let heirarchy:any = []
let thehierarchy:any
// let dirs:any = []
let flowpane:any = await flowElement.getPropertyStringValue("FlowPane")
let outputprop:any = await flowElement.getPropertyStringValue("Output")
let flowxmls:any = await flowElement.getPropertyStringValue("flowxmls")
let exportder:any = await flowElement.getPropertyStringValue("exportdir")
let packages:any = await flowElement.getPropertyStringValue("packages")
let dir:any = await mk.readdirSync(flowxmls)
let flowpanexml = await fs.readFile(flowpane)
let flowpanexmlconvert = await iconv.decode(flowpanexml, 'utf-8')
let flowpanedoc = await new dom().parseFromString(flowpanexmlconvert, 'text/xml')
let statuserror:any = "good"
//loop through flows directory
for (let i = 0;i<dir.length;i++){
async function testflowxml(){
try{
let xmlfile:any = await fs.readFile(flowxmls + "/" + dir[i])
let flowxmlconvert:any = await iconv.decode(xmlfile, 'utf-8')
let flowdoc:any = await new dom().parseFromString(flowxmlconvert, 'text/xml')
let flowid = xpath.select1("//Data/@Id", flowdoc).value
return "good"
}catch(error){
let status = "failed to read Flow.xml, please confirm you selected the correct flow xmls folder."
return status;
}
}
let status = await testflowxml()
if (status != "good"){
statuserror = status
break;
}
let xmlfile:any = await fs.readFile(flowxmls + "/" + dir[i])
let flowxmlconvert:any = await iconv.decode(xmlfile, 'utf-8')
let flowdoc:any = await new dom().parseFromString(flowxmlconvert, 'text/xml')
let flowid = xpath.select1("//Data/@Id", flowdoc).value
let flowversion = xpath.select("//Version", flowdoc)[0].firstChild.data
let Switchversionroot = xpath.select1("//Version/@ModifiedBySwitch",flowdoc).value
await job.log(LogLevel.Warning, "SwitchVersion: " +Switchversionroot)
let SwitchVersionArray = Switchversionroot.split(".")
let SwitchVersionNumber = SwitchVersionArray[0]
let SwitchVersionUpdateNumber = SwitchVersionArray[1]
while (SwitchVersionUpdateNumber.length != 3){
SwitchVersionUpdateNumber = SwitchVersionUpdateNumber+"0"
}
let platform = process.platform
let OperatingSystem:any
if (platform == "darwin"){
OperatingSystem = "Mac"
} else {
OperatingSystem = "Windows"
}
let flowname = xpath.select("//FlowFolder", flowdoc)[0].firstChild.data
let object = xpath.select("//ScriptPackagePath", flowdoc)
let actions = xpath.select("//ActionList", flowdoc)
let profiles = xpath.select("//PDFprofile", flowdoc)
let variables = xpath.select("//VariableSet", flowdoc)
let flow = "Flow=" + flowid
let query = "//Group[" + flow + "]/@Name"
let exdir:any = exportder
//archive hierarchy
if (xpath.select1(query, flowpanedoc) != undefined){
let nodes = xpath.select1(query, flowpanedoc).value
let dirs:any = []
dirs.push(nodes)
while (nodes != undefined){
if (nodes != undefined){
try{
let nodes1 = xpath.select1(query+"/../../@Name", flowpanedoc).value
dirs.push(nodes1)
query = query + "/../../@Name"
}catch(error){
break;
}
}else{
break;
}
}
for (let i = dirs.length-1;i>=0;i--){
exdir = exdir + "/" + dirs[i]
// thehierarchy = thehierarchy + "," + dirs[i]
// heirarchy.push(dirs[i])
}
}
if (mk.existsSync(exdir) || outputprop == "Output backups into flow"){
}else{
mk.mkdirSync(exdir, {recursive:true})
}
let thefile:any, tempfile:any
if(outputprop == "Output backups into flow"){
thefile = exportder+"/Flow_" + flowid + "_" + flowname + "_v" + flowversion + ".sflow"
tempfile = exportder+"/Flow_" + flowid + "_" + flowname + "_v" + flowversion + ".sflow"
}else{
thefile = exdir+"/Flow_" + flowid + "_" + flowname + "_v" + flowversion + ".sflow"
tempfile = exdir+"/Flow_" + flowid + "_" + flowname + "_v" + flowversion + ".sflow"
}
if(mk.existsSync(thefile)){
await job.log(LogLevel.Info, thefile + " Already exists, not backing up")
}else{
for(let i = 0;i<flowversion;i++){
if(mk.existsSync(exdir+"/Flow_" + flowid + "_" + flowname + "_v" + i+ ".sflow")){
await job.log(LogLevel.Warning, "Deleting old version: Flow_" + flowid + "_" + flowname + "_v" + i+ ".sflow")
await fs.unlink(exdir+"/Flow_" + flowid + "_" + flowname + "_v" + i+ ".sflow")
}
}
heirarchy.push(exdir)
async function archive(){
const output = await mk.createWriteStream(tempfile)
const archive = archiver('zip', {})
await flowlist.push(thefile)
// await heirarchy.push(dirs)
await output.on('close', async function() {
await job.log(LogLevel.Info, "Archived: " + thefile)
})
output.on('end', async function() {
await job.log(LogLevel.Info, "Data Complete")
})
await archive.pipe(output)
let manifest:any
if(packages == "Yes"){
let propsets:any = []
let filename
await job.log(LogLevel.Warning, "Number of PDF profiles present: " + profiles.length)
if (variables.length > 0) {
for (let i = 0; i<variables.length;i++){
try{
let fullPath = variables[i].firstChild.data
filename = fullPath.replace(/^.*[\\/]/,'')
let intopro = `<PropertySet Plugin="" PropertyType="file" InternalPath="${filename}" Path="${fullPath}"/>`
await archive.file(fullPath, {name: filename})
propsets.push(intopro)
}catch(error){
await job.log(LogLevel.Warning, "Pitstop configurator used but no VariableSet present")
}
}
}
if (profiles.length > 0){
for(let i = 0;i<profiles.length;i++){
try{
let propfile:any = await fs.readFile(profiles[i].firstChild.data)
let intopro = `<PropertySet Plugin="" PropertyType="file" InternalPath="profile${i}/PropertySet" Path="${profiles[i].firstChild.data}"/>`
propsets.push(intopro)
await archive.file(profiles[i].firstChild.data, {name: "profile" + i + "/PropertySet"})
let propxmlconvert:any = await iconv.decode(propfile, 'utf-8')
let propdoc:any = await new dom().parseFromString(propxmlconvert, 'text/xml')
let profilesprop = xpath.select("//Path", propdoc)
for (let b = 0; b < profilesprop.length; b++){
let fullPath = profilesprop[b].firstChild.data
filename = fullPath.replace(/^.*[\\/]/,'')
let intopro = `<PropertySet Plugin="" PropertyType="file" InternalPath="${filename}" Path="${fullPath}"/>`
await archive.file(fullPath, {name: filename})
propsets.push(intopro)
}
}catch(error){
await job.log(LogLevel.Warning, "Pitstop configurator used but no PDF profile present")
}
}
}
await job.log(LogLevel.Warning, "Number of actions present: " + actions.length)
if (actions.length > 0){
for (let i = 0; i < actions.length; i++){
try{
// await job.log(LogLevel.Warning, "first action: " + actions[i].firstChild.data)
let propfile:any = await fs.readFile(actions[i].firstChild.data)
let intopro = `<PropertySet Plugin="" PropertyType="file" InternalPath="action${i}/PropertySet" Path="${actions[i].firstChild.data}"/>`
propsets.push(intopro)
await archive.file(actions[i].firstChild.data, {name: "action" + i + "/PropertySet"})
let propxmlconvert:any = await iconv.decode(propfile, 'utf-8')
let propdoc:any = await new dom().parseFromString(propxmlconvert, 'text/xml')
let actionsprop = xpath.select("//Path", propdoc)
// await job.log(LogLevel.Warning, actionsprop[0].firstChild.data)
for (let b = 0; b < actionsprop.length; b++){
let fullPath = actionsprop[b].firstChild.data
filename = fullPath.replace(/^.*[\\/]/,'')
let intopro = `<PropertySet Plugin="" PropertyType="file" InternalPath="${filename}" Path="${fullPath}"/>`
await archive.file(fullPath, {name: filename})
propsets.push(intopro)
}
// let fullPath = actionsprop[0].firstChild.data
// filename = fullPath.replace(/^.*[\\/]/,'')
// let intopro = `<PropertySet Plugin="" PropertyType="file" InternalPath="${filename}" Path="${fullPath}"/>`
// await archive.file(fullPath, {name: filename})
// propsets.push(intopro)
}catch(error){
await job.log(LogLevel.Warning, "Pitstop configurator used but no action present")
}
}
}
if (object.length == 0 && actions.length == 0 && profiles.length == 0 && variables.length == 0){
propsets = ""
}else{
for (let i = 0; i < object.length; i++){
let fullPath = object[i].firstChild.data
filename = fullPath.replace(/^.*[\\/]/,'')
let intopro = `<PropertySet Plugin="" PropertyType="file" InternalPath="${filename}" Path="${fullPath}"/>`
await archive.file(fullPath, {name: filename})
propsets.push(intopro)
}
}
let Propertysets1 = `<PropertySets>${propsets}</PropertySets>`
let Propertysets = Propertysets1.replace(/>,</g,"><")
manifest = `<Manifest>
<ProductInfo>Switch Version ${SwitchVersionNumber}</ProductInfo>
<ExportFormatVersion>1.0</ExportFormatVersion>
<FlowFile>flow.xml</FlowFile>
<SwitchFlavour>Switch</SwitchFlavour>
<SwitchReleaseVersionNumber>${SwitchVersionNumber}</SwitchReleaseVersionNumber>
<SwitchReleaseType></SwitchReleaseType>
<SwitchReleaseTypeNumber>0</SwitchReleaseTypeNumber>
<SwitchUpdateVersionNumber>${SwitchVersionUpdateNumber}</SwitchUpdateVersionNumber>
<OperatingSystem>${OperatingSystem}</OperatingSystem>
${Propertysets}
</Manifest>`
}else{
manifest = `<Manifest>
<ProductInfo>Switch Version ${SwitchVersionNumber}</ProductInfo>
<ExportFormatVersion>1.0</ExportFormatVersion>
<FlowFile>flow.xml</FlowFile>
<SwitchFlavour>Switch</SwitchFlavour>
<SwitchReleaseVersionNumber>${SwitchVersionNumber}</SwitchReleaseVersionNumber>
<SwitchReleaseType></SwitchReleaseType>
<SwitchReleaseTypeNumber>0</SwitchReleaseTypeNumber>
<SwitchUpdateVersionNumber>${SwitchVersionUpdateNumber}</SwitchUpdateVersionNumber>
<OperatingSystem>${OperatingSystem}</OperatingSystem>
</Manifest>`;
}
await archive.append(flowxmlconvert, {name:"flow.xml"})
await archive.append(manifest,{name:"manifest.xml"})
await archive.finalize();
}
let archived:any = await archive()
}
}
if(statuserror == "good"){
await job.log(LogLevel.Warning, "Number of Flows: " + flowlist.length + " DIRLENGTH: " + dir.length)
//"Save backups to export directory""Output backups into flow""Both"
await job.log(LogLevel.Warning, "OUTPUT PROP " + outputprop)
if(outputprop == "Both" || outputprop == "Output backups into flow"){
await job.log(LogLevel.Warning, "TEST INSIDE BOTH")
for(let i = 0; i<flowlist.length;i++){
let newlocation = flowlist[i]
let subjob = await flowElement.createJob(newlocation)
//remove exportder
let heirarchyremoveexportder = heirarchy[i].replace(exportder+"/","")
let heirarchyreplaceslasharray,heirarchyreplaceslash
if (heirarchyremoveexportder == exportder){
heirarchyreplaceslash = ""
} else {
heirarchyreplaceslasharray = heirarchyremoveexportder.replace(/\//g,";")
heirarchyreplaceslash = heirarchyreplaceslasharray.split(";")
}
await job.log(LogLevel.Warning, "HIERARCHY: " + heirarchyreplaceslash)
await subjob.setPrivateData("EnfocusSwitch.hierarchy", heirarchyreplaceslash)
// await subjob.sendToSingle()
await subjob.sendToData(Connection.Level.Success)
if(outputprop == "Output backups into flow"){
mk.unlinkSync(newlocation)
}
// await job.sendToNull()
// await job.log(LogLevel.Warning, "loop num " + i + "flowlist " + flowlist.length)
if (i+1 === flowlist.length){
await job.sendToNull()
}
}
}
}else{
await job.log(LogLevel.Error, statuserror)
await job.sendToData(Connection.Level.Error)
}
// await job.setPrivateData("EnfocusSwitch.hierarchy", "C:/Temp")
// await job.sendToNull()
}