diff --git a/README.md b/README.md index 9262721..478dae8 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ Internal module to help eggs handle global updates ## Module ```ts -import * from "https://x.nest.land/hatcher@0.8.1/mod.ts" -import * from "https://x.nest.land/hatcher@0.8.1/lib/registries.ts" +import * from "https://x.nest.land/hatcher@0.8.2/mod.ts" +import * from "https://x.nest.land/hatcher@0.8.2/lib/registries.ts" /** Install update handler cli to check for updates and notify user */ function installUpdateHandler(moduleName: string, execName: string, updateCheckInterval?: number): Promise @@ -31,5 +31,5 @@ function analyzeURL(url: string): { ## CLI ```bash -deno run https://x.nest.land/hatcher@0.8.1/cli.ts [ARGS...] +deno run https://x.nest.land/hatcher@0.8.2/cli.ts [ARGS...] ``` diff --git a/deps.ts b/deps.ts index 832cdf9..cb01cfd 100644 --- a/deps.ts +++ b/deps.ts @@ -1,17 +1,16 @@ -export { writeJson } from "https://x.nest.land/std@0.65.0/fs/write_json.ts"; - -export { readJson } from "https://x.nest.land/std@0.65.0/fs/read_json.ts"; - -export * as colors from "https://x.nest.land/std@0.65.0/fmt/colors.ts"; - -export * as semver from "https://deno.land/x/semver@v1.0.0/mod.ts"; - -export { Table } from "https://x.nest.land/cliffy@0.11.1/table.ts"; +/**************** std ****************/ +export * as colors from "https://x.nest.land/std@0.69.0/fmt/colors.ts"; export { assertEquals, assertMatch, assert, -} from "https://x.nest.land/std@0.65.0/testing/asserts.ts"; +} from "https://x.nest.land/std@0.69.0/testing/asserts.ts"; + +export * as path from "https://x.nest.land/std@0.69.0/path/mod.ts"; + +/**************** semver ****************/ +export * as semver from "https://deno.land/x/semver@v1.0.0/mod.ts"; -export * as path from "https://x.nest.land/std@0.65.0/path/mod.ts"; +/**************** cliffy ****************/ +export { Table, Cell } from "https://x.nest.land/cliffy@0.14.1/table/mod.ts"; diff --git a/egg.json b/egg.json index 5ced116..27a92c4 100644 --- a/egg.json +++ b/egg.json @@ -1,7 +1,7 @@ { "name": "hatcher", "description": "Utility module to handle module updates", - "version": "0.8.1", + "version": "0.8.2", "unlisted": true, "stable": true, "files": [ diff --git a/foo.ts b/foo.ts new file mode 100644 index 0000000..e5bc175 --- /dev/null +++ b/foo.ts @@ -0,0 +1 @@ +console.log("FOO"); diff --git a/lib/config.ts b/lib/config.ts index 804b6d7..516d3d8 100644 --- a/lib/config.ts +++ b/lib/config.ts @@ -1,4 +1,5 @@ -import { path, readJson, writeJson } from "../deps.ts"; +import { path } from "../deps.ts"; +import { readJson, writeJson } from "./utilities/json.ts"; export type GlobalModuleConfig = { [key: string]: GlobalModule; diff --git a/lib/update.ts b/lib/update.ts index 4094a39..b95e66e 100644 --- a/lib/update.ts +++ b/lib/update.ts @@ -115,7 +115,7 @@ export async function installUpdateHandler( "-A", "-n", module, - "https://x.nest.land/hatcher@0.8.1/cli.ts", + "https://x.nest.land/hatcher@0.8.2/cli.ts", executable, updateCheckInterval.toString(), ], diff --git a/lib/utilities/json.ts b/lib/utilities/json.ts new file mode 100644 index 0000000..577589c --- /dev/null +++ b/lib/utilities/json.ts @@ -0,0 +1,84 @@ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +type Replacer = (key: string, value: any) => any; + +export interface WriteJsonOptions extends Deno.WriteFileOptions { + replacer?: Array | Replacer; + spaces?: number | string; +} + +function serialize( + filePath: string, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + object: any, + options: WriteJsonOptions, +): string { + try { + const jsonString = JSON.stringify( + object, + options.replacer as string[], + options.spaces, + ); + return `${jsonString}\n`; + } catch (err) { + err.message = `${filePath}: ${err.message}`; + throw err; + } +} + +/* Writes an object to a JSON file. */ +export async function writeJson( + filePath: string, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + object: any, + options: WriteJsonOptions = {}, +): Promise { + const jsonString = serialize(filePath, object, options); + await Deno.writeTextFile(filePath, jsonString, { + append: options.append, + create: options.create, + mode: options.mode, + }); +} + +/* Writes an object to a JSON file. */ +export function writeJsonSync( + filePath: string, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + object: any, + options: WriteJsonOptions = {}, +): void { + const jsonString = serialize(filePath, object, options); + Deno.writeTextFileSync(filePath, jsonString, { + append: options.append, + create: options.create, + mode: options.mode, + }); +} + +/** Reads a JSON file and then parses it into an object */ +export async function readJson(filePath: string): Promise { + const decoder = new TextDecoder("utf-8"); + + const content = decoder.decode(await Deno.readFile(filePath)); + + try { + return JSON.parse(content); + } catch (err) { + err.message = `${filePath}: ${err.message}`; + throw err; + } +} + +/** Reads a JSON file and then parses it into an object */ +export function readJsonSync(filePath: string): unknown { + const decoder = new TextDecoder("utf-8"); + + const content = decoder.decode(Deno.readFileSync(filePath)); + + try { + return JSON.parse(content); + } catch (err) { + err.message = `${filePath}: ${err.message}`; + throw err; + } +} diff --git a/lib/utils.ts b/lib/utils.ts index 5a06e5d..b873074 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,4 +1,4 @@ -import { semver, Table } from "../deps.ts"; +import { semver, Table, Cell } from "../deps.ts"; export const versionSubstitute = "${version}"; @@ -26,9 +26,9 @@ export function latest(list: T[]) { } export function box(text: string) { - console.log(""); - Table.from([[text]]) - .padding(1) + new Table() + .header([Cell.from("").border(false)]) + .body([[text]]) .indent(2) .border(true) .render();