Skip to content

Commit

Permalink
improve format + more refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 21, 2023
1 parent 976631d commit e6c84f2
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions src/prerender.ts
Expand Up @@ -237,26 +237,7 @@ export async function prerender(nitro: Nitro) {
}

await nitro.hooks.callHook("prerender:route", _route);

if (_route.error) {
const parents = linkParents.get(_route.route);
const parentsText = parents?.size
? `\n${[...parents.values()]
.map((link) => chalk.gray(` │ └── Linked from ${link}`))
.join("\n")}`
: "";
nitro.logger.log(
chalk[_route.error.statusCode === 404 ? "yellow" : "red"](
` ├─ ${_route.route} (${
_route.generateTimeMS
}ms) ${`(${_route.error})`}${parentsText}`
)
);
} else {
nitro.logger.log(
chalk.gray(` ├─ ${_route.route} (${_route.generateTimeMS}ms)`)
);
}
nitro.logger.log(formatPrerenderRoute(_route));
}

await runParallel(routes, processRoute, {
Expand All @@ -273,11 +254,7 @@ export async function prerender(nitro: Nitro) {
.map((link) => chalk.gray(` │ └── Linked from ${link}`))
.join("\n")}`
: "";
nitro.logger.log(
chalk[route.error.statusCode === 404 ? "yellow" : "red"](
` ├─ ${route.route} (${route.error.statusCode})${parentsText}`
)
);
nitro.logger.log(formatPrerenderRoute(route));

Check warning on line 257 in src/prerender.ts

View check run for this annotation

Codecov / codecov/patch

src/prerender.ts#L251-L257

Added lines #L251 - L257 were not covered by tests
}
nitro.logger.log("");
throw new Error("Exiting due to prerender errors.");
Expand Down Expand Up @@ -381,3 +358,24 @@ function getExtension(link: string): string {
const pathname = parseURL(link).pathname;
return (pathname.match(EXT_REGEX) || [])[0] || "";
}

function formatPrerenderRoute(route: PrerenderGenerateRoute) {
if (!route.error) {
return chalk.gray(` ├─ ${route.route} (${route.generateTimeMS}ms)`);
}
const parents = linkParents.get(route.route);
const parentsText = parents?.size
? `\n${[...parents.values()]
.map((link) => ` │ └── Linked from ${link}`)
.join("\n")}`
: "";

Check warning on line 371 in src/prerender.ts

View check run for this annotation

Codecov / codecov/patch

src/prerender.ts#L371

Added line #L371 was not covered by tests
const color = chalk[route.error.statusCode === 404 ? "yellow" : "red"];
return (
color(` ├─ ${route.route} (${route.generateTimeMS}ms)`) +
chalk.gray(
`${`\n │ ${parentsText ? "├" : "└"}── ${chalk.bold(
route.error
)}`}${parentsText}`
)
);
}

0 comments on commit e6c84f2

Please sign in to comment.