From 0e5d99b7c0ebf9842bdc600a46444c103cbb19c9 Mon Sep 17 00:00:00 2001 From: LitoMore Date: Fri, 3 May 2024 20:25:27 +0800 Subject: [PATCH] Refactor read/write logics --- Makefile | 16 ++-- deno.json | 3 +- deno.lock | 165 ++++++++++++++++++++++++++++++++++++++++- source/add-platform.ts | 17 ++--- source/add.ts | 21 +++--- source/build.ts | 8 +- source/check.ts | 16 ++-- source/reset.ts | 8 +- source/search.ts | 11 ++- source/summary.ts | 5 +- source/utils.ts | 18 ++++- 11 files changed, 234 insertions(+), 54 deletions(-) diff --git a/Makefile b/Makefile index fa6c38f..8036a97 100644 --- a/Makefile +++ b/Makefile @@ -5,27 +5,27 @@ lint: deno lint -q add: - cd source && deno run --allow-env --allow-read --allow-write add.ts "$(anchor)" "$(name)" "$(website)" + deno run --allow-env --allow-read --allow-write source/add.ts add-platform: - cd source && deno run --allow-env --allow-read --allow-write add-platform.ts "$(anchor)" "$(name)" "$(badge)" "$(hostnames)" + deno run --allow-env --allow-read --allow-write source/add-platform.ts check: - cd source && deno run --allow-env --allow-read check.ts + deno run --allow-env --allow-read source/check.ts fix: - cd source && deno run --allow-env --allow-read --allow-write check.ts --fix + deno run --allow-env --allow-read --allow-write source/check.ts --fix build: - cd source && deno run --allow-env --allow-read --allow-write build.ts + deno run --allow-env --allow-read --allow-write source/build.ts reset: - cd source && deno run --allow-env --allow-read --allow-write reset.ts + deno run --allow-env --allow-read --allow-write source/reset.ts search: - cd source && deno run --allow-env --allow-read search.ts "$(anchor)" "$(name)" + deno run --allow-env --allow-read source/search.ts summary: - cd source && deno run --allow-read summary.ts + deno run --allow-env --allow-read source/summary.ts default: lint check build diff --git a/deno.json b/deno.json index 1672c5e..0c2062f 100644 --- a/deno.json +++ b/deno.json @@ -1,5 +1,6 @@ { "imports": { - "@std/fmt": "jsr:@std/fmt@^0.224.0" + "@std/fmt": "jsr:@std/fmt@^0.224.0", + "@std/path": "jsr:@std/path@^0.224.0" } } diff --git a/deno.lock b/deno.lock index b2aecab..11afae2 100644 --- a/deno.lock +++ b/deno.lock @@ -2,19 +2,137 @@ "version": "3", "packages": { "specifiers": { + "jsr:@std/assert@^0.224.0": "jsr:@std/assert@0.224.0", "jsr:@std/fmt@^0.224.0": "jsr:@std/fmt@0.224.0", + "jsr:@std/path@^0.224.0": "jsr:@std/path@0.224.0", + "npm:@inquirer/input": "npm:@inquirer/input@2.1.6", "npm:log-symbols": "npm:log-symbols@6.0.0" }, "jsr": { + "@std/assert@0.224.0": { + "integrity": "8643233ec7aec38a940a8264a6e3eed9bfa44e7a71cc6b3c8874213ff401967f" + }, "@std/fmt@0.224.0": { "integrity": "e20e9a2312a8b5393272c26191c0a68eda8d2c4b08b046bad1673148f1d69851" + }, + "@std/path@0.224.0": { + "integrity": "55bca6361e5a6d158b9380e82d4981d82d338ec587de02951e2b7c3a24910ee6", + "dependencies": [ + "jsr:@std/assert@^0.224.0" + ] } }, "npm": { + "@inquirer/core@8.1.0": { + "integrity": "sha512-kfx0SU9nWgGe1f03ao/uXc85SFH1v2w3vQVH7QDGjKxdtJz+7vPitFtG++BTyJMYyYgH8MpXigutcXJeiQwVRw==", + "dependencies": { + "@inquirer/figures": "@inquirer/figures@1.0.1", + "@inquirer/type": "@inquirer/type@1.3.1", + "@types/mute-stream": "@types/mute-stream@0.0.4", + "@types/node": "@types/node@20.12.8", + "@types/wrap-ansi": "@types/wrap-ansi@3.0.0", + "ansi-escapes": "ansi-escapes@4.3.2", + "chalk": "chalk@4.1.2", + "cli-spinners": "cli-spinners@2.9.2", + "cli-width": "cli-width@4.1.0", + "mute-stream": "mute-stream@1.0.0", + "signal-exit": "signal-exit@4.1.0", + "strip-ansi": "strip-ansi@6.0.1", + "wrap-ansi": "wrap-ansi@6.2.0" + } + }, + "@inquirer/figures@1.0.1": { + "integrity": "sha512-mtup3wVKia3ZwULPHcbs4Mor8Voi+iIXEWD7wCNbIO6lYR62oPCTQyrddi5OMYVXHzeCSoneZwJuS8sBvlEwDw==", + "dependencies": {} + }, + "@inquirer/input@2.1.6": { + "integrity": "sha512-M8bUFOlcn/kQcVYskl4kkB6dYrHtymJJ1S4nSg/khXT3W3l71u2qhSzfo6PdBG3jUe6ILJZ0gUh4Kef2uJ5pxw==", + "dependencies": { + "@inquirer/core": "@inquirer/core@8.1.0", + "@inquirer/type": "@inquirer/type@1.3.1" + } + }, + "@inquirer/type@1.3.1": { + "integrity": "sha512-Pe3PFccjPVJV1vtlfVvm9OnlbxqdnP5QcscFEFEnK5quChf1ufZtM0r8mR5ToWHMxZOh0s8o/qp9ANGRTo/DAw==", + "dependencies": {} + }, + "@types/mute-stream@0.0.4": { + "integrity": "sha512-CPM9nzrCPPJHQNA9keH9CVkVI+WR5kMa+7XEs5jcGQ0VoAGnLv242w8lIVgwAEfmE4oufJRaTc9PNLQl0ioAow==", + "dependencies": { + "@types/node": "@types/node@18.16.19" + } + }, + "@types/node@18.16.19": { + "integrity": "sha512-IXl7o+R9iti9eBW4Wg2hx1xQDig183jj7YLn8F7udNceyfkbn1ZxmzZXuak20gR40D7pIkIY1kYGx5VIGbaHKA==", + "dependencies": {} + }, + "@types/node@20.12.8": { + "integrity": "sha512-NU0rJLJnshZWdE/097cdCBbyW1h4hEg0xpovcoAQYHl8dnEyp/NAOiE45pvc+Bd1Dt+2r94v2eGFpQJ4R7g+2w==", + "dependencies": { + "undici-types": "undici-types@5.26.5" + } + }, + "@types/wrap-ansi@3.0.0": { + "integrity": "sha512-ltIpx+kM7g/MLRZfkbL7EsCEjfzCcScLpkg37eXEtx5kmrAKBkTJwd1GIAjDSL8wTpM6Hzn5YO4pSb91BEwu1g==", + "dependencies": {} + }, + "ansi-escapes@4.3.2": { + "integrity": "sha512-gKXj5ALrKWQLsYG9jlTRmR/xKluxHV+Z9QEwNIgCfM1/uwPMCuzVVnh5mwTd+OuBZcwSIMbqssNWRm1lE51QaQ==", + "dependencies": { + "type-fest": "type-fest@0.21.3" + } + }, + "ansi-regex@5.0.1": { + "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", + "dependencies": {} + }, + "ansi-styles@4.3.0": { + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dependencies": { + "color-convert": "color-convert@2.0.1" + } + }, + "chalk@4.1.2": { + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "ansi-styles@4.3.0", + "supports-color": "supports-color@7.2.0" + } + }, "chalk@5.3.0": { "integrity": "sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==", "dependencies": {} }, + "cli-spinners@2.9.2": { + "integrity": "sha512-ywqV+5MmyL4E7ybXgKys4DugZbX0FC6LnwrhjuykIjnK9k8OQacQ7axGKnjDXWNhns0xot3bZI5h55H8yo9cJg==", + "dependencies": {} + }, + "cli-width@4.1.0": { + "integrity": "sha512-ouuZd4/dm2Sw5Gmqy6bGyNNNe1qt9RpmxveLSO7KcgsTnU7RXfsw+/bukWGo1abgBiMAic068rclZsO4IWmmxQ==", + "dependencies": {} + }, + "color-convert@2.0.1": { + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "dependencies": { + "color-name": "color-name@1.1.4" + } + }, + "color-name@1.1.4": { + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "dependencies": {} + }, + "emoji-regex@8.0.0": { + "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", + "dependencies": {} + }, + "has-flag@4.0.0": { + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "dependencies": {} + }, + "is-fullwidth-code-point@3.0.0": { + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "dependencies": {} + }, "is-unicode-supported@1.3.0": { "integrity": "sha512-43r2mRvz+8JRIKnWJ+3j8JtjRKZ6GmjzfaE/qiBJnikNnYv/6bagRJ1kUhNk8R5EX/GkobD+r+sfxCPJsiKBLQ==", "dependencies": {} @@ -25,6 +143,50 @@ "chalk": "chalk@5.3.0", "is-unicode-supported": "is-unicode-supported@1.3.0" } + }, + "mute-stream@1.0.0": { + "integrity": "sha512-avsJQhyd+680gKXyG/sQc0nXaC6rBkPOfyHYcFb9+hdkqQkR9bdnkJ0AMZhke0oesPqIO+mFFJ+IdBc7mst4IA==", + "dependencies": {} + }, + "signal-exit@4.1.0": { + "integrity": "sha512-bzyZ1e88w9O1iNJbKnOlvYTrWPDl46O1bG0D3XInv+9tkPrxrN8jUUTiFlDkkmKWgn1M6CfIA13SuGqOa9Korw==", + "dependencies": {} + }, + "string-width@4.2.3": { + "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", + "dependencies": { + "emoji-regex": "emoji-regex@8.0.0", + "is-fullwidth-code-point": "is-fullwidth-code-point@3.0.0", + "strip-ansi": "strip-ansi@6.0.1" + } + }, + "strip-ansi@6.0.1": { + "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", + "dependencies": { + "ansi-regex": "ansi-regex@5.0.1" + } + }, + "supports-color@7.2.0": { + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dependencies": { + "has-flag": "has-flag@4.0.0" + } + }, + "type-fest@0.21.3": { + "integrity": "sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w==", + "dependencies": {} + }, + "undici-types@5.26.5": { + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dependencies": {} + }, + "wrap-ansi@6.2.0": { + "integrity": "sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==", + "dependencies": { + "ansi-styles": "ansi-styles@4.3.0", + "string-width": "string-width@4.2.3", + "strip-ansi": "strip-ansi@6.0.1" + } } } }, @@ -33,7 +195,8 @@ }, "workspace": { "dependencies": [ - "jsr:@std/fmt@^0.224.0" + "jsr:@std/fmt@^0.224.0", + "jsr:@std/path@^0.224.0" ] } } diff --git a/source/add-platform.ts b/source/add-platform.ts index 194cb4d..82b92bd 100644 --- a/source/add-platform.ts +++ b/source/add-platform.ts @@ -1,12 +1,13 @@ import { bold, cyan } from "@std/fmt/colors"; import logSymbols from "npm:log-symbols"; -import { GamesJson } from "./types.ts"; -import { nameCompare } from "./utils.ts"; +import input from "npm:@inquirer/input"; +import { loadGamesJson, nameCompare, writeGamesJson } from "./utils.ts"; -const gamesJson = await Deno.readTextFile("./games.json"); -const games = JSON.parse(gamesJson) as GamesJson; - -const [anchor, name, badge, hostnames] = Deno.args; +const games = await loadGamesJson(); +const anchor = await input({ message: "anchor:" }); +const name = await input({ message: "name:" }); +const badge = await input({ message: "badge:" }); +const hostnames = await input({ message: "hostnames:" }); if (!(anchor && name && badge && hostnames)) { console.error( @@ -33,10 +34,8 @@ const platform = { }; games.platforms.push(platform); - games.platforms.sort(nameCompare()); -const jsonFile = JSON.stringify(games, null, 2); -await Deno.writeTextFile("./games.json", jsonFile + "\n"); +await writeGamesJson(games); console.log(logSymbols.success, `${name} added.`); console.log(platform); diff --git a/source/add.ts b/source/add.ts index 00e95a6..8a148c0 100644 --- a/source/add.ts +++ b/source/add.ts @@ -1,12 +1,17 @@ import { bold, cyan, green } from "@std/fmt/colors"; import logSymbols from "npm:log-symbols"; -import { GamesJson } from "./types.ts"; -import { nameCompare, websiteChecks } from "./utils.ts"; +import input from "npm:@inquirer/input"; +import { + loadGamesJson, + nameCompare, + websiteChecks, + writeGamesJson, +} from "./utils.ts"; -const gamesJson = await Deno.readTextFile("./games.json"); -const games = JSON.parse(gamesJson) as GamesJson; - -let [anchor, name, website] = Deno.args; +const games = await loadGamesJson(); +const anchor = await input({ message: "anchor:" }); +const name = await input({ message: "name:" }); +let website = await input({ message: "website:" }); if (!(anchor && name && website)) { console.error( @@ -60,11 +65,9 @@ const game = { }; foundPlatform.gameList.push(game); - foundPlatform.gameList.sort(nameCompare()); -const jsonFile = JSON.stringify(games, null, 2); -await Deno.writeTextFile("./games.json", jsonFile + "\n"); +await writeGamesJson(games); console.log( logSymbols.success, `${green(bold(name))} added to ${cyan(bold(foundPlatform.name))}.`, diff --git a/source/build.ts b/source/build.ts index 5aa7624..4cf3894 100644 --- a/source/build.ts +++ b/source/build.ts @@ -1,9 +1,7 @@ import logSymbols from "npm:log-symbols"; -import { GamesJson } from "./types.ts"; -import { nameCompare } from "./utils.ts"; +import { loadGamesJson, nameCompare, writeReadme } from "./utils.ts"; -const gamesJson = await Deno.readTextFile("./games.json"); -const games = JSON.parse(gamesJson) as GamesJson; +const games = await loadGamesJson(); const titleSection = `

GAMES

`; @@ -77,5 +75,5 @@ const relatedSection = games.related.length > 0 const markdownContent = titleSection + badgesSection + descriptionSection + ciSection + gamesSection + relatedSection; -await Deno.writeTextFile("../README.md", markdownContent); +await writeReadme(markdownContent); console.log(logSymbols.success, "Compiled successfully."); diff --git a/source/check.ts b/source/check.ts index 05b23bf..6166d33 100644 --- a/source/check.ts +++ b/source/check.ts @@ -1,9 +1,14 @@ import logSymbols from "npm:log-symbols"; import { Game, Platform } from "./types.ts"; -import { nameChecks, nameCompare, websiteChecks } from "./utils.ts"; - -const gamesJson = await Deno.readTextFile("./games.json"); -const games = JSON.parse(gamesJson); +import { + loadGamesJson, + nameChecks, + nameCompare, + websiteChecks, + writeGamesJson, +} from "./utils.ts"; + +const games = await loadGamesJson(); const withFix = Deno.args.includes("--fix"); const checkWebsite = () => { @@ -49,8 +54,7 @@ checkPlatformOrder(); checkGamesOrder(); if (withFix) { - const jsonFile = JSON.stringify(games, null, 2); - await Deno.writeTextFile("./games.json", jsonFile + "\n"); + await writeGamesJson(games); } console.log(logSymbols.success, "Validation passed."); diff --git a/source/reset.ts b/source/reset.ts index 7c0579a..bb5829e 100644 --- a/source/reset.ts +++ b/source/reset.ts @@ -1,8 +1,7 @@ import logSymbols from "npm:log-symbols"; -import { GamesJson } from "./types.ts"; +import { loadGamesJson, writeGamesJson } from "./utils.ts"; -const gamesJson = await Deno.readTextFile("./games.json"); -const games = JSON.parse(gamesJson) as GamesJson; +const games = await loadGamesJson(); games.platforms.forEach((platform) => { platform.hostnames = []; @@ -11,6 +10,5 @@ games.platforms.forEach((platform) => { games.related = []; -const jsonFile = JSON.stringify(games, null, 2); -await Deno.writeTextFile("./games.json", jsonFile + "\n"); +await writeGamesJson(games); console.log(logSymbols.success, "Reset to default done."); diff --git a/source/search.ts b/source/search.ts index 5d3cbab..7dff083 100644 --- a/source/search.ts +++ b/source/search.ts @@ -1,10 +1,9 @@ -import { GamesJson } from "./types.ts"; -import { fuzzyMatched } from "./utils.ts"; +import input from "npm:@inquirer/input"; +import { fuzzyMatched, loadGamesJson } from "./utils.ts"; -const gamesJson = await Deno.readTextFile("./games.json"); -const games = JSON.parse(gamesJson) as GamesJson; - -const [anchor, name] = Deno.args; +const games = await loadGamesJson(); +const anchor = await input({ message: "author:" }); +const name = await input({ message: "name:" }); const matchedGames = []; diff --git a/source/summary.ts b/source/summary.ts index 8a79dad..0b3ad5a 100644 --- a/source/summary.ts +++ b/source/summary.ts @@ -1,7 +1,6 @@ -import { GamesJson } from "./types.ts"; +import { loadGamesJson } from "./utils.ts"; -const gamesJson = await Deno.readTextFile("./games.json"); -const games = JSON.parse(gamesJson) as GamesJson; +const games = await loadGamesJson(); const summary = [ ...games.platforms.map( diff --git a/source/utils.ts b/source/utils.ts index b1b0e9d..9e32f50 100644 --- a/source/utils.ts +++ b/source/utils.ts @@ -1,5 +1,21 @@ +import { join } from "@std/path/join"; import logSymbols from "npm:log-symbols"; -import { Game, Platform } from "./types.ts"; +import { Game, GamesJson, Platform } from "./types.ts"; + +const gamesJsonPath = join(import.meta.dirname ?? ".", "games.json"); +const readmePath = join(import.meta.dirname ?? ".", "..", "README.md"); + +export const loadGamesJson = async () => { + const file = await Deno.readTextFile(gamesJsonPath); + const games = JSON.parse(file) as GamesJson; + return games; +}; + +export const writeGamesJson = (games: GamesJson) => + Deno.writeTextFile(gamesJsonPath, JSON.stringify(games, null, 2) + "\n"); + +export const writeReadme = (content: string) => + Deno.writeTextFile(readmePath, content); const normalizeName = (name: string) => name.normalize("NFD").replace(/[\u0300-\u036f]/g, "");