Skip to content

Commit

Permalink
Fixes #1646.
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Feb 24, 2021
1 parent f39b93a commit 1993035
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 57 deletions.
48 changes: 30 additions & 18 deletions cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ try {
const Eleventy = require("./src/Eleventy");

process.on("unhandledRejection", (error, promise) => {
errorHandler.error(error, `Unhandled rejection in promise (${promise})`);
errorHandler.error(error, "Unhandled rejection in promise");
});
process.on("uncaughtException", (error) => {
errorHandler.fatal(error, "Uncaught exception");
Expand Down Expand Up @@ -86,42 +86,54 @@ try {

// careful, we can’t use async/await here to error properly
// with old node versions in `please-upgrade-node` above.
elev
.init()
.then(function () {
elev.init().then(function () {
try {
if (argv.version) {
console.log(elev.getVersion());
} else if (argv.help) {
console.log(elev.getHelp());
} else if (argv.serve) {
elev.watch().then(function () {
elev.serve(argv.port);
});
let startBrowsersync = true;
elev
.watch()
.catch((e) => {
// Build failed but error message already displayed.
startBrowsersync = false;
console.log("Watch catch");
})
.then(function () {
if (startBrowsersync) {
elev.serve(argv.port);
}
});
} else if (argv.watch) {
elev.watch();
elev.watch().catch((e) => {
console.log("watch catch 2");
});
} else {
if (argv.to === "json") {
elev.toJSON().then(function (result) {
console.log(JSON.stringify(result, null, 2));
});
} else if (argv.to === "ndjson") {
elev
.toNDJSON()
.then(function (stream) {
stream.pipe(process.stdout);
})
.catch(errorHandler.fatal.bind(errorHandler));
elev.toNDJSON().then(function (stream) {
stream.pipe(process.stdout);
});
} else if (!argv.to || argv.to === "fs") {
elev.write();
} else {
throw new EleventyCommandCheckError(
`Invalid --to value: ${argv.to}. Supported values: \`fs\`, \`json\`, and \`ndjson\`.`
`Invalid --to value: ${argv.to}. Supported values: \`fs\` (default), \`json\`, and \`ndjson\`.`
);
}
}
})
.catch(errorHandler.fatal.bind(errorHandler));
} catch (e) {
errorHandler.fatal(e, "Eleventy CLI Error");
}
});
// unlikely because we handle a lot of errors internally in Eleventy
// .catch(errorHandler.fatal.bind(errorHandler));
} catch (e) {
let errorHandler = new EleventyErrorHandler();
errorHandler.fatal(e, "Eleventy fatal error");
errorHandler.fatal(e, "Eleventy CLI Fatal Error");
}
61 changes: 36 additions & 25 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,8 @@ Arguments:
}
}

await this.write();
let writeResult = await this.write();
let hasError = !!writeResult.error;

this.writer.resetIncrementalFile();

Expand Down Expand Up @@ -699,7 +700,11 @@ Arguments:

// Note that watching indirectly depends on this for fetching dependencies from JS files
// See: TemplateWriter:pathCache and EleventyWatchTargets
await this.write();
let result = await this.write();
if (result.error) {
// build failed—quit watch early
return Promise.reject(result.error);
}

let initWatchBench = this.watcherBench.get("Start up --watch");
initWatchBench.before();
Expand All @@ -716,7 +721,7 @@ Arguments:

this.watcherBench.finish("Watch");

this.logger.log("Watching…");
this.logger.forceLog("Watching…");

this.watcher = watcher;

Expand All @@ -743,12 +748,12 @@ Arguments:
};

watcher.on("change", async (path) => {
this.logger.log(`File changed: ${path}`);
this.logger.forceLog(`File changed: ${path}`);
await watchRun(path);
});

watcher.on("add", async (path) => {
this.logger.log(`File added: ${path}`);
this.logger.forceLog(`File added: ${path}`);
await watchRun(path);
});

