-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenericfunctions.js
More file actions
116 lines (108 loc) · 3.08 KB
/
genericfunctions.js
File metadata and controls
116 lines (108 loc) · 3.08 KB
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
const axios = require('axios')
const fs = require('fs')
const errDebug = require('debug')('debug:err')
const superDebug = require('debug')('debug:stdout')
let pkgArray = []
const functions = {
getPackages,
downloadPackages,
deletePackagefile,
genPkgArray,
sendDataToPKGVal
}
module.exports = functions;
async function getPackages(dlUrl) {
const res = await axios({
url: dlUrl,
method: 'GET'
})
superDebug(res.data)
return res.data
}
async function downloadPackages(packagelist, dlUrl, targetDir) {
const amount = packagelist.length
for ( let i = 0; i < amount; i++) {
const packageName = packagelist[i]
console.log(packageName)
await downloadPackage(`${dlUrl}${packageName}`, `${targetDir}/${packageName}`)
}
}
function downloadPackage(fileUrl, outputLocationPath) {
const writer = fs.createWriteStream(outputLocationPath);
return axios({
method: 'get',
url: fileUrl,
responseType: 'stream',
}).then((response) => {
return new Promise((res, rej) => {
response.data.pipe(writer);
let error = null;
writer.on('error', err => {
error = err;
writer.close();
console.log(err)
rej(err);
});
writer.on('close', () => {
if (!error) {
console.log(fileUrl, 'download complete')
res(true);
}
});
});
});
}
function deletePackagefile(pkg) {
return new Promise ((res, rej) => {
try {
fs.unlinkSync(pkg)
console.log(`Deleted package file ${pkg} succesfully`)
res(true)
} catch (err) {
console.log(`Error deleting file ${pkg} run debug to view error`)
errDebug(err)
rej(err)
}
})
}
function genPkgArray(pkg, code, msg) {
return new Promise ((res, rej) => {
try {
if (pkgArray.some(e => e.name === pkg)) {
const index = pkgArray.findIndex(e => e.name === pkg)
if (index > -1) {
pkgArray.splice(index, 1)
}
}
pkgArray.push({ name: pkg, statusCode: code, msg: msg})
res(true)
} catch {
rej("Unable to push to pkg array")
}
})
}
function sendDataToPKGVal(ValURL, pkgtype, SessionID) {
superDebug(JSON.stringify(pkgArray))
if (pkgArray.length != 0){
let data = JSON.stringify({"sid":SessionID,"type":pkgtype,"statusCode":0,"pkgs":pkgArray});
let put = {
url: ValURL,
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
data: data
};
axios(put)
.then(function (res) {
console.log('Sent package array to Package Validator')
superDebug(res)
})
.catch(function (err) {
console.log(`Error sending package array to Package validator, run debug to view error`)
errDebug(err)
})
} else{
console.log(`no packages to validate`)
}
}