From b7270df017760a894374f9a67bff0f31d87a98ec Mon Sep 17 00:00:00 2001 From: Kid Date: Tue, 5 Jul 2022 02:49:53 +0800 Subject: [PATCH 1/2] refactor: use `strip-json-comments` for `.jsonc` config --- deps.ts | 1 + src/load_config.ts | 10 +++------- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/deps.ts b/deps.ts index 9ab8648..c908727 100644 --- a/deps.ts +++ b/deps.ts @@ -29,3 +29,4 @@ export { UpdateNotifier, } from "https://x.nest.land/hatcher@0.10.2/mod.ts"; export * as semver from "https://deno.land/x/semver@v1.4.0/mod.ts"; +export { default as stripJsonComments } from "https://esm.sh/strip-json-comments@4.0.0"; diff --git a/src/load_config.ts b/src/load_config.ts index ef679b2..5268689 100644 --- a/src/load_config.ts +++ b/src/load_config.ts @@ -1,4 +1,4 @@ -import { existsSync, parseYaml, path } from "../deps.ts"; +import { existsSync, parseYaml, path, stripJsonComments } from "../deps.ts"; import { ScriptsConfiguration } from "./scripts_config.ts"; const CONFIG_FILE_NAMES = ["scripts", "velociraptor"]; @@ -61,12 +61,8 @@ async function parseDenoConfig( configPath: string, ): Promise { let content = Deno.readTextFileSync(configPath); - // Strips comments for .jsonc (credits to @tarkh) - if (/\.jsonc$/.test(configPath)) { - content = content.replace( - /\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, - (m, g) => g ? "" : m, - ); + if (configPath.endsWith('.jsonc')) { + content = stripJsonComments(content); } const { velociraptor: config = {} } = JSON.parse(content); return config as ScriptsConfiguration; From cf813d2200dcd02bc664f4ded95374de06ddaf5b Mon Sep 17 00:00:00 2001 From: Kid <44045911+kidonng@users.noreply.github.com> Date: Mon, 8 Aug 2022 03:54:03 +0000 Subject: [PATCH 2/2] refactor: prefer std --- deps.ts | 2 +- src/load_config.ts | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/deps.ts b/deps.ts index c908727..a1c331d 100644 --- a/deps.ts +++ b/deps.ts @@ -29,4 +29,4 @@ export { UpdateNotifier, } from "https://x.nest.land/hatcher@0.10.2/mod.ts"; export * as semver from "https://deno.land/x/semver@v1.4.0/mod.ts"; -export { default as stripJsonComments } from "https://esm.sh/strip-json-comments@4.0.0"; +export { parse as parseJson } from "https://deno.land/std@0.151.0/encoding/jsonc.ts"; diff --git a/src/load_config.ts b/src/load_config.ts index 5268689..a70bf4d 100644 --- a/src/load_config.ts +++ b/src/load_config.ts @@ -1,4 +1,4 @@ -import { existsSync, parseYaml, path, stripJsonComments } from "../deps.ts"; +import { existsSync, parseYaml, path, parseJson } from "../deps.ts"; import { ScriptsConfiguration } from "./scripts_config.ts"; const CONFIG_FILE_NAMES = ["scripts", "velociraptor"]; @@ -60,10 +60,7 @@ async function parseConfig( async function parseDenoConfig( configPath: string, ): Promise { - let content = Deno.readTextFileSync(configPath); - if (configPath.endsWith('.jsonc')) { - content = stripJsonComments(content); - } - const { velociraptor: config = {} } = JSON.parse(content); + const content = Deno.readTextFileSync(configPath); + const { velociraptor: config = {} } = parseJson(content); return config as ScriptsConfiguration; }