diff --git a/deps.ts b/deps.ts index d6f22f2..2375a8b 100644 --- a/deps.ts +++ b/deps.ts @@ -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"; diff --git a/format/json.ts b/format/json.ts index b52ac88..4f177b7 100644 --- a/format/json.ts +++ b/format/json.ts @@ -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; + 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 }; diff --git a/log/log.ts b/log/log.ts new file mode 100644 index 0000000..168d795 --- /dev/null +++ b/log/log.ts @@ -0,0 +1,9 @@ +import { ifElse } from "../deps.ts"; +const loggerFactory = (isSilent: boolean) => + ifElse( + isSilent, + () => (..._: any[]) => {}, + () => (...message: any[]) => console.log(...message), + ); + +export { loggerFactory }; diff --git a/mod.ts b/mod.ts index 5dff2e7..bd15687 100644 --- a/mod.ts +++ b/mod.ts @@ -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 }; diff --git a/request/deno_land.ts b/request/deno_land.ts index 2c5135d..90b3eb5 100644 --- a/request/deno_land.ts +++ b/request/deno_land.ts @@ -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; @@ -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 };