@iobroker/repochecker 3.0.0-alpha.1 → 3.0.0-alpha.3

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,480 @@
1
+ 'use strict';
2
+ /*
3
+ This module is a support module for iobroker.repochecker
4
+
5
+ Area checked: Adapter Code
6
+ Numbering : 500 - 599
7
+
8
+ */
9
+
10
+ const compareVersions = require('compare-versions');
11
+ const JSON5 = require('json5');
12
+ const stream = require('node:stream');
13
+ const unzipper = require('unzipper');
14
+ const util = require('node:util');
15
+ const Writable = stream.Writable;
16
+
17
+
18
+ const common = require('./common.js');
19
+
20
+ const memStore = {};
21
+
22
+ /* Writable memory stream */
23
+ function WMStrm(key, options) {
24
+ // allow use without a new operator
25
+ if (!(this instanceof WMStrm)) {
26
+ return new WMStrm(key, options);
27
+ }
28
+ Writable.call(this, options); // init super
29
+ this.key = key; // save key
30
+ memStore[key] = Buffer.from(''); // empty
31
+ }
32
+
33
+ util.inherits(WMStrm, Writable);
34
+
35
+ WMStrm.prototype._write = function (chunk, enc, cb) {
36
+ // our memory store stores things in buffers
37
+ const buffer = Buffer.isBuffer(chunk) ?
38
+ chunk : // already is Buffer use it
39
+ new Buffer(chunk, enc); // string, convert
40
+
41
+ // concat to the buffer already there
42
+ memStore[this.key] = Buffer.concat([memStore[this.key], buffer]);
43
+ cb();
44
+ };
45
+
46
+ // utility to parse words.js
47
+ function extractWords(words) {
48
+ try {
49
+ const lines = words.split(/\r\n|\r|\n/g);
50
+ let i = 0;
51
+ while (!lines[i].match(/^systemDictionary = {/)) {
52
+ i++;
53
+ }
54
+ lines.splice(0, i);
55
+
56
+ // remove last empty lines
57
+ i = lines.length - 1;
58
+ while (!lines[i]) {
59
+ i--;
60
+ }
61
+ if (i < lines.length - 1) {
62
+ lines.splice(i + 1);
63
+ }
64
+
65
+ lines[0] = lines[0].replace('systemDictionary = ', '');
66
+ lines[lines.length - 1] = lines[lines.length - 1].trim().replace(/};$/, '}');
67
+ words = lines.join('\n');
68
+ const resultFunc = new Function(`return ${words};`);
69
+
70
+ return resultFunc();
71
+ } catch (e) {
72
+ return null;
73
+ }
74
+ }
75
+
76
+ // E5xx
77
+ async function checkCode(context) {
78
+ console.log('\ncheckCode [E5xx]');
79
+
80
+ const readFiles = [
81
+ '.npmignore',
82
+ '.gitignore',
83
+ 'iob_npm.done',
84
+ '.travis.yml',
85
+ 'gulpfile.js',
86
+ '.releaseconfig.json',
87
+
88
+ // Add all potential files anyway. If they exist, they must be valid.
89
+ // If they are unnecessary, a warning could be issued
90
+ 'admin/index_m.html',
91
+ 'admin/words.js',
92
+ 'admin/jsonConfig.json',
93
+ 'admin/jsonConfig.json5',
94
+ 'admin/jsonCustom.json',
95
+ 'admin/jsonCustom.json5',
96
+ 'admin/blockly.js',
97
+
98
+ 'src-admin/package.json', // check if react is used
99
+ 'src-widgets/package.json', // check if react is used
100
+ 'src/package.json', // check if react is used
101
+ ];
102
+
103
+ context.cfg.allowedLanguages.forEach(lang =>
104
+ readFiles.push(`admin/i18n/${lang}/translations.json`)
105
+ );
106
+
107
+ context.cfg.allowedLanguages.forEach(lang =>
108
+ readFiles.push(`admin/i18n/${lang}.json`)
109
+ );
110
+
111
+ if (context.packageJson.main) {
112
+ readFiles.push(context.packageJson.main);
113
+ }
114
+
115
+ // if (!context.ioPackageJson.common.noConfig) {
116
+ // if (context.ioPackageJson.common.materialize || (context.ioPackageJson.common.adminUI && context.ioPackageJson.common.adminUI.config === 'materialize')) {
117
+ // readFiles.push('admin/index_m.html');
118
+ // readFiles.push('admin/words.js');
119
+ // }
120
+ //
121
+ // if (context.ioPackageJson.common.adminUI && context.ioPackageJson.common.adminUI.config === 'json') {
122
+ // readFiles.push('admin/jsonConfig.json');
123
+ // readFiles.push('admin/jsonConfig.json5');
124
+ // allowedLanguages.forEach(lang =>
125
+ // readFiles.push(`admin/i18n/${lang}/translations.json`));
126
+ // allowedLanguages.forEach(lang =>
127
+ // readFiles.push(`admin/i18n/${lang}.json`));
128
+ // }
129
+ //
130
+ // if (context.ioPackageJson.common.supportCustoms || context.ioPackageJson.common.jsonCustom || (context.ioPackageJson.common.adminUI && context.ioPackageJson.common.adminUI.custom === 'json')) {
131
+ // readFiles.push('admin/jsonCustom.json');
132
+ // readFiles.push('admin/jsonCustom.json5');
133
+ // }
134
+ // }
135
+ //
136
+ // if (context.ioPackageJson.common.blockly) {
137
+ // readFiles.push('admin/blockly.js');
138
+ // }
139
+
140
+ // https://github.com/userName/ioBroker.adaptername/archive/${context.branch}.zip
141
+ const data = await common.downloadFile(context.githubUrlOriginal, `/archive/${context.branch}.zip`, true)
142
+ console.log(`${context.branch}.zip ${data.length} bytes`);
143
+
144
+ let found = false;
145
+ const bufferStream = new stream.PassThrough();
146
+ bufferStream.end(data);
147
+ context.filesList = [];
148
+
149
+ // parse zip file
150
+ const promises = [];
151
+ await (function () { return new Promise(_resolve => {
152
+ common.debug('init bufferstream');
153
+ bufferStream
154
+ .pipe(unzipper.Parse())
155
+ .on('entry', entry => {
156
+ common.debug('on.entry ' + entry.path);
157
+ if (!found && entry.type === 'Directory' && entry.path.match(/\/node_modules\/$/)) {
158
+ console.log(`Found ${entry.path}`);
159
+ found = true;
160
+ context.errors.push('[E500] node_modules found in repo. Please delete it');
161
+ }
162
+
163
+ // Get a list of all files and `.npmignore` + `.gitignore`
164
+ const name = entry.path.replace(/^[^/]+\//, '');
165
+ context.filesList.push(name);
166
+
167
+ if (readFiles.includes(name)) {
168
+ promises.push(new Promise(resolve => {
169
+ const wstream = new WMStrm(name);
170
+ wstream.on('finish', () => {
171
+ context[`/${name}`] = memStore[name].toString();
172
+ resolve(context[`/${name}`]);
173
+ });
174
+ entry.pipe(wstream);
175
+ }));
176
+ } else {
177
+ entry.autodrain();
178
+ }
179
+ })
180
+ .on('error', () => {
181
+ common.debug('on.error');
182
+ context.errors.push(`[E501] Cannot get ${context.branch}.zip on github`);
183
+ return Promise.all(promises).then(() => _resolve(context));
184
+ })
185
+ .on('close', () => {
186
+ common.debug('on.close');
187
+ return Promise.all(promises).then(() => _resolve(context));
188
+ });
189
+ });
190
+ })();
191
+
192
+ let usesReact = false;
193
+ if (context.packageJson.devDependencies &&
194
+ (context.packageJson.devDependencies['@iobroker/adapter-react-v5'] || context.packageJson.devDependencies['react'])) {
195
+ console.log('REACT detected at package devDependencies');
196
+ usesReact = true;
197
+ }
198
+ if (context.packageJson.dependencies &&
199
+ (context.packageJson.dependencies['@iobroker/adapter-react-v5'] || context.packageJson.dependencies['react'])) {
200
+ console.log('REACT detected at package dependencies');
201
+ usesReact = true;
202
+ }
203
+ if (context['/src-admin/package.json']) {
204
+ //console.log('"src-admin/package.json" exists');
205
+ const packageJson = JSON.parse(context['/src-admin/package.json']);
206
+ if (packageJson.devDependencies && packageJson.devDependencies['@iobroker/adapter-react-v5'] ) {
207
+ console.log('REACT detected at src-admin/package devDependencies');
208
+ usesReact = true;
209
+ }
210
+ if (packageJson.dependencies && packageJson.dependencies['@iobroker/adapter-react-v5'] ) {
211
+ console.log('REACT detected at src-admin/package dependencies');
212
+ usesReact = true;
213
+ }
214
+ }
215
+ if (context['/src-widgets/package.json']) {
216
+ //console.log('"src-widgets/package.json" exists');
217
+ const packageJson = JSON.parse(context['/src-widgets/package.json']);
218
+ if (packageJson.devDependencies && (
219
+ packageJson.devDependencies['@iobroker/adapter-react-v5'] || packageJson.devDependencies['react'])) {
220
+ console.log('REACT detected at src-widgets/package devDependencies');
221
+ usesReact = true;
222
+ }
223
+ if (packageJson.dependencies && (
224
+ packageJson.dependencies['@iobroker/adapter-react-v5'] || packageJson.dependencies['react'])) {
225
+ console.log('REACT detected at src-widgets/package dependencies');
226
+ usesReact = true;
227
+ }
228
+ }
229
+ if (context['/src/package.json']) {
230
+ //console.log('"src/package.json" exists');
231
+ const packageJson = JSON.parse(context['/src/package.json']);
232
+ if (packageJson.devDependencies && packageJson.devDependencies['@iobroker/adapter-react-v5'] ) {
233
+ // console.log('REACT detected');
234
+ usesReact = true;
235
+ }
236
+ if (packageJson.dependencies && packageJson.dependencies['@iobroker/adapter-react-v5'] ) {
237
+ // console.log('REACT detected');
238
+ usesReact = true;
239
+ }
240
+ }
241
+
242
+ if (! usesReact && !context.ioPackageJson.common.noConfig &&
243
+ (!context.ioPackageJson.common.adminUI ||
244
+ (context.ioPackageJson.common.adminUI.config !== 'json' && context.ioPackageJson.common.adminUI.config !== 'none')
245
+ )) {
246
+ context.warnings.push('[S522] Please consider migrating to admin 5 UI (jsonConfig).');
247
+ }
248
+
249
+ if (context.ioPackageJson.common.materialize || (context.ioPackageJson.common.adminUI && context.ioPackageJson.common.adminUI.config === 'materialize')) {
250
+ if (context['/admin/index_m.html'] && context['/admin/index_m.html'].includes('selectID.js') && !context.filesList.includes('admin/img/info-big.png')) {
251
+ context.errors.push('[E502] "admin/img/info-big.png" not found, but selectID.js used in index_m.html ');
252
+ }
253
+
254
+ if (context['/admin/words.js']) {
255
+ // at least 3 languages must be in
256
+ const words = extractWords(context['/admin/words.js']);
257
+ if (words) {
258
+ const problem = Object.keys(words).filter(word => !words[word].de || !words[word].ru);
259
+ if (problem.length > 3) {
260
+ context.errors.push(`[E506] More non translated in german or russian words found in admin/words.js. You can use https://translator.iobroker.in/ for translations`);
261
+ } else {
262
+ problem.forEach(word => {
263
+ if (!words[word].de) {
264
+ context.errors.push(`[E506] Word "${word}" is not translated to german in admin/words.js. You can use https://translator.iobroker.in/ for translations`);
265
+ }
266
+ if (!words[word].ru) {
267
+ context.errors.push(`[E506] Word "${word}" is not translated to russian in admin/words.js. You can use https://translator.iobroker.in/ for translations`);
268
+ }
269
+ });
270
+ }
271
+ }
272
+ } else {
273
+ context.checks.push('admin/words.js found.');
274
+ }
275
+ }
276
+
277
+ if (context.ioPackageJson.common.adminUI && context.ioPackageJson.common.adminUI.config === 'json') {
278
+ let jsonConfig;
279
+ if (context['/admin/jsonConfig.json'] || context['/admin/jsonConfig.json5']) {
280
+ try {
281
+ jsonConfig = context['/admin/jsonConfig.json'] ? JSON.parse(context['/admin/jsonConfig.json']) : JSON5.parse(context['/admin/jsonConfig.json5']);
282
+ } catch (e) {
283
+ context.errors.push(`[E507] Cannot parse "admin/jsonConfig.json${context['/admin/jsonConfig.json'] ? '' : '5'}": ${e}`);
284
+ }
285
+ } else {
286
+ context.errors.push(`[E508] "admin/jsonConfig.json${context['/admin/jsonConfig.json'] ? '' : '5'}" not found, but admin support is declared`);
287
+ }
288
+
289
+ if (jsonConfig && jsonConfig.i18n === true) {
290
+ context.cfg.allowedLanguages.forEach(lang => {
291
+ if (context[`/admin/i18n/${lang}/translations.json`]) {
292
+ try {
293
+ JSON.parse(context[`/admin/i18n/${lang}/translations.json`]);
294
+ } catch (e) {
295
+ context.errors.push(`[E509] Cannot parse "admin/i18n/${lang}/translations.json": ${e}`);
296
+ }
297
+ } else if (context[`/admin/i18n/${lang}.json`]) {
298
+ try {
299
+ JSON.parse(context[`/admin/i18n/${lang}.json`]);
300
+ } catch (e) {
301
+ context.errors.push(`[E509] Cannot parse "admin/i18n/${lang}.json": ${e}`);
302
+ }
303
+ } else {
304
+ context.warnings.push(`[W510] "/admin/i18n/${lang}/translations.json" or "admin/i18n/${lang}.json" not found, but admin support is declared. Please add.`);
305
+ }
306
+ });
307
+ }
308
+ if (jsonConfig && jsonConfig.i18n === false) {
309
+ context.warnings.push(`[W515] Why did you decide to disable i18n support?`);
310
+ }
311
+ }
312
+
313
+ if (context.ioPackageJson.common.supportCustoms || context.ioPackageJson.common.jsonCustom || (context.ioPackageJson.common.adminUI && context.ioPackageJson.common.adminUI.custom === 'json')) {
314
+ if (context['/admin/jsonCustom.json'] || context['/admin/jsonCustom.json5']) {
315
+ try {
316
+ context['/admin/jsonCustom.json'] ? JSON.parse(context['/admin/jsonCustom.json']) : JSON5.parse(context['/admin/jsonCustom.json5']);
317
+ } catch (e) {
318
+ context.errors.push(`[E511] Cannot parse "admin/jsonCustom.json${context['/admin/jsonCustom.json'] ? '' : '5'}": ${e}`);
319
+ }
320
+ } else {
321
+ context.errors.push(`[E512] "admin/jsonCustom.json${context['/admin/jsonCustom.json'] ? '' : '5'}" not found, but custom support is declared`);
322
+ }
323
+ }
324
+
325
+ if (context.ioPackageJson.common.blockly && !context['/admin/blockly.js']) {
326
+ context.errors.push('[E514] "admin/blockly.js" not found, but blockly support is declared');
327
+ }
328
+
329
+ if (context.ioPackageJson.common.javascriptRules) {
330
+ if (!context.ioPackageJson.common.javascriptRules.url) {
331
+ context.errors.push('[E517] JavaScript-Rules support is declared, but no location in property common.javascriptRules.url defined');
332
+ }
333
+ if (!context.filesList.includes('admin/' + context.ioPackageJson.common.javascriptRules.url)) {
334
+ context.errors.push(`[E516] "${context.ioPackageJson.common.javascriptRules.url}" not found, but JavaScript-Rules support is declared`);
335
+ }
336
+ }
337
+
338
+ const forbiddenFiles = [
339
+ 'iob_npm.done',
340
+ 'iob',
341
+ 'iobroker'
342
+ ];
343
+
344
+ forbiddenFiles.forEach( file => {
345
+ if (context.filesList.includes(file)) {
346
+ context.errors.push(`[E503] File "${file}" found in repo! Please remove file.`);
347
+ }
348
+ });
349
+
350
+ if (!context.filesList.includes('package-lock.json')) {
351
+ context.warnings.push('[W523] "package-lock.json" not found in repo! Please remove from .gitignore!');
352
+ }
353
+
354
+ if (context['/.travis.yml']) {
355
+ context.hasTravis = true;
356
+ }
357
+
358
+ if (context['/gulpfile.js']) {
359
+ if (!usesReact) {
360
+ context.warnings.push('[W513] "gulpfile.js" found in repo! Think about migrating to @iobroker/adapter-dev package');
361
+ }
362
+ if (!context.packageJson.devDependencies['gulp']) {
363
+ context.warnings.push('[W520] "gulpfile.js" found in repo but "gulp" not found at devDependencies at package.json. Check whether it can be removed.');
364
+ }
365
+ } else {
366
+ if (context.packageJson.devDependencies['gulp']) {
367
+ context.warnings.push('[W521] "gulp" found at devDependencies at package.json but no "gulpfile.js" found. Is this dependency really required?');
368
+ }
369
+ }
370
+
371
+ if (context.packageJson.devDependencies && context.packageJson.devDependencies['@alcalzone/release-script'] ) {
372
+ const version = context.packageJson.devDependencies['@alcalzone/release-script'];
373
+ if ( compareVersions.compareVersions( version, '3.0.0' ) >= 0) {
374
+ if (!context['/.releaseconfig.json']) {
375
+ context.errors.push('[E518] "@alcalzone/release-script" (>=3.0.0) is used, but ".releaseconfig.json" not found. Please create.');
376
+ } else {
377
+ console.log('context[/.releaseconfig.json: '+ context['/.releaseconfig.json']);
378
+ const releaseConfigJson = JSON.parse(context['/.releaseconfig.json']);
379
+ console.log(`releaseConfigJson: ${releaseConfigJson}`);
380
+ const plugins = releaseConfigJson.plugins;
381
+ console.log(`plugins: ${plugins}`);
382
+ if (!context.packageJson.devDependencies['@alcalzone/release-script-plugin-iobroker']) {
383
+ context.errors.push('[E524] "@alcalzone/release-script" requires plugin "@alcalzone/release-script-plugin-iobroker". Please add.');
384
+ } else {
385
+ if (!plugins.includes('iobroker')) {
386
+ context.errors.push('[E527] Plugin "iobroker" missing at .releaseconfig.json. Please add.');
387
+ }
388
+ }
389
+ if (!context.packageJson.devDependencies['@alcalzone/release-script-plugin-license']) {
390
+ context.errors.push('[E525] "@alcalzone/release-script" requires plugin "@alcalzone/release-script-plugin-license". Please add.');
391
+ } else {
392
+ if (!plugins.includes('license')) {
393
+ context.errors.push('[E528] Plugin "license" missing at .releaseconfig.json. Please add.');
394
+ }
395
+ }
396
+ if (!context.packageJson.devDependencies['@alcalzone/release-script-plugin-manual-review']) {
397
+ context.warnings.push('[S526] Consider adding plugin "@alcalzone/release-script-plugin-manual-review".');
398
+ } else {
399
+ if (!plugins.includes('manual-review')) {
400
+ context.warnings.push('[W529] Plugin "manual-review" missing at .releaseconfig.json. Please add.');
401
+ }
402
+ }
403
+ }
404
+ }
405
+ }
406
+
407
+ if (context.packageJson.main && context.packageJson.main.endsWith('.js')) {
408
+ if (!context[`/${context.packageJson.main}`]) {
409
+ if (!context.ioPackageJson.common.nogit) {
410
+ context.errors.push(`[E519] "${context.packageJson.main}" found in package.json, but not found as file`);
411
+ }
412
+ } else {
413
+ if (context[`/${context.packageJson.main}`].includes('setInterval(') && !context[`/${context.packageJson.main}`].includes('clearInterval(')) {
414
+ if (context.ioPackageJson.common.compact) {
415
+ // if compact mode supported, it is critical
416
+ context.errors.push(`[E504] setInterval found in "${context.packageJson.main}", but no clearInterval detected`);
417
+ } else {
418
+ context.warnings.push(`[W504] setInterval found in "${context.packageJson.main}", but no clearInterval detected`);
419
+ }
420
+ }
421
+ if (context['/' + context.packageJson.main].includes('setTimeout(') && !context['/' + context.packageJson.main].includes('clearTimeout(')) {
422
+ if (context.ioPackageJson.common.compact) {
423
+ // if compact mode supported, it is critical
424
+ // context.errors.push(`[E505] setTimeout found in "${context.packageJson.main}", but no clearTimeout detected`);
425
+ context.warnings.push(`[W505] setTimeout found in "${context.packageJson.main}", but no clearTimeout detected`);
426
+ } else {
427
+ context.warnings.push(`[W505] setTimeout found in "${context.packageJson.main}", but no clearTimeout detected`);
428
+ }
429
+ }
430
+ }
431
+ }
432
+
433
+ // single free 522
434
+ // first free 0530
435
+
436
+ return context;
437
+ }
438
+
439
+ exports.checkCode = checkCode;
440
+
441
+ // List of error and warnings used at this module
442
+ // ----------------------------------------------
443
+
444
+ // [500] node_modules found in repo. Please delete it
445
+ // [501] Cannot get ${context.branch}.zip on github
446
+ // [502] "admin/img/info-big.png" not found, but selectID.js used in index_m.html
447
+ // [503] File "${file}" found in repo! Please remove file.
448
+ // [504] setInterval found in "${context.packageJson.main}", but no clearInterval detected
449
+ // [504] setInterval found in "${context.packageJson.main}", but no clearInterval detected
450
+ // [505] setTimeout found in "${context.packageJson.main}", but no clearTimeout detected
451
+ // [505] setTimeout found in "${context.packageJson.main}", but no clearTimeout detected
452
+ // [505] setTimeout found in "${context.packageJson.main}", but no clearTimeout detected
453
+ // [506] More non translated in german or russian words found in admin/words.js. You can use https://translator.iobroker.in/ for translations
454
+ // [506] Word "${word}" is not translated to german in admin/words.js. You can use https://translator.iobroker.in/ for translations
455
+ // [506] Word "${word}" is not translated to russian in admin/words.js. You can use https://translator.iobroker.in/ for translations
456
+ // [507] Cannot parse "admin/jsonConfig.json${context['/admin/jsonConfig.json'] ? '' : '5'}": ${e}
457
+ // [508] "admin/jsonConfig.json${context['/admin/jsonConfig.json'] ? '' : '5'}" not found, but admin support is declared
458
+ // [509] Cannot parse "admin/i18n/${lang}.json": ${e}
459
+ // [509] Cannot parse "admin/i18n/${lang}/translations.json": ${e}
460
+ // [510] "/admin/i18n/${lang}/translations.json" or "admin/i18n/${lang}.json" not found, but admin support is declared. Please add.
461
+ // [511] Cannot parse "admin/jsonCustom.json${context['/admin/jsonCustom.json'] ? '' : '5'}": ${e}
462
+ // [512] "admin/jsonCustom.json${context['/admin/jsonCustom.json'] ? '' : '5'}" not found, but custom support is declared
463
+ // [513] "gulpfile.js" found in repo! Think about migrating to @iobroker/adapter-dev package
464
+ // [514] "admin/blockly.js" not found, but blockly support is declared
465
+ // [515] Why did you decide to disable i18n support?
466
+ // [516] "${context.ioPackageJson.common.javascriptRules.url}" not found, but JavaScript-Rules support is declared
467
+ // [517] JavaScript-Rules support is declared, but no location in property common.javascriptRules.url defined
468
+ // [518] "@alcalzone/release-script" (>=3.0.0) is used, but ".releaseconfig.json" not found. Please create.
469
+ // [519] "${context.packageJson.main}" found in package.json, but not found as file
470
+ // [520] "gulpfile.js" found in repo but "gulp" not found at devDependencies at package.json. Check whether it can be removed.
471
+ // [521] "gulp" found at devDependencies at package.json but no "gulpfile.js" found. Is this dependency really required?
472
+ // [522]
473
+ // [523] "package-lock.json" not found in repo! Please remove from .gitignore!
474
+ // [524] "@alcalzone/release-script" requires plugin "@alcalzone/release-script-plugin-iobroker". Please add.
475
+ // [525] "@alcalzone/release-script" requires plugin "@alcalzone/release-script-plugin-license". Please add.
476
+ // [526] Consider adding plugin "@alcalzone/release-script-plugin-manual-review".
477
+ // [527] Plugin "iobroker" missing at .releaseconfig.json. Please add.
478
+ // [528] Plugin "license" missing at .releaseconfig.json. Please add.
479
+ // [529] Plugin "manual-review" missing at .releaseconfig.json. Please add.
480
+
@@ -0,0 +1,116 @@
1
+ 'use strict';
2
+ /*
3
+ This module is a support module for iobroker.repochecker
4
+
5
+ Area checked: README file
6
+ Numbering : 600 - 699
7
+
8
+ */
9
+
10
+ const execSync = require('node:child_process').execSync;
11
+
12
+ const common = require('./common.js');
13
+
14
+ function getAuthor(author) {
15
+ if (author && typeof author === 'object') {
16
+ return `${author.name} <${author.email}>`;
17
+ } else {
18
+ return author;
19
+ }
20
+ }
21
+
22
+ async function checkReadme(context) {
23
+ // https://raw.githubusercontent.com/userName/ioBroker.adaptername/${context.branch}/README.md
24
+ console.log('\ncheckReadme [E600 - E699]');
25
+
26
+ const data = await common.downloadFile(context.githubUrl, '/README.md');
27
+ if (!data) {
28
+ context.errors.push('[E601] No README.md found');
29
+ } else {
30
+ context.checks.push('README.md found');
31
+
32
+ if (!data.includes('## Changelog')) {
33
+ context.errors.push('[E603] NO "## Changelog" found in README.md');
34
+ } else {
35
+ context.checks.push('README.md contains Changelog');
36
+
37
+ if (!data.includes(`### ${context.packageJson.version}`)) {
38
+ context.errors.push(`[E606] Current adapter version ${context.packageJson.version} not found in README.md`);
39
+ } else {
40
+ context.checks.push('README.md contains current adapter version');
41
+ }
42
+ }
43
+
44
+ let npmYear = 0;
45
+ try {
46
+ const result = execSync(`npm view iobroker.${context.adapterName} --json`, { encoding: 'utf-8' });
47
+ const npmJson = JSON.parse(result);
48
+ //console.log(`[DEBUG] ${JSON.stringify(npmJson)}`);
49
+ if (npmJson['dist-tags'] && npmJson['dist-tags'].latest) {
50
+ const latest = npmJson['dist-tags'].latest;
51
+ const timeStr = npmJson.time[latest];
52
+ npmYear = new Date(timeStr).getFullYear();
53
+ console.log(`[DEBUG] ${latest} - ${timeStr} - ${npmYear}`);
54
+ }
55
+ } catch (e) {
56
+ context.warnings.push('[W606] Could not retrieve timestamp of LATEST revision at npm.');
57
+ console.log(`Error executing "npm view"`);
58
+ }
59
+
60
+ const pos = data.indexOf('## License');
61
+ if (pos === -1) {
62
+ context.errors.push('[E604] No "## License" found in README.md');
63
+ } else {
64
+ context.checks.push('## License found in README.md');
65
+ const text = data.substring(pos);
66
+ const year = new Date().getFullYear();
67
+ const commitYear = context.lastCommitYear || 0;
68
+ let readmeYear = 0;
69
+ let m = text.match(/\d\d\d\d\s*-\s*(\d\d\d\d)/);
70
+ if (m) {
71
+ readmeYear = Number(m[1]);
72
+ }
73
+ m = text.match(/(\d\d\d\d)/); /* both variants could be present */
74
+ if (m) {
75
+ if (Number(m[1]) > readmeYear) {
76
+ readmeYear = Number(m[1]);
77
+ }
78
+ }
79
+
80
+ console.log(`README year ${readmeYear}`);
81
+ console.log(`Current year ${year}`);
82
+ console.log(`Commit year ${commitYear}`);
83
+ console.log(`NPM year ${npmYear}`);
84
+
85
+ const valid = (readmeYear === year || readmeYear >= commitYear || readmeYear >=npmYear);
86
+ if (!valid) {
87
+ const m = text.match(/(\d\d\commitd\d)-\d\d\d\d/);
88
+ if (m) {
89
+ context.errors.push(`[E605] No actual year found in copyright. Please add "Copyright (c) ${m[1]}-${year} ${getAuthor(context.packageJson.author)}" at the end of README.md`);
90
+ } else {
91
+ context.errors.push(`[E605] No actual year found in copyright. Please add "Copyright (c) ${year} ${getAuthor(context.packageJson.author)}" at the end of README.md`);
92
+ }
93
+ } else {
94
+ context.checks.push('Valid copyright year found in README.md');
95
+ }
96
+ }
97
+
98
+ // languages = languagedetect.detect(data, 3);
99
+ //console.log(JSON.stringify(languages));
100
+ }
101
+
102
+ return context;
103
+ }
104
+
105
+ exports.checkReadme = checkReadme;
106
+
107
+ // List of error and warnings used at this module
108
+ // ----------------------------------------------
109
+
110
+ // [601] No README.md found
111
+ // [603] NO "## Changelog" found in README.md
112
+ // [606] Current adapter version ${context.packageJson.version} not found in README.md
113
+ // [604] No "## License" found in README.md
114
+ // [605] No actual year found in copyright. Please add "Copyright (c) ${m[1]}-${year} ${getAuthor(context.packageJson.author)}" at the end of README.md
115
+ // [605] No actual year found in copyright. Please add "Copyright (c) ${year} ${getAuthor(context.packageJson.author)}" at the end of README.md
116
+ // [606] Could not retrieve timestamp of LATEST revision at npm.