Skip to content

Commit

Permalink
A little refactor for #182
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Sep 13, 2018
1 parent 86368a8 commit 1b8ab3f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 41 deletions.
37 changes: 19 additions & 18 deletions cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,29 @@ require("please-upgrade-node")(pkg, {
});

if (process.env.DEBUG) {
require("time-require");
let timeRequire = require("time-require");
}

const argv = require("minimist")(process.argv.slice(2));
const Eleventy = require("./src/Eleventy");
const EleventyCommandCheck = require("./src/EleventyCommandCheck");
const EleventyErrorHandler = require("./src/EleventyErrorHandler");

process.on("unhandledRejection", (error, promise) => {
EleventyErrorHandler.error(promise, "Unhandled rejection in promise");
});
process.on("uncaughtException", e => {
EleventyErrorHandler.fatal(e, "Uncaught exception");
});
process.on("rejectionHandled", promise => {
EleventyErrorHandler.warn(
promise,
"A promise rejection was handled asynchronously"
);
});

try {
const argv = require("minimist")(process.argv.slice(2));
const Eleventy = require("./src/Eleventy");
const EleventyCommandCheck = require("./src/EleventyCommandCheck");

process.on("unhandledRejection", (error, promise) => {
EleventyErrorHandler.error(promise, "Unhandled rejection in promise");
});
process.on("uncaughtException", e => {
EleventyErrorHandler.fatal(e, "Uncaught exception");
});
process.on("rejectionHandled", promise => {
EleventyErrorHandler.warn(
promise,
"A promise rejection was handled asynchronously"
);
});

let cmdCheck = new EleventyCommandCheck(argv);
cmdCheck.hasUnknownArguments();

Expand Down Expand Up @@ -66,5 +67,5 @@ try {
})
.catch(EleventyErrorHandler.fatal);
} catch (e) {
EleventyErrorHandler.fatal(e);
EleventyErrorHandler.fatal(e, "Eleventy fatal error");
}
11 changes: 1 addition & 10 deletions src/Config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
const chalk = require("chalk");
const TemplateConfig = require("./TemplateConfig");
const EleventyErrorHandler = require("./EleventyErrorHandler");
const debug = require("debug")("Eleventy:Config");

debug("Setting up global TemplateConfig.");
let config;
try {
config = new TemplateConfig();
} catch (e) {
console.log("\n" + chalk.red("Problem with Eleventy configuration: "));

EleventyErrorHandler.log(e);
}
let config = new TemplateConfig();

module.exports = config;
40 changes: 29 additions & 11 deletions src/EleventyErrorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const debug = require("debug")("Eleventy:EleventyErrorHandler");

class EleventyErrorHandler {
static warn(e, msg) {
EleventyErrorHandler.message(msg, "warn", "yellow");
EleventyErrorHandler.initialMessage(msg, "warn", "yellow");
EleventyErrorHandler.log(e, "warn");
}

Expand All @@ -13,24 +13,42 @@ class EleventyErrorHandler {
}

static error(e, msg) {
EleventyErrorHandler.message(msg, "error", "red");
EleventyErrorHandler.initialMessage(msg, "error", "red");
EleventyErrorHandler.log(e, "error");
}

static message(message, type = "log", chalkColor = "blue") {
static log(e, type = "log", prefix = ">") {
let ref = e;
while (ref) {
EleventyErrorHandler.message(
(process.env.DEBUG ? "" : `${prefix} `) +
`${ref.message} (${ref.name})`,
type
);
debug(`(${type} stack): %O`, ref.stack);
ref = ref.originalError;
}
}

static initialMessage(message, type = "log", chalkColor = "blue") {
if (message) {
console[type](
chalk[chalkColor](message + ": (full stack in DEBUG output)")
EleventyErrorHandler.message(
message + ": (full stack in DEBUG output)",
type,
chalkColor
);
}
}

static log(e, type = "log", prefix = ">") {
let ref = e;
while (ref) {
console[type](`${prefix} ${ref.message} (${ref.name})`);
debug(`(${type}): %O`, ref.stack);
ref = ref.originalError;
static message(message, type = "log", chalkColor) {
if (process.env.DEBUG) {
debug(`(${type}): ${message}`);
} else {
if (chalkColor) {
console[type](chalk[chalkColor](message));
} else {
console[type](message);
}
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/TemplateConfig.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const chalk = require("chalk");
const fs = require("fs-extra");
const lodashMerge = require("lodash.merge");
const TemplatePath = require("./TemplatePath");
const EleventyBaseError = require("./EleventyBaseError");
const EleventyErrorHandler = require("./EleventyErrorHandler");
const eleventyConfig = require("./EleventyConfig");
const debug = require("debug")("Eleventy:TemplateConfig");

Expand Down

0 comments on commit 1b8ab3f

Please sign in to comment.