-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.js
More file actions
95 lines (90 loc) · 3.46 KB
/
functions.js
File metadata and controls
95 lines (90 loc) · 3.46 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
const errDebug = require('debug')('debug:err')
const superDebug = require('debug')('debug:stdout')
const { execSync } = require('child_process')
const fs = require('fs')
const { stderr } = require('process')
const genfunc = require('./genericfunctions')
const FileType = require('file-type')
var loopbacktoken = false
// functions block and export and use of funxtions. in this file is so that we can use nested stubs in our tests.
// if we don't call the functions from this block they will be imported to the test module and use the nested local functions and not as a global function
// that we can stub
const functions = {
filterPKG,
validatePKGs,
testinstallPKG,
validation
}
module.exports = functions;
async function filterPKG(StorageManagerURL) {
const pkgs = await genfunc.getPackages(StorageManagerURL)
return pkgs.filter(s=>~s.indexOf(".tgz"));
}
async function validation(pkgdir) {
do {
loopbacktoken = false
superDebug(`start while loop, loopbacktoken: ${loopbacktoken}`)
try {
await validatePKGs(pkgdir)
} catch (err) {
errDebug(err)
}
superDebug(`end of while loop, loopbacktoken: ${loopbacktoken}`)
} while (loopbacktoken)
console.log('Package validator has finished')
}
function validatePKGs(pkgdir) {
return new Promise((res, rej) => {
var itemsProcessed = 0
if (fs.readdirSync(pkgdir).length != 0) {
fs.readdirSync(pkgdir).forEach(async (file, index, array) => {
let filetype = await FileType.fromFile(`${pkgdir}/${file}`)
itemsProcessed++
if (typeof filetype !== 'undefined' && filetype.mime === "application/gzip") {
try {
await testinstallPKG(pkgdir, file)
} catch (err) {
errDebug(err)
}
if (itemsProcessed === array.length) {
res(true)
}
} else {
genfunc.genPkgArray(file,50,"bad_file_type")
fs.unlinkSync(`${pkgdir}/${file}`)
rej(`File "${file}" is not an NPM package`)
}
})
superDebug('End of readdir foreach')
} else {
res(`There are no files in the validate directory: ${pkgdir}`)
}
})
}
function testinstallPKG(dir, pkg) {
return new Promise((res, rej) => {
console.log(`Validating Package ${pkg}`)
superDebug(`Stage testinstallPKG:start loopbacktoken: ${loopbacktoken}`)
try {
const stdout = execSync(`npm i --dry-run ${dir}/${pkg}`, {stdio: [stderr]}).toString()
superDebug(stdout)
console.log(`Package ${pkg} installed successfully`)
loopbacktoken = true
genfunc.deletePackagefile(`${dir}/${pkg}`)
genfunc.genPkgArray(pkg,0,"success")
res(true)
} catch (err) {
const stderr = err.stderr
if (stderr.includes("is not in the npm registry")) {
console.log(`Package ${pkg} has missing dependencies...`)
genfunc.genPkgArray(pkg,1,"missing_deps")
errDebug(err)
} else {
console.log(`Unable to install package ${pkg}, run debug mode to view error`)
genfunc.genPkgArray(pkg,666,"unknown_err")
errDebug(err)
}
rej(err)
}
})
}