Skip to content

Commit

Permalink
streamlining the log and debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
oweitman committed Nov 12, 2024
1 parent d328128 commit 0c8c064
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 173 deletions.
44 changes: 22 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,22 +71,22 @@ axios.defaults.headers = {

function getGithubApiData(context) {
return new Promise((resolve, reject) => {
console.log('\ngetGithubApiData');
common.debug('\ngetGithubApiData');
axios
.get(context.githubUrlApi, { cache: false })
.then((response) => {
context.githubApiData = response.data;
// console.log(`API Data: ${JSON.stringify(context.githubApiData)}`);
// common.log(`API Data: ${JSON.stringify(context.githubApiData)}`);

if (!context.branch) {
context.branch = context.githubApiData.default_branch; // main vs. master
console.log(`Branch was not defined by user - checking branch: ${context.branch}`);
common.log(`Branch was not defined by user - checking branch: ${context.branch}`);
}

context.githubUrl = `${context.githubUrlOriginal.replace('https://github.com', 'https://raw.githubusercontent.com')}/${context.branch}`;
console.log(`Original URL: ${context.githubUrlOriginal}`);
console.log(`raw: ${context.githubUrl}`);
console.log(`api: ${context.githubUrlApi}`);
common.debug(`Original URL: ${context.githubUrlOriginal}`);
common.debug(`raw: ${context.githubUrl}`);
common.debug(`api: ${context.githubUrlApi}`);

resolve(context);
})
Expand All @@ -109,7 +109,7 @@ function makeResponse(code, data) {
}

function check(request, ctx, callback) {
// console.log('PROCESS: ' + JSON.stringify(request));
// common.log('PROCESS: ' + JSON.stringify(request));
if (!request.queryStringParameters.url) {
return callback(null, makeResponse(500, { error: 'No github URL provided' }));
} else {
Expand Down Expand Up @@ -163,7 +163,7 @@ function check(request, ctx, callback) {
);
})
.catch((err) => {
console.error(`GLOBAL ERROR: ${err.toString()}, ${JSON.stringify(err)}`);
common.error(`GLOBAL ERROR: ${err.toString()}, ${JSON.stringify(err)}`);
context.errors.push(`[E999] GLOBAL ERROR: ${err.toString()}, ${JSON.stringify(err)}`);

return callback(
Expand Down Expand Up @@ -225,7 +225,7 @@ if (typeof module !== 'undefined' && module.parent) {
repoUrl = `https://github.com/${repoUrl}`;
}
} else {
console.log('ERROR: No repository specified');
common.log('ERROR: No repository specified');
process.exit(1);
}

Expand All @@ -234,7 +234,7 @@ if (typeof module !== 'undefined' && module.parent) {
repoBranch = process.argv[3];
}

console.log(`Checking repository ${repoUrl} (branch ${repoBranch})`);
common.log(`Checking repository ${repoUrl} (branch ${repoBranch})`);
check(
{
queryStringParameters: {
Expand All @@ -245,34 +245,34 @@ if (typeof module !== 'undefined' && module.parent) {
null,
(err, data) => {
const context = JSON.parse(data.body);
console.log(context.result);
common.debug(context.result);

console.log('\n\n########## SUMMARY ##########');
common.log('\n########## SUMMARY ##########');
if (context.errors.length) {
console.log('\n\nErrors:');
common.log('\n\nErrors:');
context.errors.sort().forEach((err) => {
const issue = err.substring(1, 5);
console.error(err);
common.error(err);
if (issues[issue]) {
//if (issues[issue].title) {
// console.error(getText(issues[issue].title, 'en'));
// common.error(getText(issues[issue].title, 'en'));
//}
if (issues[issue].explanation) {
console.error(getText(issues[issue].explanation, 'en'));
common.error(getText(issues[issue].explanation, 'en'));
}
if (issues[issue].resolving) {
console.error(getText(issues[issue].resolving, 'en'));
common.error(getText(issues[issue].resolving, 'en'));
}
if (issues[issue].notes) {
console.error(getText(issues[issue].notes, 'en'));
common.error(getText(issues[issue].notes, 'en'));
}
}
});
} else {
console.log('\n\nNO errors encountered.');
common.log('NO errors encountered.');
}
if (context.warnings.length) {
console.log('\nWarnings:');
common.log('Warnings:');
context.warnings.sort().forEach((err) => {
const issue = err.substring(1, 5);
console.warn(err);
Expand All @@ -292,9 +292,9 @@ if (typeof module !== 'undefined' && module.parent) {
}
});
} else {
console.log('\n\nNO warnings encountered.');
common.log('\n\nNO warnings encountered.');
}
console.log(`\ncreated by repochecker ${context.version} based on commit ${context.lastCommitSha}`);
common.log(`\ncreated by repochecker ${context.version} based on commit ${context.lastCommitSha}`);
},
);
}
12 changes: 6 additions & 6 deletions lib/M000_PackageJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const blacklistPackageJson = {
};

async function getPackageJson(context) {
console.log('\ngetPackageJson');
common.debug('\ngetPackageJson');

const packageJson = await common.downloadFile(context.githubUrl, '/package.json');
context.packageJson = packageJson;
Expand All @@ -118,7 +118,7 @@ async function getPackageJson(context) {
}

async function checkPackageJson(context) {
console.log('\ncheckPackageJson [E001 - E099]');
common.log('[E001 - E099] checkPackageJson');

if (!context.githubUrlOriginal.match(/\/iobroker\./i)) {
context.errors.push('[E002] No "ioBroker." found in the name of repository');
Expand Down Expand Up @@ -429,15 +429,15 @@ async function checkPackageJson(context) {
}

for (const blacklist in blacklistPackageJson) {
// console.log(`checking blacklist ${blacklist}`);
// common.log(`checking blacklist ${blacklist}`);
let tmp = context.packageJson;
let log = '';
for (const element of blacklist.split('.')) {
log = `${log}.${element}`;
//console.log(` check ${log}`);
//common.log(` check ${log}`);
tmp = tmp[element];
if (!tmp) {
//console.log(` ${log} does not exist`);
//common.log(` ${log} does not exist`);
break;
}
}
Expand All @@ -449,7 +449,7 @@ async function checkPackageJson(context) {
}
}
// else {
// console.log(`blacklist ${blacklist} no match`);
// common.log(`blacklist ${blacklist} no match`);
// }
}
context.checks.push('"blacklist (package)" checked.');
Expand Down
18 changes: 9 additions & 9 deletions lib/M100_IOPackageJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const sizeOf = require('image-size');
const common = require('./common.js');

async function getIOPackageJson(context) {
console.log('\ngetIOPackageJson');
common.debug('\ngetIOPackageJson');

const ioPackageJson = await common.downloadFile(context.githubUrl, '/io-package.json');
context.ioPackageJson = ioPackageJson;
Expand Down Expand Up @@ -71,7 +71,7 @@ const blacklistIOPackageJson = {


async function checkIOPackageJson(context) {
console.log('\ncheckIOPackage [E100 - E249]');
common.log('[E100 - E249] checkIOPackage');

if (!context.ioPackageJson.native) {
context.errors.push('[E101] io-package.json must have at least empty "native" attribute');
Expand All @@ -97,10 +97,10 @@ async function checkIOPackageJson(context) {
//const regex = new RegExp(suspiciousPhrases.join('|'), 'i');

let suspiciousKeys = Object.keys(context.ioPackageJson.native) || [];
// console.log(`native keys: ${suspiciousKeys.join()}`);
// common.log(`native keys: ${suspiciousKeys.join()}`);
//suspiciousKeys = suspiciousKeys.filter( key => regex.test(key));
suspiciousKeys = suspiciousKeys.filter(key => suspiciousPhrases.includes(key.toLowerCase()));
// console.log(`suspicious keys: ${suspiciousKeys.join()}`);
// common.log(`suspicious keys: ${suspiciousKeys.join()}`);

if (suspiciousKeys.length) {
if (context.ioPackageJson.protectedNative) {
Expand Down Expand Up @@ -510,7 +510,7 @@ async function checkIOPackageJson(context) {

const jsControllerDependency = context.ioPackageJson.common.dependencies.find(dep => Object.keys(dep).find(attr => attr === 'js-controller'));
if (jsControllerDependency) {
console.log(`Found current js-controller dependency "${jsControllerDependency['js-controller']}"`);
common.debug(`Found current js-controller dependency "${jsControllerDependency['js-controller']}"`);

if (!jsControllerDependency['js-controller'].startsWith('>=')) {
context.errors.push(`[E159] common.dependencies "js-controller" dependency should always allow future versions (>=x.x.x) - recommended: {"js-controller": ">=${recommendedJsControllerVersion}"}`);
Expand Down Expand Up @@ -589,15 +589,15 @@ async function checkIOPackageJson(context) {
}

for (const blacklist in blacklistIOPackageJson) {
//console.log(`checking blacklist ${blacklist}`);
//common.log(`checking blacklist ${blacklist}`);
let tmp = context.ioPackageJson;
let log = '';
for (const element of blacklist.split('.')) {
log = `${log}.${element}`;
//console.log(` check ${log}`);
//common.log(` check ${log}`);
tmp = tmp[element];
if (!tmp) {
//console.log(` ${log} does not exist`);
//common.log(` ${log} does not exist`);
break;
}
}
Expand All @@ -609,7 +609,7 @@ async function checkIOPackageJson(context) {
}
}
//else {
// console.log(`blacklist ${blacklist} no match`);
// common.log(`blacklist ${blacklist} no match`);
//}
}
context.checks.push('"blacklist (io-package)" checked.');
Expand Down
6 changes: 3 additions & 3 deletions lib/M250_Npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

const axios = require('axios');
const compareVersions = require('compare-versions');

const common = require('./common.js');

// disable axios caching
axios.defaults.headers = {
Expand All @@ -22,7 +22,7 @@ axios.defaults.headers = {
// const common = require('./common.js');

async function checkNpm(context) {
console.log('\ncheckNpm [E250 - 299]');
common.log('[E250 - E299] checkNpm');
let body;
try {
const response = await axios(`https://registry.npmjs.org/iobroker.${context.adapterName}`);
Expand Down Expand Up @@ -77,7 +77,7 @@ async function checkNpm(context) {
if (context.ioPackageJson && context.ioPackageJson.common && context.ioPackageJson.common.news) {
const missingVersions = [];
for (const vers in context.ioPackageJson.common.news) {
//console.log(`[DEBUG] news for ${vers}`);
//common.log(`[DEBUG] news for ${vers}`);
if (!body.versions[vers]) {
missingVersions.push(vers);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/M300_Testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
*/

// const common = require('./common.js');
const common = require('./common.js');

async function checkTests(context) {
console.log('\ncheckTests [E300 - E399]');
common.log('[E300 - E399] checkTests');
// if found some file in `\.github\workflows` with the test inside => it is OK too
if (context && context.filesList.find(name => name.startsWith('.github/workflows/') && name.endsWith('.yml') && name.toLowerCase().includes('test'))) {
context.checks.push('Tests found on github actions');
Expand Down
14 changes: 7 additions & 7 deletions lib/M400_Repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ axios.defaults.headers = {
const common = require('./common.js');

async function checkRepository(context) {
console.log('\ncheckRepository [E400 - E499]');
common.log('[E400 - E499] checkRepository');

if (context.ioPackageJson && context.ioPackageJson.common && context.ioPackageJson.common.type) { /* TODO: why check type here ? */
// download latest repo
Expand Down Expand Up @@ -138,7 +138,7 @@ async function checkRepository(context) {
body = _response.data;
if (!body) {
context.errors.push('[E429] Cannot download http://repo.iobroker.live/sources-dist-latest.json');
throw('Cannot download http://repo.iobroker.live/sources-dist-latest.json'); // ABORT to avoid generating unuseable report
throw ('Cannot download http://repo.iobroker.live/sources-dist-latest.json'); // ABORT to avoid generating unuseable report
} else {
context.latestRepoLive = body;
}
Expand All @@ -147,7 +147,7 @@ async function checkRepository(context) {
body = _response.data;
if (!body) {
context.errors.push('[E430] Cannot download http://repo.iobroker.live/sources-dist.json');
throw('Cannot download http://repo.iobroker.live/sources-dist.json'); // ABORT to avoid generating unuseable report
throw ('Cannot download http://repo.iobroker.live/sources-dist.json'); // ABORT to avoid generating unuseable report
} else {
context.stableRepoLive = body;
}
Expand All @@ -162,7 +162,7 @@ async function checkRepository(context) {
} else {
const versDependency = dependencies[dependency];
const versRepository = context.latestRepoLive[dependency].version;
//console.log( `DEBUG: dependency ${dependency} - ${versDependency} - latest ${versRepository}`);
//common.log( `DEBUG: dependency ${dependency} - ${versDependency} - latest ${versRepository}`);
if (!versDependency.startsWith('>=')) {
context.warnings.push(`[W432] Dependency '${dependency}':'${dependencies[dependency]}' should specify '>='`);
} else {
Expand All @@ -183,7 +183,7 @@ async function checkRepository(context) {
} else {
const versDependency = dependencies[dependency];
const versRepository = context.stableRepoLive[dependency].version;
//console.log( `DEBUG: dependency ${dependency} - ${versDependency} - latest ${versRepository}`);
//common.log( `DEBUG: dependency ${dependency} - ${versDependency} - latest ${versRepository}`);
if (!versDependency.startsWith('>=')) {
context.warnings.push(`[W434] Dependency '${dependency}':'${dependencies[dependency]}' should specify '>='`);
} else {
Expand Down Expand Up @@ -213,7 +213,7 @@ async function checkRepository(context) {
} else {
const versDependency = dependencies[dependency];
const versRepository = context.latestRepoLive[dependency].version;
//console.log( `DEBUG: dependency ${dependency} - ${versDependency} - latest ${versRepository}`);
//common.log( `DEBUG: dependency ${dependency} - ${versDependency} - latest ${versRepository}`);
if (!versDependency.startsWith('>=')) {
context.warnings.push(`[W432] Dependency '${dependency}':'${dependencies[dependency]}' should specify '>='`);
} else {
Expand All @@ -234,7 +234,7 @@ async function checkRepository(context) {
} else {
const versDependency = dependencies[dependency];
const versRepository = context.stableRepoLive[dependency].version;
//console.log( `DEBUG: dependency ${dependency} - ${versDependency} - latest ${versRepository}`);
//common.log( `DEBUG: dependency ${dependency} - ${versDependency} - latest ${versRepository}`);
if (!versDependency.startsWith('>=')) {
context.warnings.push(`[W434] Dependency '${dependency}':'${dependencies[dependency]}' should specify '>='`);
} else {
Expand Down
Loading

0 comments on commit 0c8c064

Please sign in to comment.