Skip to content

Commit

Permalink
✨ Add error log output feature
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 18, 2021
1 parent 1e8c8dc commit 151f6ea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
15 changes: 8 additions & 7 deletions format/json.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
// Copyright 2021-present the Nameable authors. All rights reserved. MIT license.
import { entries, ifElse, isBoolean, length, N } from "../deps.ts";
import { entries, ifElse, isBoolean, isString, length, NN } from "../deps.ts";
const json = (val: (readonly ["deno.land", boolean])[]) =>
val.reduce((acc, [registry, isAvailable]) => {
return { ...acc, [registry]: isAvailable };
}, {} as Record<"deno.land", boolean>);

const summarize = (val: (readonly [string, boolean | Error])[]): {
const summarize = (val: (readonly [string, boolean | string])[]): {
result: Record<string, boolean>;
hasError: boolean;
errors: [string, Error][];
errors: [string, string][];
} => {
const errors = val.filter(([_, result]) =>
N(isBoolean(result))
) as ([string, Error])[];
const errors = val.filter(([_, result]) => isString(result)) as [
string,
string,
][];

const result = val.map((
[registry, result],
Expand All @@ -23,7 +24,7 @@ const summarize = (val: (readonly [string, boolean | Error])[]): {

return {
result: json(result),
hasError: N(length(errors)),
hasError: NN(length(errors)),
errors,
};
};
Expand Down
11 changes: 10 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,17 @@ const checkName = async (
const resultAll = await Promise.all(
(query as any[]).map((fn) => fn(name)),
);
const { result } = summarize(resultAll);
const { result, errors, hasError } = summarize(resultAll);

logger("Results:");

logger(outputFormat(json, result));
if (hasError) {
console.log("\nErrors:");
errors.forEach(([registry, message]) => {
logger(`${registry}: `, message);
});
}
};

const pickKeys = <T, U>(map: Record<PropertyKey, T>, val: U[]): T[] =>
Expand Down

0 comments on commit 151f6ea

Please sign in to comment.