-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathscorecard.js
75 lines (69 loc) · 2.53 KB
/
scorecard.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
const crypto = require('crypto')
const fs = require('fs-extra')
const nodereddev = require('node-red-dev')
const events = require('./events')
const npmNodes = require('./nodes')
function scorecard (packagename, version, nodePath) {
const fileid = crypto.randomBytes(4).toString('hex')
console.log('Running scorecard', packagename, version, nodePath)
try {
const pkg = fs.readJsonSync(nodePath + '/package.json')
console.log(' - Scorecard package.json:', pkg.name, pkg.version)
} catch (err) {
console.log(' - Error checking packaging:', err)
}
nodereddev.run(['validate', '-p', nodePath, '-o', `${nodePath}/../${fileid}.json`, '-e', 'true'])
// eslint-disable-next-line n/no-extraneous-require
.then(require('@oclif/command/flush'))
.then(() => {
const card = fs.readJsonSync(`${nodePath}/../${fileid}.json`)
return npmNodes.update(packagename, { scorecard: card }).then(() => card)
}).then((card) => {
// fs.removeSync(nodePath+'/../..');
let message = 'Result: '
const keys = Object.keys(card)
keys.sort(function (A, B) {
if (A[0] !== B[0]) {
// Reverse order of the groups - P, N, D
return B.localeCompare(A)
} else {
// Numerical order within the group
return A.substring(1).localeCompare(B.substring(1))
}
})
for (const rule of keys) {
if (rule !== 'package') {
const result = card[rule]
if (result.test) {
message += ':white_check_mark: '
} else {
if (['P01', 'P04', 'P05', 'D02'].includes(rule)) {
message += ':x: '
} else {
message += ':warning: '
}
}
}
}
events.add({
action: 'scorecard_added',
module: packagename,
version,
message
})
return null
})
.catch((error) => {
console.log(error.message)
events.add({
action: 'scorecard_failed',
module: packagename,
version,
message: error.message
})
// fs.removeSync(nodePath+'/../..');
})
}
module.exports = {
scorecard
}