-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcheckpackage.js
232 lines (227 loc) · 9.51 KB
/
checkpackage.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
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
const os = require('os')
const fs = require('fs');
const { t } = require('tar');
const semver = require('semver');
const p = require('path')
//const nodegit = require('nodegit');
const axios = require('axios')
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
const officialnodes = require('../officalnodes.json')
function isGitUrl(str) {
//var regex = /(?:git|ssh|https?|git@[-\w.]+):(\/\/)?(.*?)(\.git)(\/?|\#[-\d\w._]+?)$/;
var regex = /(?:git|ssh|https?|git@[-\w.]+):(\/\/)?(.*?)(\/?|\#[-\d\w._]+?)$/;
return regex.test(str);
};
function checkpackage(path, cli, scorecard, npm_metadata) {
const package = require(path+'/package.json');
return new Promise((resolve, reject) => {
cli.log(' ---Validating Package---')
cli.log(` ${package.name}@${package.version}`)
scorecard.package = {'name' : package.name, 'version' : package.version}
resolve();
})
.then(() => {
//MUST Have a License P01
if (package.hasOwnProperty('license')){
cli.log(`✅ Package is ${package.license} licensed`)
scorecard.P01 = {'test' : true, 'license' : package.license}
} else {
cli.warn('No License Specified')
scorecard.P01 = {'test' : false}
}
})
.then(() => {
//MUST Have Repository or Bugs url/email P03
if (package.hasOwnProperty('repository') || package.hasOwnProperty('bugs')){
cli.log(`✅ Repository/Bugs Link Supplied`)
scorecard.P03 = {'test' : true}
} else {
cli.warn('P03 Please provide either a repository URL or a Bugs URL/Email')
scorecard.P03 = {'test' : false}
}
})
// Test P02 Disabled pending a more effieint method than nodegit
//.then(() => {
// // Check package in repository is the same name as the package.json (check for forks) - This may need more thought and testing P02
// if (package.hasOwnProperty('repository')){
// let repourl = package.repository.url
// let repo = p.basename(repourl)
// const repopath = os.tmpdir()+'/'+repo
// if (!fs.existsSync(repopath)){
// fs.mkdirSync(repopath);
// } else{
// fs.rmSync(repopath+'/', { recursive:true })
// }
// //Fix for repourls that contain credentials eg 'git@github.com:username/project.git'
// if (repourl.indexOf('@') != -1){ repourl = 'https://'+repourl.replace(':', '/').split('@')[1]}
// if (!isGitUrl(repourl)){
// cli.error('Invalid Repository URL: '+ repourl)
// }
// return nodegit.Clone(repourl, repopath)
// .then(function (r){
// if (package.repository.hasOwnProperty('directory')){
// let path = r.workdir()+package.repository.directory
// } else{
// let path = r.workdir()
// }
// let repopackage = require(path+'/package.json')
// if (package.name != repopackage.name){
// cli.warn('P02 Package name does not match package.json in repository')
// scorecard.P02 = {'test' : false}
// } else {
// cli.log('✅ Package Name Matches Repository')
// scorecard.P02 = {'test' : true}
// }
// })
// .catch((e) =>{
// cli.error('P02 Failed to clone git repository '+e)
// scorecard.P02 = {'test' : false}
// })
// } else {
// cli.warn('P02 No Repository listed in package.json')
// scorecard.P02 = {'test' : false}
// }
//})
.then(() => {
// P04 Naming
let legacy = false
const scopedRegex = new RegExp('@[a-z\\d][\\w-.]+/[a-z\\d][\\w-.]*');
if (npm_metadata){
//New packages should Use a Scoped name
let scoped_start= Date.parse('2022-02-01T00:00:00.000Z') //New Packages should use scoped names from 1st Feb 2021
let created = Date.parse(npm_metadata.time.created)
if (created<scoped_start){
legacy = true
}
}
if (!legacy){
if (scopedRegex.test(package.name)){
cli.log('✅ Package uses a Scoped Name')
scorecard.P04 = { 'test' : true}
} else {
cli.warn('P04 New Packages should use a scoped name')
scorecard.P04 = { 'test' : false}
}
}
if (!scopedRegex.test(package.name)) {
const contribRegex = new RegExp('^(node-red|nodered)(?!-contrib-).*', 'i')
if (!contribRegex.test(package.name)){
cli.log('✅ Package uses a valid name')
scorecard.P04 = { 'test' : true}
} else if (officialnodes.nodes.indexOf(package.name) > -1) {
cli.log('✅ Package is on the official nodes list')
scorecard.P04 = { 'test' : true}
} else {
cli.warn('P04 Packages using the node-red prefix in their name must use node-red-contrib')
scorecard.P04 = { 'test' : false}
}
} else {
cli.log('✅ Package uses a Scoped Name')
scorecard.P04 = { 'test' : true}
}
})
.then(() => {
//Check for other package of same name in different scope, ask about fork? P08
scorecard.P08 = {test : true}
const name = package.name.split('/').slice(-1) // Package name without scope
let similar = false
let similarlist = []
return axios.get('https://catalogue.nodered.org/catalogue.json')
.then(response => {
response.data.modules.forEach((m) => {
if (name.includes(m.id.split('/').slice(-1))){
cli.warn(`P08 Similar named package found at ${m.id}`)
similar = true
similarlist.push(m.id)
}
})
if (similar){
scorecard.P08 = { 'test' : false, 'similar': similarlist}
} else {
cli.log('✅ No similar named packages found')
scorecard.P08 = { 'test' : true}
}
return similar
})
scorecard.P08.test = !test
})
.then(() => {
//MUST have Node-RED in keywords P05
if (package.hasOwnProperty('keywords') && package.keywords.includes('node-red')){
cli.log(`✅ Node-RED Keyword Found`)
scorecard.P05 = { 'test' : true}
} else {
cli.warn('P05 Package.json keywords MUST contain node-red')
scorecard.P05 = { 'test' : true}
}
})
.then(() => {
//SHOULD declare min node-red version in node-red P06
if (package['node-red'].hasOwnProperty('version')){
return axios.get('https://registry.npmjs.org/node-red')
.then(response => {
let tags = response.data['dist-tags']
let supportedRegex = new RegExp('.*maintenance.*|^latest$') // check which versions have a latest or maintenance tag
let versions = []
Object.keys(tags).forEach(t => {
if (supportedRegex.test(t)) {
versions.push(tags[t])
}
});
//Test version against current versions
let compatible = false
let cv = []
versions.forEach(v => {
if (semver.satisfies(v, package['node-red'].version)){
cli.log(`✅ Compatible with Node-RED v${v}`)
compatible = true
scorecard.P06 = { 'test' : true}
cv.push(v)
} else {
cli.warn(`P06 NOT Compatible with Node-RED v${v}`)
}
if (!compatible){
cli.warn('P06 Not Compatible with any current Node-RED versions')
scorecard.P06 = { 'test' : false}
} else {
scorecard.P06.versions = cv
}
})
})
} else {
cli.warn('P06 Node-RED version compatibility not declared')
scorecard.P06 = { 'test' : false}
}
})
.then(() => {
//SHOULD declare min node version in engines P07
if ( package.hasOwnProperty('engines') && package.engines.hasOwnProperty('node')){
return axios.get('https://registry.npmjs.org/node-red')
.then(response => {
nminversion = semver.minVersion(response.data.versions[response.data["dist-tags"].latest].engines.node)
if (semver.satisfies(nminversion, package.engines.node)){
cli.log(`✅ Compatible NodeJS Version found ${nminversion}`)
scorecard.P07 = { 'test' : true}
scorecard.P07.version = package.engines.node
} else {
cli.warn('P07 Minimum Node version is not compatible with minimum supported Node-RED Version Node v'+nminversion)
scorecard.P07 = { 'test' : false}
scorecard.P07.version = package.engines.node
}
})
} else {
cli.warn('P07 Node version not declared in engines')
scorecard.P07 = { 'test' :false}
}
})
.then(() => {
return scorecard
})
.catch((e) => {
cli.error(e);
});
}
module.exports = checkpackage