From efae71195d1d9fd9a2597bf8990b59d6f63fb4c3 Mon Sep 17 00:00:00 2001 From: Bauke Date: Wed, 22 Feb 2023 11:31:26 +0100 Subject: [PATCH] Make pretty-printing JSON a separate function. --- .bauke/scripts/drg-data-entry.ts | 6 ++++-- .bauke/scripts/utilities.ts | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.bauke/scripts/drg-data-entry.ts b/.bauke/scripts/drg-data-entry.ts index 9a11734..0b1ea18 100644 --- a/.bauke/scripts/drg-data-entry.ts +++ b/.bauke/scripts/drg-data-entry.ts @@ -1,6 +1,8 @@ import { Command } from "https://deno.land/x/cliffy@v0.25.5/command/mod.ts"; import * as prompt from "https://deno.land/x/cliffy@v0.25.5/prompt/mod.ts"; +import { stringifyJsonPretty } from "./utilities.ts"; + const CaveComplexity = [1, 2, 3] as const; const CaveLength = [1, 2, 3] as const; const HazardLevel = [1, 2, 3, 4, 5] as const; @@ -506,14 +508,14 @@ async function main(): Promise { }; if (options.testing) { - console.log(newMission); + console.log(stringifyJsonPretty(newMission)); return; } dataMissions.push(newMission); await Deno.writeTextFile( options.dataFile, - JSON.stringify(dataMissions, null, 2) + "\n", + stringifyJsonPretty(dataMissions) + "\n", ); } } diff --git a/.bauke/scripts/utilities.ts b/.bauke/scripts/utilities.ts index 7813e2f..9b9f110 100644 --- a/.bauke/scripts/utilities.ts +++ b/.bauke/scripts/utilities.ts @@ -1,6 +1,10 @@ import { parse } from "https://deno.land/std@0.167.0/encoding/toml.ts"; import { TextDecoder } from "https://deno.land/std@0.167.0/node/util.ts"; +export function stringifyJsonPretty(input: unknown): string { + return JSON.stringify(input, null, 2); +} + export async function runAndReturnStdout( options: Deno.RunOptions, ): Promise {