Skip to content

Commit

Permalink
Refactor read/write logics
Browse files Browse the repository at this point in the history
  • Loading branch information
LitoMore committed May 3, 2024
1 parent 1c8b26b commit 0e5d99b
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 54 deletions.
16 changes: 8 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
165 changes: 164 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 8 additions & 9 deletions source/add-platform.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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);
21 changes: 12 additions & 9 deletions source/add.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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))}.`,
Expand Down
8 changes: 3 additions & 5 deletions source/build.ts
Original file line number Diff line number Diff line change
@@ -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 = `<h1 align="center">GAMES</h1>
`;
Expand Down Expand Up @@ -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.");
16 changes: 10 additions & 6 deletions source/check.ts
Original file line number Diff line number Diff line change
@@ -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 = () => {
Expand Down Expand Up @@ -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.");
8 changes: 3 additions & 5 deletions source/reset.ts
Original file line number Diff line number Diff line change
@@ -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 = [];
Expand All @@ -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.");
11 changes: 5 additions & 6 deletions source/search.ts
Original file line number Diff line number Diff line change
@@ -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 = [];

Expand Down
Loading

0 comments on commit 0e5d99b

Please sign in to comment.