Skip to content

Commit

Permalink
refactor: use strip-json-comments for .jsonc config
Browse files Browse the repository at this point in the history
  • Loading branch information
kidonng committed Jul 4, 2022
1 parent b980624 commit b7270df
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
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 { default as stripJsonComments } from "https://esm.sh/[email protected]";
10 changes: 3 additions & 7 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, stripJsonComments } from "../deps.ts";
import { ScriptsConfiguration } from "./scripts_config.ts";

const CONFIG_FILE_NAMES = ["scripts", "velociraptor"];
Expand Down Expand Up @@ -61,12 +61,8 @@ 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,
);
if (configPath.endsWith('.jsonc')) {
content = stripJsonComments(content);
}
const { velociraptor: config = {} } = JSON.parse(content);
return config as ScriptsConfiguration;
Expand Down

0 comments on commit b7270df

Please sign in to comment.