@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,90 @@
1
+ 'use strict';
2
+ /*
3
+ This module is a support module for iobroker.repochecker
4
+
5
+ Area checked: LICENSE file
6
+ Numbering : 700 - 799
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 checkLicenseFile(context) {
23
+ // https://raw.githubusercontent.com/userName/ioBroker.adaptername/${context.branch}/LICENSE
24
+ console.log('\ncheckLicenseFile [E7xx]');
25
+
26
+ const data = await common.downloadFile(context.githubUrl, '/LICENSE');
27
+ if (!data) {
28
+ context.errors.push('[E700] NO LICENSE file found, please add it.');
29
+ } else {
30
+ context.checks.push('LICENSE file found');
31
+
32
+ let npmYear = 0;
33
+ try {
34
+ const result = execSync(`npm view iobroker.${context.adapterName} --json`, { encoding: 'utf-8' });
35
+ const npmJson = JSON.parse(result);
36
+ //console.log(`[DEBUG] ${JSON.stringify(npmJson)}`);
37
+ if (npmJson['dist-tags'] && npmJson['dist-tags'].latest) {
38
+ const latest = npmJson['dist-tags'].latest;
39
+ const timeStr = npmJson.time[latest];
40
+ npmYear = new Date(timeStr).getFullYear();
41
+ }
42
+ } catch (e) {
43
+ console.log(`Error executing "npm view" - ${e}`);
44
+ }
45
+
46
+ if (context.packageJson.license === 'MIT') {
47
+ const text = data;
48
+ const year = new Date().getFullYear();
49
+ const commitYear = context.lastCommitYear || 0;
50
+ let licenseYear = 0;
51
+ let m = text.match(/\d\d\d\d\s*-\s*(\d\d\d\d)/);
52
+ if (m) {
53
+ licenseYear = Number(m[1]);
54
+ }
55
+ m = text.match(/(\d\d\d\d)/);
56
+ if (m) {
57
+ if (Number(m[1])>licenseYear) {
58
+ licenseYear = Number(m[1]);
59
+ }
60
+ }
61
+
62
+ console.log(`License year ${licenseYear}`);
63
+ console.log(`Current year ${year}`);
64
+ console.log(`Commit year ${commitYear}`);
65
+ console.log(`NPM year ${npmYear}`);
66
+
67
+ const valid = (licenseYear === year || licenseYear >= commitYear || licenseYear >=npmYear);
68
+ if (!valid) {
69
+ const m = text.match(/(\d\d\d\d)-\d\d\d\d/);
70
+ if (m) {
71
+ context.errors.push(`[E701] No actual year found in LICENSE. Please add "Copyright (c) ${m[1]}-${year} ${getAuthor(context.packageJson.author)}" at the start of LICENSE`);
72
+ } else {
73
+ context.errors.push(`[E701] No actual year found in LICENSE. Please add "Copyright (c) ${year} ${getAuthor(context.packageJson.author)}" at the start of LICENSE`);
74
+ }
75
+ } else {
76
+ context.checks.push('Valid copyright year found in LICENSE');
77
+ }
78
+ }
79
+ }
80
+ return context;
81
+ }
82
+
83
+ exports.checkLicenseFile = checkLicenseFile;
84
+
85
+ // List of error and warnings used at this module
86
+ // ----------------------------------------------
87
+
88
+ // [E700] NO LICENSE file found, please add it.
89
+ // [E701] No actual year found in LICENSE. Please add "Copyright (c) ${m[1]}-${year} ${getAuthor(context.packageJson.author)}" at the start of LICENSE
90
+ // [E701] No actual year found in LICENSE. Please add "Copyright (c) ${year} ${getAuthor(context.packageJson.author)}" at the start of LICENSE
@@ -0,0 +1,63 @@
1
+ 'use strict';
2
+ /*
3
+ This module is a support module for iobroker.repochecker
4
+
5
+ Area checked: GitHub repository
6
+ Numbering : 800 - 899
7
+
8
+ */
9
+
10
+ const common = require('./common.js');
11
+
12
+ async function getCommitInfos(context) {
13
+ console.log('\ngetCommitInfos');
14
+ context.lastCommitYear = 0;
15
+ try{
16
+ const data = await common.downloadFile(context.githubUrlOriginal, `/commits/${context.branch}`)
17
+ const m = data.match(/Commits on [\w\s\d]+, (\d\d\d\d)/);
18
+ if (m) {
19
+ context.lastCommitYear = Number(m[1]);
20
+ common.debug (`lastCommitYear: ${context.lastCommitYear}`);
21
+ } else {
22
+ common.debug (`lastCommitYear: not found within data returned`);
23
+ }
24
+ } catch (e) {
25
+ console.log( '[INTERNAL] error retrieving commit info - ${e}');
26
+ }
27
+
28
+ return context;
29
+ }
30
+
31
+ function checkGithubRepo(context) {
32
+ console.log('\ncheckGithubRepo [E8xx]');
33
+
34
+ if (!context.githubApiData.description) {
35
+ context.errors.push(`[E801] No repository about text found. Please go to "${context.githubUrlOriginal}", press the settings button beside the about title and add the description.`);
36
+ } else {
37
+ context.checks.push('Github repository about found.');
38
+ }
39
+
40
+ if (!context.githubApiData.topics || context.githubApiData.topics.length === 0) {
41
+ context.errors.push(`[E802] No topics found in the repository. Please go to "${context.githubUrlOriginal}", press the settings button beside the about title and add some topics.`);
42
+ } else {
43
+ context.checks.push('Github repository about found.');
44
+ }
45
+
46
+ if (context.githubApiData.archived) {
47
+ context.errors.push(`[E803] Archived repositories are not allowed.`);
48
+ }
49
+
50
+ // max E803 - limited to 849
51
+
52
+ return context;
53
+ }
54
+
55
+ exports.getCommitInfos = getCommitInfos;
56
+ exports.checkGithubRepo = checkGithubRepo;
57
+
58
+ // List of error and warnings used at this module
59
+ // ----------------------------------------------
60
+
61
+ // [E801] No repository about text found. Please go to "${context.githubUrlOriginal}", press the settings button beside the about title and add the description.
62
+ // [E802] No topics found in the repository. Please go to "${context.githubUrlOriginal}", press the settings button beside the about title and add some topics.
63
+ // [E803] Archived repositories are not allowed.
@@ -0,0 +1,188 @@
1
+ 'use strict';
2
+ /*
3
+ This module is a support module for iobroker.repochecker
4
+
5
+ Area checked: .gitignore, .npmignore
6
+ Numbering : 900 - 998
7
+
8
+ NOTE: requires filesList to be loaded
9
+ */
10
+
11
+ /*
12
+ TODO:
13
+ - something is wrong with undefined 'check' variable
14
+ */
15
+
16
+ // const common = require('./common.js');
17
+
18
+ function paddingNum(num) {
19
+ if (num >= 10) return num;
20
+ return '0' + num;
21
+ }
22
+
23
+ // 900 - ???
24
+ async function checkGitIgnore(context) {
25
+
26
+ const checkFiles = [
27
+ '.idea',
28
+ 'tmp',
29
+ 'node_modules',
30
+ 'iob_npm.done',
31
+ ];
32
+
33
+ // https://raw.githubusercontent.com/userName/ioBroker.adaptername/${context.branch}/.gitignore
34
+ console.log('\ncheckGitIgnore [E9xx]');
35
+
36
+ if (!context.filesList) {
37
+ throw('FATAL:context.fileslist is undefined');
38
+ }
39
+
40
+ if (!context.filesList.includes('.gitignore')) {
41
+ context.errors.push(`[E901] .gitignore not found`);
42
+ } else {
43
+ const rules = (context['/.gitignore'] || '').split('\n').map(line => line.trim().replace('\r', '')).filter(line => line);
44
+ rules.forEach((name, i) => {
45
+ if (name.includes('*')) {
46
+ rules[i] = new RegExp(name
47
+ .replace('.', '\\.')
48
+ .replace('/', '\\/')
49
+ .replace('**', '.*')
50
+ .replace('*', '[^\\/]*')
51
+ );
52
+ }
53
+ });
54
+
55
+ /* TODO: something is wrong with undefined 'check' variable
56
+ if (!rules.includes('node_modules') && !rules.includes('/node_modules') && !rules.includes('/node_modules/*') && !rules.includes('node_modules/*')) {
57
+ !check && context.errors.push(`[E902] node_modules not found in .npmignore`);
58
+ }
59
+ if (!rules.includes('iob_npm.done') && !rules.includes('/iob_npm.done')) {
60
+ !check && context.errors.push(`[E903] iob_npm.done not found in .gitignore`);
61
+ }
62
+ */
63
+
64
+ checkFiles.forEach(file => {
65
+ if (context.filesList.includes(file)) {
66
+ // maybe it is with regex
67
+ const check = rules.some(rule => {
68
+ if (typeof rule === 'string') {
69
+ return rule === file || rule === file.replace(/\/$/, '');
70
+ } else {
71
+ return rule.test(file);
72
+ }
73
+ });
74
+
75
+ !check && context.errors.push(`[E904] file ${file} found in repository, but not found in .gitignore`);
76
+ }
77
+ });
78
+ }
79
+
80
+ return context;
81
+ }
82
+
83
+ // 950 - ???
84
+ async function checkNpmIgnore(context) {
85
+ const checkFiles = [
86
+ 'node_modules/',
87
+ 'test/',
88
+ 'src/',
89
+ 'appveyor.yml',
90
+ '.travis.yml',
91
+ 'tsconfig.json',
92
+ 'tsconfig.build.json',
93
+ 'iob_npm.done',
94
+ // '.git/',
95
+ // '.github/',
96
+ // '.idea/',
97
+ // '.gitignore',
98
+ // '.npmignore',
99
+ // '.travis.yml',
100
+ // '.babelrc',
101
+ // '.editorconfig',
102
+ // '.eslintignore',
103
+ // '.eslintrc.js',
104
+ // '.fimbullinter.yaml',
105
+ // '.lgtm.yml',
106
+ // '.prettierignore',
107
+ // '.prettierignore',
108
+ // '.prettierrc.js',
109
+ // '.vscode/',
110
+ ];
111
+
112
+ // https://raw.githubusercontent.com/userName/ioBroker.adaptername/${context.branch}/.npmignore
113
+ console.log('\ncheckNpmIgnore [E95x]');
114
+ if (context.packageJson.files && context.packageJson.files.length) {
115
+ if (context.filesList.includes('.npmignore')) {
116
+ context.warnings.push(`[E951] .npmignore found but "files" is used at package.json. Please remove .npmignore.`);
117
+ } else {
118
+ context.checks.push('package.json "files" already used.');
119
+ }
120
+ return Promise.resolve(context);
121
+ }
122
+
123
+ // package.json files section is NOT used
124
+ if (!context.filesList.includes('.npmignore')) {
125
+ context.warnings.push(`[E952] .npmignore not found`);
126
+ } else {
127
+ context.warnings.push(`[W953] .npmignore found - consider using package.json object "files" instead.`);
128
+
129
+ const rules = (context['/.npmignore'] || '').split('\n').map(line => line.trim().replace('\r', '')).filter(line => line);
130
+ let tooComplexToCheck = false;
131
+ rules.forEach((name, i) => {
132
+ if (name.includes('*')) {
133
+ rules[i] = new RegExp(name
134
+ .replace('.', '\\.')
135
+ .replace('/', '\\/')
136
+ .replace('**', '.*')
137
+ .replace('*', '[^\\/]*')
138
+ );
139
+ }
140
+ if (name.startsWith('!')) {
141
+ tooComplexToCheck = true;
142
+ }
143
+ });
144
+
145
+ // There's no need to put node_modules in `.npmignore`. npm will never publish node_modules in the package, except if one of the modules is explicitly mentioned in bundledDependencies.
146
+ /*if (!rules.includes('node_modules') && !rules.includes('/node_modules') && !rules.includes('/node_modules/*') && !rules.includes('node_modules/*')) {
147
+ !check && context.errors.push(`[E954] node_modules not found in `.npmignore``);
148
+ }*/
149
+ if (!tooComplexToCheck) {
150
+ if (!rules.includes('iob_npm.done') && !rules.includes('/iob_npm.done')) {
151
+ /*!check &&*/ context.errors.push(`[E955] iob_npm.done not found in .npmignore`);
152
+ }
153
+
154
+ checkFiles.forEach((file, i) => {
155
+ if (context.filesList.includes(file)) {
156
+ // maybe it is with regex
157
+ const check = rules.some(rule => {
158
+ if (typeof rule === 'string') {
159
+ return rule === file || rule === file.replace(/\/$/, '');
160
+ } else {
161
+ return rule.test(file);
162
+ }
163
+ });
164
+
165
+ !check && context.errors.push(`[E9${paddingNum(i + 61)}] file ${file} found in repository, but not found in .npmignore`);
166
+ }
167
+ });
168
+ }
169
+ }
170
+ return context;
171
+ }
172
+
173
+ exports.checkGitIgnore = checkGitIgnore;
174
+ exports.checkNpmIgnore = checkNpmIgnore;
175
+
176
+ // List of error and warnings used at this module
177
+ // ----------------------------------------------
178
+
179
+ // [901] .gitignore not found
180
+ // [902] node_modules not found in .npmignore
181
+ // [903] iob_npm.done not found in .gitignore
182
+ // [904] file ${file} found in repository, but not found in .gitignore
183
+
184
+ // [951] .npmignore found but "files" is used at package.json. Please remove .npmignore.
185
+ // [952] .npmignore not found
186
+ // [953] .npmignore found - consider using package.json object "files" instead.
187
+ // [954] node_modules not found in `.npmignore`
188
+ // [955] iob_npm.done not found in .npmignore`
package/lib/common.js ADDED
@@ -0,0 +1,109 @@
1
+ 'use strict';
2
+ /*
3
+ This module contains genaral utility routines for iobroker.repochecker
4
+
5
+ */
6
+ const axios = require('axios');
7
+
8
+ // disable axios caching
9
+ axios.defaults.headers = {
10
+ 'Cache-Control': 'no-cache',
11
+ 'Pragma': 'no-cache',
12
+ 'Expires': '0',
13
+ };
14
+
15
+ let gDebug = false;
16
+ function setDebug(flag){
17
+ gDebug = flag;
18
+ }
19
+
20
+ function debug(msg) {
21
+ console.log(`[DEBUG] ${msg}`);
22
+ }
23
+
24
+ function checkLanguages(langObj, languages) {
25
+ return languages.filter(lang => !langObj[lang]);
26
+ }
27
+
28
+ async function downloadFile(githubUrl, path, binary, noError) {
29
+
30
+ console.log(`Download ${githubUrl}${path || ''}`);
31
+
32
+ const options = {};
33
+ if (binary) {
34
+ options.responseType = 'arraybuffer';
35
+ }
36
+
37
+ try {
38
+ const response = await axios(githubUrl + (path || ''), options);
39
+ debug(`download succeded`);
40
+ return(response.data);
41
+ } catch (e) {
42
+ (!noError || gDebug) && console.error(`Cannot download ${githubUrl}${path || ''} ${e}`);
43
+ throw e;
44
+ }
45
+
46
+ }
47
+
48
+ function getDependencyArray(deps) {
49
+ return deps
50
+ .map(dep => typeof dep === 'object' ? Object.keys(dep) : [dep])
51
+ .reduce((acc, dep) => acc.concat(dep), []);
52
+ }
53
+
54
+ // dependencies might be:
55
+ // [
56
+ // {"js-controller":">=1.2.3"}
57
+ // ]
58
+ // or
59
+ // [
60
+ // {"js-controller":">=1.2.3"},
61
+ // {"vis":">=1.2.3"}
62
+ // ]
63
+ // or
64
+ // [
65
+ // {
66
+ // "js-controller":">=1.2.3",
67
+ // "vis":">=1.2.3"
68
+ // }
69
+ // ]
70
+ // or
71
+ // [
72
+ // {"js-controller":">=1.2.3"},
73
+ // "vis"
74
+ // ]
75
+ function getDependencies(deps) {
76
+ const ret = {};
77
+ console.log(`deps: ${JSON.stringify(deps)}, type ${typeof deps}`);
78
+ for (const dep of deps ) {
79
+ if (typeof dep === 'object') {
80
+ for (const key in dep ) {
81
+ ret[key] = dep[key];
82
+ }
83
+ } else {
84
+ ret[ dep ] = '>=0';
85
+ }
86
+ }
87
+ console.log(`ret: ${JSON.stringify(ret)}`);
88
+ return ret;
89
+ }
90
+
91
+ /*
92
+ return the greater of two semver versions
93
+ */
94
+ function maxVersion(v1, v2) {
95
+ if (compareVersions.compareVersions(v1, v2) > 0) {
96
+ return v1;
97
+ }
98
+ return v2;
99
+ }
100
+
101
+ exports.setDebug=setDebug;
102
+ exports.debug=debug;
103
+
104
+ exports.checkLanguages=checkLanguages;
105
+ exports.downloadFile=downloadFile;
106
+ exports.getDependencyArray=getDependencyArray;
107
+ exports.getDependencies=getDependencies;
108
+ exports.maxVersion=maxVersion;
109
+