Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use standard library to parse JSONC config #117

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ export {
UpdateNotifier,
} from "https://x.nest.land/[email protected]/mod.ts";
export * as semver from "https://deno.land/x/[email protected]/mod.ts";
export { parse as parseJson } from "https://deno.land/[email protected]/encoding/jsonc.ts";
13 changes: 3 additions & 10 deletions src/load_config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, parseYaml, path } from "../deps.ts";
import { existsSync, parseYaml, path, parseJson } from "../deps.ts";
import { ScriptsConfiguration } from "./scripts_config.ts";

const CONFIG_FILE_NAMES = ["scripts", "velociraptor"];
Expand Down Expand Up @@ -60,14 +60,7 @@ async function parseConfig(
async function parseDenoConfig(
configPath: string,
): Promise<ScriptsConfiguration> {
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,
);
}
const { velociraptor: config = {} } = JSON.parse(content);
const content = Deno.readTextFileSync(configPath);
const { velociraptor: config = {} } = parseJson(content);
return config as ScriptsConfiguration;
}