Expand Down Expand Up @@ -813,10 +818,10 @@ Arguments:
*/
async executeBuild(to = "fs") {
let ret;

await this.config.events.emit("beforeBuild");
let hasError = false;

try {
await this.config.events.emit("beforeBuild");
let promise;
if (to === "fs") {
promise = this.writer.write();
Expand All @@ -834,30 +839,36 @@ Arguments:

if (to === "ndjson") {
// return a stream
// TODO this might return only after all the templates have been added to the stream
// TODO this might output the ndjson rows only after all the templates have been written to the stream?
ret = this.logger.closeStream(to);
}

await this.config.events.emit("afterBuild");
} catch (e) {
this.errorHandler.initialMessage(
"Problem writing Eleventy templates",
"error",
"red"
);
this.errorHandler.fatal(e);
}

bench.finish();
if (to === "fs") {
this.logger.message(this.logFinished(), "info", "green", true);
}
debug("Finished writing templates.");
hasError = true;
ret = {
error: e,
};
this.errorHandler.fatal(e, "Problem writing Eleventy templates");
} finally {
// Note, this executes even though we return above in `catch`
bench.finish();
if (to === "fs") {
this.logger.message(
this.logFinished(),
"info",
hasError ? "red" : "green",
true
);
}
debug("Finished writing templates.");

debug(`
Getting frustrated? Have a suggestion/feature request/feedback?
I want to hear it! Open an issue: https://github.com/11ty/eleventy/issues/new`);
debug(`
Getting frustrated? Have a suggestion/feature request/feedback?
I want to hear it! Open an issue: https://github.com/11ty/eleventy/issues/new`);

return ret;
return ret;
}
}
}

Expand Down
27 changes: 20 additions & 7 deletions src/EleventyErrorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class EleventyErrorHandler {

error(e, msg) {
if (msg) {
this.initialMessage(msg, "error", "red");
this.initialMessage(msg, "error", "red", true);
}
this.log(e, "error");
this.log(e, "error", undefined, undefined, true);
}

//https://nodejs.org/api/process.html
log(e, type = "log", prefix = ">") {
log(e, type = "log", prefix = ">", chalkColor = "", forceToConsole = false) {
let ref = e;
while (ref) {
let nextRef = ref.originalError;
Expand All @@ -66,7 +66,9 @@ class EleventyErrorHandler {
).trim()}
\`${ref.name}\` was thrown${!nextRef && ref.stack ? ":" : ""}`,
type
type,
chalkColor,
forceToConsole
);

if (process.env.DEBUG) {
Expand All @@ -83,18 +85,29 @@ class EleventyErrorHandler {
"(Repeated output has been truncated…)"
);
}
this.logger.message(prefix + stackStr.split("\n").join("\n" + prefix));
this.logger.message(
prefix + stackStr.split("\n").join("\n" + prefix),
type,
chalkColor,
forceToConsole
);
}
ref = nextRef;
}
}

initialMessage(message, type = "log", chalkColor = "blue") {
initialMessage(
message,
type = "log",
chalkColor = "blue",
forceToConsole = false
) {
if (message) {
this.logger.message(
message + ":" + (process.env.DEBUG ? "" : " (more in DEBUG output)"),
type,
chalkColor
chalkColor,
forceToConsole
);
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/TemplateWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,8 @@ class TemplateWriter {
) {
promises.push(...(await this.generateTemplates(paths)));
}

return Promise.all(promises).catch((e) => {
this.errorHandler.error(e, "Error writing templates");
throw e;
return Promise.reject(e);
});
}

Expand Down
13 changes: 9 additions & 4 deletions src/Util/ConsoleLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,23 @@ class ConsoleLogger {
this._logger = logger;
}

log(msg) {
this.message(msg);
}

forceLog(msg) {
this.message(msg, undefined, undefined, true);
}

warn(msg) {
this.message(msg, "warn", "yellow");
}

// Is this used?
error(msg) {
this.message(msg, "error", "red");
}

log(msg) {
this.message(msg);
}

toStream(msg) {
this.outputStream.push(msg);
}
Expand Down

0 comments on commit 1993035

Please sign in to comment.