Skip to content

Commit

Permalink
✨ Add logger and accept arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 17, 2021
1 parent 2394c27 commit 1bb5d23
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 14 deletions.
4 changes: 3 additions & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright 2021-present the Nameable authors. All rights reserved. MIT license.
export {
first,
and,
has,
ifElse,
isBoolean,
isUndefined,
length,
map,
N,
NN,
or,
props,
} from "https://deno.land/x/fonction@v1.7.0/mod.ts";
26 changes: 25 additions & 1 deletion format/json.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import { ifElse, isBoolean, length, N } from "../deps.ts";
const json = (val: (readonly ["deno.land", boolean])[]) =>
val.reduce((acc, [registry, isAvailable]) => {
return { ...acc, [registry]: isAvailable };
}, {} as Record<"deno.land", boolean>);

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

const result = val.map((
[registry, result],
) => [registry, ifElse(isBoolean(result), result, false)]) as (readonly [
"deno.land",
boolean,
])[];

return {
result: json(result),
hasError: N(length(errors)),
errors,
};
};

export { json, summarize };
9 changes: 9 additions & 0 deletions log/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ifElse } from "../deps.ts";
const loggerFactory = (isSilent: boolean) =>
ifElse(
isSilent,
() => (..._: any[]) => {},
() => (...message: any[]) => console.log(...message),
);

export { loggerFactory };
34 changes: 28 additions & 6 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,36 @@ import { query } from "./request/deno_land.ts";
import { query as queryNestLand } from "./request/nest_land.ts";
import { query as queryNpm } from "./request/npm.ts";
// import { info } from "https://deno.land/std@0.96.0/log/mod.ts";
import { json } from "./format/json.ts";
import { summarize } from "./format/json.ts";
import { loggerFactory } from "./log/log.ts";

const run = async (name: string) => {
console.log(`Check name: ${name}\n`);
const result = await Promise.all(
type Option = {
silent: boolean;
json: boolean;
registry: [];
};

const defaultOption: Option = {
silent: false,
json: true,
registry: [],
};

const checkName = async (
name: string,
option?: Partial<
Option
>,
) => {
const { silent = defaultOption.silent } = option || defaultOption;
const logger = loggerFactory(silent);
logger(`Check name: ${name}\n`);

const resultAll = await Promise.all(
([query, queryNestLand, queryNpm] as any[]).map((fn) => fn(name)),
);
console.log(json(result as any));
const { result } = summarize(resultAll);
logger(result);
};

export { run };
export { checkName };
23 changes: 17 additions & 6 deletions request/deno_land.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2021-present the Nameable authors. All rights reserved. MIT license.
import { DENO_LAND_BASE_URL } from "../constants.ts";
import { N, or, props } from "../deps.ts";
import { and, isBoolean, N, NN, or, props } from "../deps.ts";

type Response = {
success: boolean;
Expand All @@ -11,17 +11,28 @@ type Response = {
};
};

const query = async (search: string) => {
const simpleCompare = async (search: string) => {
const modulesUrl = new URL(search, DENO_LAND_BASE_URL);

try {
const res = await fetch(modulesUrl.toString());
const { success, data } = await res.json() as Response;
const isAvailable = or(N(success), () => props("name", data) !== search);
return ["deno.land", isAvailable] as const;
} catch {
throw Error("Error:");
return isAvailable;
} catch (e) {
return e as Error;
}
};

const query = async (search: string) => {
const result = await simpleCompare(search);
if (
or(and(isBoolean(result), () => NN(result)), () => result instanceof Error)
) {
return ["deno.land", result] as const;
}

return ["deno.land", false] as const;
};

export { query };
export { query, simpleCompare };

0 comments on commit 1bb5d23

Please sign in to comment.