Skip to content

Commit

Permalink
Begin implementing logging
Browse files Browse the repository at this point in the history
  • Loading branch information
MattIPv4 committed Jul 4, 2019
1 parent 6808040 commit d8f49c0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 39 deletions.
50 changes: 30 additions & 20 deletions cnames.js
@@ -1,3 +1,6 @@
// Load in custom logging
const {logDown, logUp, log} = require("./log.js");

// Load in custom caching
const {getCache, setCache} = require("./cache.js");

Expand Down Expand Up @@ -26,7 +29,7 @@ require("./types.js");
*/
const getCNAMEsFile = async () => {
// Log
console.log(chalk.cyanBright.bold("\nStarting getCNAMEsFile process"));
log("\nStarting getCNAMEsFile process", chalk.cyanBright.bold);

// Get the raw GitHub API data
const req = await octokit.repos.getContents({
Expand All @@ -39,7 +42,7 @@ const getCNAMEsFile = async () => {
const content = Buffer.from(req.data.content, req.data.encoding).toString();

// Done
console.log(chalk.greenBright.bold("Fetching completed for getCNAMEsFile"));
log("Fetching completed for getCNAMEsFile", chalk.greenBright.bold);
return content;
};

Expand All @@ -50,19 +53,21 @@ const getCNAMEsFile = async () => {
*/
const getCNAMEs = async (file) => {
// Log
console.log(chalk.cyanBright.bold("\nStarting getCNAMEs process"));
log("\nStarting getCNAMEs process", chalk.cyanBright.bold);

// Fetch any cache we have
const cache = await getCache("getCNAMEs");
if (cache) {
console.log(chalk.greenBright.bold("Cached data found for getCNAMEs"));
log("Cached data found for getCNAMEs", chalk.greenBright.bold);
return cache;
}

// Get the raw cnames file
if (!file) {
logDown();
file = await getCNAMEsFile();
console.log(chalk.cyanBright.bold("\nResuming getCNAMEs process"));
logUp();
log("\nResuming getCNAMEs process", chalk.cyanBright.bold);
}

// Regex time
Expand All @@ -80,7 +85,7 @@ const getCNAMEs = async (file) => {
await setCache("getCNAMEs", cnames);

// Done
console.log(chalk.greenBright.bold("Parsing completed for getCNAMEs"));
log("Parsing completed for getCNAMEs", chalk.greenBright.bold);
return cnames
};

Expand All @@ -92,12 +97,14 @@ const getCNAMEs = async (file) => {
*/
const generateCNAMEsFile = async (cnames, file) => {
// Log
console.log(chalk.cyanBright.bold("\nStarting generateCNAMEsFile process"));
log("\nStarting generateCNAMEsFile process", chalk.cyanBright.bold);

// Get the raw cnames file
if (!file) {
logDown();
file = await getCNAMEsFile();
console.log(chalk.cyanBright.bold("\nResuming generateCNAMEsFile process"));
logUp();
log("\nResuming generateCNAMEsFile process", chalk.cyanBright.bold);
}

// Regex time to find the top/bottom comment blocks
Expand All @@ -111,11 +118,11 @@ const generateCNAMEsFile = async (cnames, file) => {
// Abort if couldn't find the top/bottom blocks
if (commentBlocks.length < 2) {
// Log
console.log(chalk.yellow(" Could not locate top & bottom comment blocks in raw file"));
console.log(chalk.redBright.bold("Generation aborted for generateCNAMEsFile"));
log(" Could not locate top & bottom comment blocks in raw file", chalk.yellow);
log("Generation aborted for generateCNAMEsFile", chalk.redBright.bold);
return;
}
console.log(chalk.blue(" Comment blocks located in existing raw file"));
log(" Comment blocks located in existing raw file", chalk.blue);

// Get perfect alphabetical order
const cnamesKeys = Object.keys(cnames);
Expand All @@ -133,7 +140,7 @@ const generateCNAMEsFile = async (cnames, file) => {
const content = `\n${commentBlocks[0]}\n\nvar cnames_active = {\n${cnamesList.join("\n")}\n ${commentBlocks[1]}\n}\n`;

// Done
console.log(chalk.greenBright.bold("Genearation completed for generateCNAMEsFile"));
log("Generation completed for generateCNAMEsFile", chalk.greenBright.bold);
return content
};

Expand Down Expand Up @@ -162,11 +169,14 @@ const testUrl = async url => {
* @returns {Promise<cnamesObject>} - Any failed CNAME entries
*/
const validateCNAMEs = async () => {
// Log
log("\nStarting validateCNAMEs process", chalk.cyanBright.bold);

// Get the CNAMEs
logDown();
const cnames = await getCNAMEs();

// Log
console.log(chalk.cyanBright.bold("\nStarting validateCNAMEs process"));
logUp();
log("\nResuming validateCNAMEs process", chalk.cyanBright.bold);

// Fetch any cache we have
const cache = await getCache("validateCNAMEs");
Expand Down Expand Up @@ -201,21 +211,21 @@ const validateCNAMEs = async () => {

// If in cache, use that
if (cache && cname in cache) {
console.log(chalk.blue(` [${position}] ${urlHttp} in cache, skipping tests.`));
log(` [${position}] ${urlHttp} in cache, skipping tests.`, chalk.blue);
tests[cname] = cache[cname];
if (tests[cname].failed) failedCounter++;
continue;
}

// Run the tests
console.log(chalk.blue(` [${position}] Testing ${urlHttp}...`));
log(` [${position}] Testing ${urlHttp}...`, chalk.blue);
const failedHttp = await testUrl(urlHttp);
const failedHttps = await testUrl(urlHttps);

// Allow one to fail without care
if (failedHttp && failedHttps) {
// Log
console.log(chalk.yellow(` ...failed: HTTP: \`${failedHttp}\` HTTPS: \`${failedHttps}\``));
log(` ...failed: HTTP: \`${failedHttp}\` HTTPS: \`${failedHttps}\``, chalk.yellow);

// Save
failedCounter++;
Expand All @@ -226,7 +236,7 @@ const validateCNAMEs = async () => {
tests[cname] = data;
} else {
// Log
console.log(chalk.green(` ...succeeded`));
log(` ...succeeded`, chalk.green);

// Save
const data = cnames[cname];
Expand All @@ -245,7 +255,7 @@ const validateCNAMEs = async () => {
const data = tests[cname];
if (data.failed) failed[cname] = data;
}
console.log(chalk.greenBright.bold("Testing completed for validateCNAMEs"));
log("Testing completed for validateCNAMEs", chalk.greenBright.bold);
return failed
};

Expand Down
22 changes: 22 additions & 0 deletions log.js
@@ -1,15 +1,37 @@
// Load in chalk for logging
const chalk = require("chalk");

let logLevel = 0;

/*
* TODO: Method to increase indent (use '>' to indicate level) of log messages
*/
const logDown = () => {
logLevel++;
};

/*
* TODO: Method to decrease indent level of log messages
*/
const logUp = () => {
logLevel--;
if (logLevel < 0) logLevel = 0;
};

/*
* TODO: Method to log message, takes string and chalk color (callback essentially)
* This should make use of the indent level as controlled above
*/
const log = (message, chalkColor) => {
const indent = chalk.grey(`${">".repeat(logLevel)}${logLevel === 0 ? "" : " "}`);
const lines = message.split("\n");
lines.forEach(line => {
console.log(`${line ? indent : ""}${chalkColor(line)}`);
});
};

// TODO: Use this new logging method for all logging in script
// The aim being that this will make it visually easier to trace how methods are getting called in the stack

// Export
module.exports = {logDown, logUp, log};
55 changes: 36 additions & 19 deletions prs.js
@@ -1,3 +1,6 @@
// Load in custom logging
const {logDown, logUp, log} = require("./log.js");

// Load in custom caching
const {removeCache} = require("./cache.js");

Expand Down Expand Up @@ -26,38 +29,44 @@ const chalk = require("chalk");
*/
const perfectCNAMEsFile = async () => {
// Log
console.log(chalk.cyanBright.bold("\nStarting perfectCNAMEsFile process"));
log("\nStarting perfectCNAMEsFile process", chalk.cyanBright.bold);

// Get the original file
logDown();
const file = await getCNAMEsFile();
logUp();

// Get the raw cnames
logDown();
const cnames = await getCNAMEs(file);
logUp();

// Get the new file
logDown();
const newFile = await generateCNAMEsFile(cnames, file);
logUp();

// Log
console.log(chalk.cyanBright.bold("\nResuming perfectCNAMEsFile process"));
log("\nResuming perfectCNAMEsFile process", chalk.cyanBright.bold);

// Compare
if (newFile == file) {
// Log
console.log(chalk.yellow(" Existing file is already perfect, no changes"));
log(" Existing file is already perfect, no changes", chalk.green);

// Reset cache
console.log(chalk.blue(" Purging cache before completion"));
log(" Purging cache before completion", chalk.blue);
await removeCache("getCNAMEs");
await removeCache("generateCNAMEsFile");

// Done
console.log(chalk.greenBright.bold("Generation completed for perfectCNAMEsFile"));
log("Generation completed for perfectCNAMEsFile", chalk.greenBright.bold);
return;
}

// Create fork, commit & PR
console.log(chalk.yellow(" Changes are required to make the file perfect"));
console.log(chalk.blue(" Creating pull request with changes..."));
log(" Changes are required to make the file perfect", chalk.yellow);
log(" Creating pull request with changes...", chalk.blue);
const pr = await octokit.createPullRequest({
owner: config.repository_owner,
repo: config.repository_name,
Expand All @@ -73,13 +82,13 @@ const perfectCNAMEsFile = async () => {
});

// Reset cache
console.log(chalk.green(" ...pull request created"));
console.log(chalk.blue(" Purging cache before completion"));
log(" ...pull request created", chalk.green);
log(" Purging cache before completion", chalk.blue);
await removeCache("getCNAMEs");
await removeCache("generateCNAMEsFile");

// Done
console.log(chalk.greenBright.bold("Generation completed for perfectCNAMEsFile"));
log("Generation completed for perfectCNAMEsFile", chalk.greenBright.bold);
// TODO: waiting on https://github.com/gr2m/octokit-create-pull-request/pull/13 for PR data
/*return pr.data.html_url;*/
};
Expand All @@ -91,51 +100,59 @@ const perfectCNAMEsFile = async () => {
*/
const mainCleanupPull = async issueNumber => {
// Log
console.log(chalk.cyanBright.bold("\nStarting mainCleanupPull process"));
log("\nStarting mainCleanupPull process", chalk.cyanBright.bold);

// Fetch any cache we have
// TODO: waiting on https://github.com/gr2m/octokit-create-pull-request/pull/13 for PR data
/*const cache = await getCache("mainCleanupPull");
if (cache) {
console.log(chalk.greenBright.bold("Cached data found for mainCleanupPull"));
log("Cached data found for mainCleanupPull", chalk.greenBright.bold);
return cache.html_url;
}*/

// Get the file so we only need to fetch once
logDown();
const file = await getCNAMEsFile();
logUp();

// Fetch all cname data
logDown();
const allCNAMEs = await getCNAMEs(file);
logUp();

// Get the bad cnames
logDown();
const badCNAMEs = await parseIssueEntries(issueNumber);
logUp();

// Log
console.log(chalk.cyanBright.bold("\nResuming mainCleanupPull process"));
log("\nResuming mainCleanupPull process", chalk.cyanBright.bold);

// Generate new cname data w/o bad cnames
const newCNAMEs = {};
for (const cname in allCNAMEs) {
if (!allCNAMEs.hasOwnProperty(cname)) continue;
if (badCNAMEs.includes(cname)) {
console.log(chalk.blue(` Removed ${cname} from cnames_active`));
log(` Removed ${cname} from cnames_active`, chalk.blue);
continue;
}
newCNAMEs[cname] = allCNAMEs[cname];
}

// Generate new cnames_active
logDown();
const cnamesActive = await generateCNAMEsFile(newCNAMEs, file);
logUp();

// Log
console.log(chalk.cyanBright.bold("\nResuming mainCleanupPull process"));
log("\nResuming mainCleanupPull process", chalk.cyanBright.bold);

// Create PR info
const body = await mainPullRequest(issueNumber, badCNAMEs);
const name = `JS.ORG CLEANUP (#${issueNumber})`;

// Make pull request
console.log(chalk.blue(" Creating pull request with changes..."));
log(" Creating pull request with changes...", chalk.blue);
const pr = await octokit.createPullRequest({
owner: config.repository_owner,
repo: config.repository_name,
Expand All @@ -155,13 +172,13 @@ const mainCleanupPull = async issueNumber => {
/*await setCache("mainCleanupPull", pr.data);*/

// Reset cache
console.log(chalk.green(" ...pull request created"));
console.log(chalk.blue(" Purging cache before completion"));
log(" ...pull request created", chalk.green);
log(" Purging cache before completion", chalk.blue);
await removeCache("getCNAMEs");
await removeCache("parseIssueCNAMEs");

// Done
console.log(chalk.greenBright.bold("Generation completed for mainCleanupPull"));
log("Generation completed for mainCleanupPull", chalk.greenBright.bold);
// TODO: waiting on https://github.com/gr2m/octokit-create-pull-request/pull/13 for PR data
/*return pr.data.html_url;*/
};
Expand Down

0 comments on commit d8f49c0

Please sign in to comment.