From 8c06a4bc96bfb14a0fedf7b5300878537b82ce0a Mon Sep 17 00:00:00 2001 From: Kai Jellinghaus Date: Thu, 24 Nov 2022 21:06:53 +0100 Subject: [PATCH] Rework input parsing to try-catch JSON --- cli/script.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/cli/script.ts b/cli/script.ts index 463e5b5bd3..bf2e276bb9 100644 --- a/cli/script.ts +++ b/cli/script.ts @@ -180,24 +180,28 @@ export async function resolve(inputs: string[]): Promise> { } for (const input of inputs) { - let jsonObj; + let data: string; if (input.startsWith("@")) { - let data: string; if (input == "@-") { data = new TextDecoder().decode(await readAll(Deno.stdin)); } else { data = await Deno.readTextFile(input.substring(1)); } - jsonObj = JSON.parse(data); } else { if (input.startsWith("{")) { - jsonObj = JSON.parse(input); + data = input; } else { const key = input.split("=", 1)[0]; const value = JSON.parse(input.substring(key.length + 1)); - jsonObj = Object.fromEntries([[key, value]]); + data = JSON.stringify(Object.fromEntries([[key, value]])); } } + let jsonObj; + try { + jsonObj = JSON.parse(data); + } catch { + jsonObj = data; + } result = { ...result, ...jsonObj }; } return result;