@iobroker/repochecker 3.0.0-alpha.0 → 3.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,401 @@
1
+ 'use strict';
2
+ /*
3
+ This module is a support module for iobroker.repochecker
4
+
5
+ Area checked: package.json
6
+ Numbering : 001 - 099
7
+
8
+ */
9
+
10
+ const compareVersions = require('compare-versions');
11
+
12
+ const common = require('./common.js');
13
+
14
+ const recommendedNodeVersion = '18'; // This is the minimum node version that should be required
15
+ const requiredNodeVersion = '16'; // This is the minimum node version that must be required
16
+
17
+ /* TODO: configuration should be moved to context */
18
+
19
+ const dependenciesPackageJson = {
20
+ '@iobroker/adapter-core': {
21
+ required: '3.1.4',
22
+ recommended: '3.1.6',
23
+ },
24
+ };
25
+
26
+ const devDependenciesPackageJson = {
27
+ '@iobroker/testing': {
28
+ required: '4.1.3',
29
+ recommended: '4.1.3',
30
+ onlyWWW: false,
31
+ },
32
+ };
33
+
34
+ const blacklistPackageJson = {
35
+
36
+ };
37
+
38
+ async function getPackageJson(context) {
39
+ console.log('\ngetPackageJson');
40
+
41
+ const packageJson = await common.downloadFile(context.githubUrl, '/package.json');
42
+ context.packageJson = packageJson;
43
+ if (typeof context.packageJson === 'string') {
44
+ try {
45
+ context.packageJson = JSON.parse(context.packageJson);
46
+ } catch (e) {
47
+ context.errors.push(`[E001] Cannot parse package.json: ${e}`);
48
+ }
49
+ }
50
+ return context;
51
+ }
52
+
53
+ async function checkPackageJson(context) {
54
+ console.log('\ncheckPackageJson [E0xx]');
55
+ if (typeof context.packageJson === 'string') {
56
+ try {
57
+ context.packageJson = JSON.parse(context.packageJson);
58
+ } catch (e) {
59
+ context.errors.push(`[E001] Cannot parse package.json: ${e}`);
60
+ return context;
61
+ }
62
+ }
63
+
64
+ if (!context.githubUrlOriginal.match(/\/iobroker\./i)) {
65
+ context.errors.push('[E002] No "ioBroker." found in the name of repository');
66
+ } else {
67
+ context.checks.push('"ioBroker" was found in the name of repository');
68
+ }
69
+
70
+ if (context.githubUrlOriginal.includes('/iobroker.')) {
71
+ context.errors.push('[E003] Repository must have name ioBroker.adaptername, but now io"b"roker is in lowercase');
72
+ } else {
73
+ context.checks.push('Repository has name ioBroker.adaptername (not iobroker.adaptername)');
74
+ }
75
+
76
+ const m = context.githubUrlOriginal.match(/\/ioBroker\.(.*)$/);
77
+ let adapterName = '';
78
+ if (!m || !m[1]) {
79
+ context.errors.push(`[E004] No adapter name found in URL: ${context.githubUrlOriginal}`);
80
+ } else {
81
+ context.checks.push('Adapter name found in the URL');
82
+ adapterName = m[1].replace(/\/master$/, '').replace(/\/main$/, '');
83
+ }
84
+
85
+ context.adapterName = adapterName;
86
+
87
+ if (adapterName.match(/[A-Z]/)) {
88
+ context.errors.push('[E005] Adapter name must be lowercase');
89
+ } else {
90
+ context.checks.push('Adapter name is lowercase');
91
+ }
92
+
93
+ if (adapterName.match(/[^-_a-z\d]/)) {
94
+ context.errors.push(`[E006] Invalid characters found in adapter name "${adapterName}". Only lowercase chars, digits, "-" and "_" are allowed`);
95
+ } else {
96
+ context.checks.push(`No invalid characters found in "${adapterName}"`);
97
+ }
98
+
99
+ if (adapterName.startsWith('_')) {
100
+ context.errors.push(`[E024] Adapter name "${adapterName}" may not start with '_'`);
101
+ } else {
102
+ context.checks.push(`Adapter name "${adapterName}" does not start with '_'`);
103
+ }
104
+
105
+ const n = context.githubUrlOriginal.match(/\/([^/]+)\/iobroker\./i);
106
+ if (!n || !n[1]) {
107
+ context.errors.push('[E007] Cannot find author repo in the URL');
108
+ } else {
109
+ context.authorName = n[1];
110
+ }
111
+
112
+ if (context.packageJson.name !== `iobroker.${adapterName.toLowerCase()}`) {
113
+ context.errors.push(`[E020] Name of adapter in package.json must be lowercase and be equal to "iobroker.${adapterName.toLowerCase()}". Now is "${packageJson.name}"`);
114
+ } else {
115
+ context.checks.push(`Name of adapter in package.json must be lowercase and be equal to "iobroker.${adapterName.toLowerCase()}".`);
116
+ }
117
+
118
+ if (!context.packageJson.version) {
119
+ context.errors.push('[E009] No version found in the package.json');
120
+ } else {
121
+ context.checks.push('Version found in package.json');
122
+ }
123
+
124
+ if (!context.packageJson.description) {
125
+ context.errors.push('[E010] No description found in the package.json');
126
+ } else {
127
+ context.checks.push('Description found in package.json');
128
+ }
129
+
130
+ if (context.packageJson.licenses) {
131
+ context.errors.push('[E021] "licenses" in package.json are deprecated. Please remove and use "license": "NAME" field.');
132
+ } else {
133
+ context.checks.push('No "licenses" found in package.json');
134
+ }
135
+
136
+ if (!context.packageJson.author) {
137
+ context.errors.push('[E013] No author found in the package.json');
138
+ } else {
139
+ context.checks.push('Author found in package.json');
140
+ }
141
+
142
+ if (context.packageJson._args) {
143
+ context.errors.push('[E014] NPM information found in package.json. Please remove all attributes starting with "_"');
144
+ } else {
145
+ context.checks.push('No npm generated attributes found in package.json');
146
+ }
147
+
148
+ if (!context.packageJson.license) {
149
+ context.errors.push('[E015] No license found in package.json');
150
+ } else {
151
+ context.checks.push('"license" found in package.json');
152
+
153
+ // check if license valid
154
+ if (!context.cfg.allowedLicenses.includes(context.packageJson.license)) {
155
+ context.errors.push(`[E016] ${context.packageJson.license} found in package.json is no valid SPDX license. Please use one of listed here: https://spdx.org/licenses/`);
156
+ } else {
157
+ context.checks.push('"license" is valid in package.json');
158
+ }
159
+ }
160
+
161
+ if (!context.packageJson.repository) {
162
+ context.errors.push('[E017] No repository found in the package.json');
163
+ } else {
164
+ context.checks.push('Repository found in package.json');
165
+
166
+ const allowedRepoUrls = [
167
+ context.githubApiData.html_url, // https://github.com/klein0r/ioBroker.luftdaten
168
+ `git+${context.githubApiData.html_url}`, // git+https://github.com/klein0r/ioBroker.luftdaten
169
+ context.githubApiData.git_url, // git://github.com/klein0r/ioBroker.luftdaten.git
170
+ context.githubApiData.ssh_url, // git@github.com:klein0r/ioBroker.luftdaten.git
171
+ context.githubApiData.clone_url, // https://github.com/klein0r/ioBroker.luftdaten.git
172
+ `git+${context.githubApiData.clone_url}` // git+https://github.com/klein0r/ioBroker.luftdaten.git
173
+ ];
174
+
175
+ // https://docs.npmjs.com/cli/v7/configuring-npm/package-json#repository
176
+ if (context.packageJson.repository && typeof context.packageJson.repository === 'object') {
177
+ if (context.packageJson.repository.type !== 'git') {
178
+ context.errors.push(`[E018] Invalid repository type in package.json: ${context.packageJson.repository.type}. It should be git`);
179
+ } else {
180
+ context.checks.push('Repository type is valid in package.json: git');
181
+ }
182
+
183
+ if (!allowedRepoUrls.includes(context.packageJson.repository.url)) {
184
+ context.errors.push(`[E019] Invalid repository URL in package.json: ${context.packageJson.repository.url}. Expected: ${context.githubApiData.ssh_url} or ${context.githubApiData.clone_url}`);
185
+ } else {
186
+ context.checks.push('Repository URL is valid in package.json');
187
+ }
188
+ } else if (context.packageJson.repository && typeof context.packageJson.repository === 'string') {
189
+ if (!allowedRepoUrls.includes(context.packageJson.repository)) {
190
+ context.errors.push(`[E019] Invalid repository URL in package.json: ${context.packageJson.repository}. Expected: ${context.githubApiData.ssh_url} or ${context.githubApiData.clone_url}`);
191
+ } else {
192
+ context.checks.push('Repository URL is valid in package.json');
193
+ }
194
+ } else {
195
+ context.errors.push('[E019] Invalid repository URL in package.json');
196
+ }
197
+ }
198
+
199
+ if (context.cfg.reservedAdapterNames.includes(adapterName)) {
200
+ context.errors.push('[E022] Adapter name is reserved. Please rename adapter.');
201
+ } else {
202
+ context.checks.push('Adapter name is not reserved');
203
+ }
204
+
205
+ if (!context.ioPackageJson.common.onlyWWW && !context.packageJson.dependencies) {
206
+ context.errors.push('[W030] No dependencies declared at package.json. Is this really correct?');
207
+ }
208
+ if (!context.packageJson.devDependencies) {
209
+ context.errors.push('[E031] No devDependencies declared at package.json. Please correct package.json');
210
+ }
211
+
212
+ if ((context.packageJson.dependencies && context.packageJson.dependencies.npm) || (context.packageJson.optionalDependencies && context.packageJson.optionalDependencies.npm)) {
213
+ context.errors.push('[E023] Do not include "npm" as dependency!');
214
+ } else {
215
+ context.checks.push('npm is not in dependencies');
216
+ }
217
+
218
+ if ((context.packageJson.dependencies && context.packageJson.dependencies['iobroker.js-controller']) ||
219
+ (context.packageJson.devDependencies && context.packageJson.devDependencies['iobroker.js-controller']) ||
220
+ (context.packageJson.optionalDependencies && context.packageJson.optionalDependencies['iobroker.js-controller'])
221
+ ) {
222
+ context.errors.push('[E025] Do not include "iobroker.js-controller" as dependency!');
223
+ } else {
224
+ context.checks.push('iobroker.js-controller is not in dependencies');
225
+ }
226
+
227
+ if (!context.ioPackageJson.common.onlyWWW) {
228
+ if (!context.packageJson.engines) {
229
+ context.errors.push(`[E026] "{'engines': {'node'>='${requiredNodeVersion}'}}" is required at package.json, "{'engines':{'node'>='${recommendedNodeVersion}'}}" is recommended`);
230
+ } else {
231
+ if (!context.packageJson.engines.node) {
232
+ context.errors.push(`[E026] "{'engines': {'node'>='${requiredNodeVersion}'}}" is required at package.json, "{'engines':{'node'>='${recommendedNodeVersion}'}}" is recommended`);
233
+ } else {
234
+ context.checks.push('engines attribute containing node requirements exist.');
235
+ // 'engines': { 'node': '>= 18' }
236
+ // 'engines': { 'node': '>= 18.1.2' }
237
+ // 'engines': { 'node': '>= 18.1.2 < 19' }
238
+ const nodeVal = context.packageJson.engines.node;
239
+ const match = nodeVal.match(/^(?<cmp>[<>=~]+)?\s*(?<vers>\d+(\.\d+(\.\d+)?)?(-\w+\.\d+)?)/m);
240
+ if ( ! match ) {
241
+ context.warnings.push(`[W027] {'engines' : { 'node' : '${nodeVal}' } }" is not parseable.`);
242
+ } else {
243
+ common.debug( `node check: ${JSON.stringify(match.groups)}`);
244
+ if ( match.groups.cmp !== '>' && match.groups.cmp !== '>=' ) {
245
+ context.warnings.push(`[W028] Minimum node.js version ${recommendedNodeVersion} recommended. Please adapt "{'engines' : { 'node' >= '${match.groups.vers}' } }" at package.json.`);
246
+ } else {
247
+ if ( ! compareVersions.compare( match.groups.vers, requiredNodeVersion, '>=')) {
248
+ context.errors.push(`[E029] Node.js ${requiredNodeVersion} is required as minimum, node.js ${recommendedNodeVersion} is recommended. Please adapt "{'engines' : { 'node' >= '${match.groups.vers}' } }" at package.json.`);
249
+ } else if ( ! compareVersions.compare( match.groups.vers, recommendedNodeVersion, '>=')) {
250
+ context.warnings.push(`[W028] Minimum node.js version ${recommendedNodeVersion} recommended. Please adapt "{'engines' : { 'node' >= '${match.groups.vers}' } }" at package.json.`);
251
+ } else {
252
+ context.checks.push(`Correct node.js version ${match.groups.vers} requested by "engines" attribute at package.json.`);
253
+ }
254
+ }
255
+ }
256
+ }
257
+ }
258
+ } else {
259
+ context.checks.push('"engines" check skipped for wwwOnly adapter.');
260
+ }
261
+
262
+ if (!context.ioPackageJson.common.onlyWWW) {
263
+ for (const dependency in dependenciesPackageJson) {
264
+ const requiredVersion = dependenciesPackageJson[dependency].required;
265
+ const recommendedVersion = dependenciesPackageJson[dependency].recommended;
266
+ let dependencyVersion = context.packageJson.dependencies[`${dependency}`] || '';
267
+ dependencyVersion = dependencyVersion.replace(/[\^~]/,'' );
268
+ if (!dependencyVersion) {
269
+ context.errors.push(`[E032] No dependency declared for ${dependency}. Please add "${dependency}":"${recommendedVersion}" to dependencies at package.json`);
270
+ } else if (! compareVersions.compare( dependencyVersion, requiredVersion, '>=' )) {
271
+ context.errors.push(`[E033] ${dependency} ${dependencyVersion} specified. ${requiredVersion} is required as minimum, ${recommendedVersion} is recommended. Please update dependencies at package.json`);
272
+ } else if (! compareVersions.compare( dependencyVersion, recommendedVersion, '>=' )) {
273
+ context.warnings.push(`[W034] ${dependency} ${dependencyVersion} specified. ${recommendedVersion} is recommended. Please consider updating dependencies at package.json`);
274
+ } else {
275
+ context.checks.push('dependency ${dependency} ${dependencyVersion} is ok');
276
+ }
277
+ }
278
+ context.checks.push('"dependenciesPackageJson" checked.');
279
+ } else {
280
+ context.checks.push('"dependenciesPackageJson" check skipped for wwwOnly adapter.');
281
+ }
282
+
283
+ for (const dependency in devDependenciesPackageJson) {
284
+ if ((devDependenciesPackageJson[dependency].onlyWWW === false) && context.ioPackageJson.common.onlyWWW) continue;
285
+ const requiredVersion = devDependenciesPackageJson[dependency].required;
286
+ const recommendedVersion = devDependenciesPackageJson[dependency].recommended;
287
+ let dependencyVersion = context.packageJson.devDependencies[`${dependency}`] || '';
288
+ dependencyVersion = dependencyVersion.replace(/[\^~]/,'' );
289
+ if (!dependencyVersion) {
290
+ context.errors.push(`[E035] No devDependency declared for ${dependency}. Please add "${dependency}":"${recommendedVersion}" to devDependencies at package.json`);
291
+ } else if (! compareVersions.compare( dependencyVersion, requiredVersion, '>=' )) {
292
+ context.errors.push(`[E036] ${dependency} ${dependencyVersion} specified. ${requiredVersion} is required as minimum, ${recommendedVersion} is recommended. Please update devDependencies at package.json`);
293
+ } else if (! compareVersions.compare( dependencyVersion, recommendedVersion, '>=' )) {
294
+ context.warnings.push(`[W037] ${dependency} ${dependencyVersion} specified. ${recommendedVersion} is recommended. Please consider updating devDependencies at package.json`);
295
+ } else {
296
+ context.checks.push('devDependency ${dependency} ${dependencyVersion} is ok');
297
+ }
298
+ }
299
+ context.checks.push('"devDependenciesPackageJson" checked.');
300
+
301
+ const enforcedDependencies = [
302
+ '@iobroker/adapter-core',
303
+ '@alcalzone/release-script',
304
+ '@alcalzone/release-script-plugin-iobroker',
305
+ '@alcalzone/release-script-plugin-license',
306
+ '@alcalzone/release-script-manual-review',
307
+ '@iobroker/adapter-dev',
308
+ '@iobroker/testing'
309
+ ];
310
+ for (const dependency in context.packageJson.dependencies) {
311
+ if (!context.packageJson.dependencies[dependency].startsWith('^') &&
312
+ !context.packageJson.dependencies[dependency].startsWith('~') &&
313
+ !context.packageJson.dependencies[dependency].startsWith('>')
314
+ ) {
315
+ if (context.packageJson.dependencies[dependency].toLowerCase().includes('github.com')) {
316
+ context.warnings.push(`[W043] dependency should not require a github version. Please change "${dependency}:${context.packageJson.dependencies[dependency]}"`);
317
+ } else if (enforcedDependencies.includes(dependency)) {
318
+ context.errors.push(`[E044] dependency must not require a specific version. Use "~1.2.3" or "^1.2.3" syntax. Please update "${dependency}:${context.packageJson.dependencies[dependency]}"`);
319
+ } else {
320
+ context.warnings.push(`[W044] dependency should not require a specific version. Use "~1.2.3" or "^1.2.3" syntax. Please update "${dependency}:${context.packageJson.dependencies[dependency]}"`);
321
+ }
322
+ }
323
+ }
324
+
325
+ for (const dependency in context.packageJson.devDependencies) {
326
+ if (!context.packageJson.devDependencies[dependency].startsWith('^') &&
327
+ !context.packageJson.devDependencies[dependency].startsWith('~') &&
328
+ !context.packageJson.devDependencies[dependency].startsWith('>')
329
+ ) {
330
+ if (context.packageJson.devDependencies[dependency].toLowerCase().includes('github.com')) {
331
+ context.warnings.push(`[W045] devDependency should not require github versions. Please change "${dependency}:${context.packageJson.devDependencies[dependency]}"`);
332
+ } else if (enforcedDependencies.includes(dependency)) {
333
+ context.errors.push(`[E046] devDependency must not require a specific version. Use "~1.2.3" or "^1.2.3" syntax. Please update "${dependency}:${context.packageJson.devDependencies[dependency]}"`);
334
+ } else {
335
+ context.warnings.push(`[W046] devDependency should not require a specific version. Use "~1.2.3" or "^1.2.3" syntax. Please update "${dependency}:${context.packageJson.devDependencies[dependency]}"`);
336
+ }
337
+ }
338
+ }
339
+
340
+ for (const blacklist in blacklistPackageJson) {
341
+ // console.log(`checking blacklist ${blacklist}`);
342
+ let tmp = context.packageJson;
343
+ let log = '';
344
+ for (const element of blacklist.split('.')) {
345
+ log = `${log}.${element}`;
346
+ //console.log(` check ${log}`);
347
+ tmp = tmp[element];
348
+ if (!tmp) {
349
+ //console.log(` ${log} does not exist`);
350
+ break;
351
+ }
352
+ }
353
+ if (tmp) {
354
+ if (blacklistPackageJson[blacklist].err) {
355
+ context.errors.push(`[E038] ${blacklistPackageJson[blacklist].msg}`);
356
+ } else {
357
+ context.warnings.push(`[W038] ${blacklistPackageJson[blacklist].msg}`);
358
+ }
359
+ }
360
+ // else {
361
+ // console.log(`blacklist ${blacklist} no match`);
362
+ // }
363
+ }
364
+ context.checks.push('"blacklist (package)" checked.');
365
+
366
+ if (! context.packageJson.keywords) {
367
+ context.errors.push('[E039] "keywords" must be an array within package.json and contain some useful keywords');
368
+ } else if (!Array.isArray(context.packageJson.keywords)) {
369
+ context.errors.push('[E039] "keywords" must be an array within package.json and contain some useful keywords');
370
+ } else {
371
+ const forbiddenKeywords = [];
372
+ const ignoredKeywords = ['iobroker', 'smart home', 'smarthome', 'home automation', 'template'];
373
+ const recommendedKeywords = ['ioBroker'];
374
+ //console.log(`[DEBUG] package.keywords: "${context.packageJson.keywords.join(', ')}"`);
375
+ //console.log(`[DEBUG] filtered: "${context.packageJson.keywords.filter(keyword => !ignoredKeywords.includes(keyword.toLowerCase()))}"`);
376
+ if (context.packageJson.keywords.filter(keyword => !ignoredKeywords.includes(keyword.toLowerCase())).length === 0 ) {
377
+ context.errors.push(`[E039] "keywords" within package.json must contain some keywords besides "${context.packageJson.keywords.join(', ')}" related to adapter`);
378
+ }
379
+ if (! recommendedKeywords.filter(keyword => context.packageJson.keywords.includes(keyword)).length > 0) {
380
+ context.warnings.push(`[W040] "keywords" within package.json should contain "${recommendedKeywords.join(', ')}"`);
381
+ }
382
+ if (forbiddenKeywords.filter(keyword => context.packageJson.keywords.map(k => k.toLowerCase()).includes(keyword)).length > 0) {
383
+ context.warnings.push(`[W041] "keywords" within package.json should not contain "${forbiddenKeywords.join(', ')}"`);
384
+ }
385
+
386
+ context.checks.push('"keywords" found in package.json and refers to an array');
387
+ }
388
+
389
+ if (context.packageJson.globalDependencies) {
390
+ context.errors.push('[E042] "globalDependencies" is misplaced at package.json. Did you mean "common.globalDependencies" at io-package.json?');
391
+ } else {
392
+ context.checks.push('"globalDependencies" not found in package.json');
393
+ }
394
+
395
+ // max number is W046
396
+
397
+ return context;
398
+ }
399
+
400
+ exports.getPackageJson = getPackageJson;
401
+ exports.checkPackageJson = checkPackageJson;