Skip to content

Commit

Permalink
feat: support deno.json config file
Browse files Browse the repository at this point in the history
  • Loading branch information
lowlighter committed Dec 27, 2021
1 parent 971b7db commit a4e1101
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cli_test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { assertEquals, assertMatch, assertStringIncludes } from "./dev_deps.ts";
const yamlWd = "./test/yaml";
const tsWd = "./test/ts";
const jsonWd = "./test/json";
const cliArgs = [
"deno",
"run",
Expand Down Expand Up @@ -46,6 +47,11 @@ Deno.test("ts config file", async () => {
assertStringIncludes(output, expectedOutput);
});

Deno.test("deno.json(c) config file", async () => {
const output = await runScript("test", jsonWd);
assertStringIncludes(output, expectedOutput);
});

Deno.test("deno run", async () => {
const output = await runScript("denorun");
assertStringIncludes(output, expectedOutput);
Expand Down Expand Up @@ -91,6 +97,11 @@ Deno.test("--help", async () => {
assertStringIncludes(output, "--version");
});

Deno.test("--config", async () => {
const output = await runScript("config", yamlWd);
assertMatch(output, /^\s*$/);
});

Deno.test("forward arguments", async () => {
const output = await runScript("forward", yamlWd, ["--shuffle=1234"]);
assertStringIncludes(
Expand Down
10 changes: 10 additions & 0 deletions src/build_command_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum DenoOptions {
allow = "allow",
cachedOnly = "cachedOnly",
cert = "cert",
config = "config",
imap = "imap",
inspect = "inspect",
inspectBrk = "inspectBrk",
Expand All @@ -25,6 +26,7 @@ enum DenoOptions {
const denoCmdOptions: { [key: string]: DenoOptions[] } = {
bundle: [
DenoOptions.cert,
DenoOptions.config,
DenoOptions.imap,
DenoOptions.lock,
DenoOptions.log,
Expand All @@ -39,6 +41,7 @@ const denoCmdOptions: { [key: string]: DenoOptions[] } = {
DenoOptions.allow,
DenoOptions.cachedOnly,
DenoOptions.cert,
DenoOptions.config,
DenoOptions.imap,
DenoOptions.inspect,
DenoOptions.inspectBrk,
Expand All @@ -56,6 +59,7 @@ const denoCmdOptions: { [key: string]: DenoOptions[] } = {
DenoOptions.allow,
DenoOptions.cachedOnly,
DenoOptions.cert,
DenoOptions.config,
DenoOptions.imap,
DenoOptions.inspect,
DenoOptions.inspectBrk,
Expand All @@ -74,6 +78,7 @@ const denoCmdOptions: { [key: string]: DenoOptions[] } = {
DenoOptions.allow,
DenoOptions.cachedOnly,
DenoOptions.cert,
DenoOptions.config,
DenoOptions.imap,
DenoOptions.inspect,
DenoOptions.inspectBrk,
Expand All @@ -90,6 +95,7 @@ const denoCmdOptions: { [key: string]: DenoOptions[] } = {
],
cache: [
DenoOptions.cert,
DenoOptions.config,
DenoOptions.imap,
DenoOptions.lock,
DenoOptions.log,
Expand All @@ -110,6 +116,7 @@ const denoCmdOptions: { [key: string]: DenoOptions[] } = {
eval: [
DenoOptions.cachedOnly,
DenoOptions.cert,
DenoOptions.config,
DenoOptions.imap,
DenoOptions.inspect,
DenoOptions.inspectBrk,
Expand All @@ -126,6 +133,7 @@ const denoCmdOptions: { [key: string]: DenoOptions[] } = {
repl: [
DenoOptions.cachedOnly,
DenoOptions.cert,
DenoOptions.config,
DenoOptions.imap,
DenoOptions.inspect,
DenoOptions.inspectBrk,
Expand All @@ -140,11 +148,13 @@ const denoCmdOptions: { [key: string]: DenoOptions[] } = {
DenoOptions.v8Flags,
],
fmt: [
DenoOptions.config,
DenoOptions.log,
DenoOptions.quiet,
DenoOptions.unstable,
],
lint: [
DenoOptions.config,
DenoOptions.log,
DenoOptions.quiet,
DenoOptions.unstable,
Expand Down
25 changes: 25 additions & 0 deletions src/load_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ScriptsConfiguration } from "./scripts_config.ts";

const CONFIG_FILE_NAMES = ["scripts", "velociraptor"];
const CONFIG_FILE_EXTENSIONS = ["yaml", "yml", "json", "ts"];
const CONFIG_DENO_FILE_NAMES = ["deno.json", "deno.jsonc"];

export interface ConfigData {
cwd: string;
Expand All @@ -23,6 +24,15 @@ export async function loadConfig(): Promise<ConfigData | null> {
}
}
}
for (const file of CONFIG_DENO_FILE_NAMES) {
const p = path.join(dir, file);
if (existsSync(p)) {
return {
cwd: dir,
config: await parseDenoConfig(p),
};
}
}
dir = parent(dir);
}
return null;
Expand All @@ -44,3 +54,18 @@ async function parseConfig(
Deno.readTextFileSync(configPath),
) as ScriptsConfiguration;
}

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);
return config as ScriptsConfiguration;
}
6 changes: 6 additions & 0 deletions src/scripts_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ export interface DenoCliOptions {
*/
cert?: string;

/**
* The path to a deno configuration file,
* passed to deno cli's `--config` option.
*/
config?: string;

/**
* The path to an importmap json file,
* passed to deno cli's `--importmap` option.
Expand Down
16 changes: 16 additions & 0 deletions test/json/deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// Used to test that --config option is working properly
"lint": {
"rules": {
"exclude": ["no-explicit-any"]
}
},
/*
* Section below contains Velociraptor configuration
*/
"velociraptor": {
"scripts": {
"test": "echo '/* Works! */'"
}
}
}
3 changes: 3 additions & 0 deletions test/yaml/scripts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ scripts:
imap: importmap.json
forward:
cmd: echo deno test --lock=lock.json --cached-only
config:
cmd: deno lint test_config.ts
config: ../json/deno.jsonc
4 changes: 4 additions & 0 deletions test/yaml/test_config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//Linting this file should normally throw a no-explicit-any
//but this rule is disabled by deno.jsonc and should work
//if --config is correctly specified
type ExplicitAny = any;

0 comments on commit a4e1101

Please sign in to comment.