Skip to content

Commit

Permalink
feat: Added fmt and test flags (#27)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Renamed `denonrc.ts` to `denon_config.ts`
  • Loading branch information
eliassjogreen committed Apr 23, 2020
1 parent 6fdbd44 commit c5d19c3
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 18 deletions.
33 changes: 28 additions & 5 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ export interface Args {
runnerFlags: string[];
skip?: string[];
watch?: string[];
fmt?: boolean;
test?: boolean;
}

export function help() {
console.log(`
Usage:
denon [OPTIONS] [DENO_ARGS] [SCRIPT] [-- <SCRIPT_ARGS>]
OPTIONS:
-c, --config <file> A path to a config file, defaults to [default: .denonrc | .denonrc.json]
-c, --config <file> A path to a config file, defaults to [default: .denon | denon.json | .denonrc | .denonrc.json]
-d, --debug Debugging mode for more verbose logging
-e, --extensions List of extensions to look for separated by commas
-f, --fullscreen Clears the screen each reload
Expand All @@ -34,7 +36,13 @@ export function help() {
-q, --quiet Turns off all logging
-s, --skip <glob> Glob pattern for ignoring specific files or directories
-w, --watch List of paths to watch separated by commas
--fmt Adds a deno fmt executor
--test Adds a deno test executor
COMMANDS:
fmt Alias for flag --fmt
test Alias for flag --test
DENO_ARGS: Arguments passed to Deno to run SCRIPT (like permisssions)
`);
}
Expand Down Expand Up @@ -68,9 +76,12 @@ export function parseArgs(args: string[]): Args {
let deno_args: string[] = [];
const files: string[] = [];

let fmt = false;
let test = false;

const flags = parse(args, {
string: ["config", "extensions", "interval", "match", "skip", "watch"],
boolean: ["debug", "fullscreen", "help", "quiet"],
boolean: ["debug", "fullscreen", "help", "quiet", "fmt", "test"],
alias: {
config: "c",
debug: "d",
Expand All @@ -86,7 +97,17 @@ export function parseArgs(args: string[]): Args {
"--": true,
unknown: (arg: string, k?: string, v?: unknown) => {
if (k == null && v == null) {
files.push(arg);
switch (arg) {
case "fmt":
fmt = true;
break;
case "test":
test = true;
break;
default:
files.push(arg);
break;
}
return false;
}
deno_args.push(arg);
Expand Down Expand Up @@ -117,6 +138,8 @@ export function parseArgs(args: string[]): Args {
files,
runnerFlags: flags["--"],
deno_args,
fmt: fmt || flags.fmt,
test: test || flags.test,
};
}

Expand Down
17 changes: 15 additions & 2 deletions cli_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ test(function parseArgsEmpty() {
runnerFlags: [],
skip: undefined,
watch: undefined,
fmt: false,
test: false,
};
assertEquals(parseArgs([]), expected);
});
Expand All @@ -37,6 +39,8 @@ test(function parseArgsOnlyFile() {
runnerFlags: [],
skip: undefined,
watch: undefined,
fmt: false,
test: false,
};
assertEquals(parseArgs(args), expected);
});
Expand All @@ -57,6 +61,8 @@ test(function parseArgsWithoutFile() {
runnerFlags: [],
skip: undefined,
watch: undefined,
fmt: false,
test: false,
};
assertEquals(parseArgs(args), expected);
});
Expand All @@ -77,13 +83,16 @@ test(function parseArgsDoubleDashWithoutFile() {
runnerFlags: ["--foo", "bar"],
skip: undefined,
watch: undefined,
fmt: false,
test: false,
};
assertEquals(parseArgs(args), expected);
});

test(function parseArgsAll() {
let args = "--config denon.json -dfqhe js,ts -i 500 -m foo/** -s bar/**"
.split(" ");
let args =
"--config denon.json -dfqhe js,ts -i 500 -m foo/** -s bar/** --fmt --test"
.split(" ");
args = args.concat(
"-w lib/** --importmap=import_map.json -A mod.ts -- --allow-net --port 500"
.split(" "),
Expand All @@ -102,6 +111,8 @@ test(function parseArgsAll() {
runnerFlags: ["--allow-net", "--port", "500"],
skip: ["bar/**"],
watch: ["lib/**"],
fmt: true,
test: true,
};
assertEquals(parseArgs(args), expected);
});
Expand All @@ -125,6 +136,8 @@ test(function parseSameArgsMultipleTimes() {
runnerFlags: [],
skip: ["foo", "bar", "bla"],
watch: ["foo", "bar", "bla"],
fmt: false,
test: false,
};
assertEquals(parseArgs(args), expected);
});
Expand Down
29 changes: 24 additions & 5 deletions denon.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { dirname, exists, extname, resolve } from "./deps.ts";
import { parseArgs, help, applyIfDefined } from "./cli.ts";
import { DenonConfig, DenonConfigDefaults, readConfig } from "./denonrc.ts";
import {
DenonConfig,
DenonConfigDefaults,
readConfig,
} from "./denon_config.ts";
import { debug, log, fail, setConfig } from "./log.ts";
import Watcher from "./watcher.ts";

Expand All @@ -12,18 +16,19 @@ if (import.meta.main) {

if (flags.debug) {
config.debug = flags.debug;
debug("Debug enabled!");
}

if (flags.config) {
debug(`Reading config from ${flags.config}`);
config = await readConfig(flags.config);
} else {
debug(`Reading config from .denonrc | .denonrc.json`);
debug(
`Reading config from .denon | .denon.json | .denonrc | .denonrc.json`,
);
config = await readConfig();
}

setConfig(config);

debug(`Args: ${Deno.args}`);
debug(`Flags: ${JSON.stringify(flags)}`);

Expand All @@ -45,12 +50,16 @@ if (import.meta.main) {
"quiet",
"skip",
"watch",
"fmt",
"test",
],
);

debug(`Config: ${JSON.stringify(config)}`);

if (config.files.length < 1 && flags.files.length < 1) {
if (config.fmt || config.test) {
config.watch.push(Deno.cwd());
} else if (config.files.length < 1 && flags.files.length < 1) {
fail(
"Could not start denon because no file was provided, use -h for help",
);
Expand Down Expand Up @@ -119,6 +128,16 @@ if (import.meta.main) {
};
};

if (config.fmt) {
debug("Added deno fmt executor");
executors.push(execute("deno", "fmt"));
}

if (config.test) {
debug("Added deno test executor");
executors.push(execute("deno", "test"));
}

for (const file of config.files) {
const extension = extname(file);
const cmds = config.execute[extension] as string[] | undefined;
Expand Down
16 changes: 11 additions & 5 deletions denonrc.ts → denon_config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { exists, readFileStr } from "./deps.ts";
import { fail, debug } from "./log.ts";
import { fail } from "./log.ts";

export interface DenonConfig {
files: string[];
Expand All @@ -13,6 +13,8 @@ export interface DenonConfig {
watch: string[];
deno_args: string[];
execute: { [extension: string]: string[] };
fmt: boolean;
test: boolean;
}

export const DenonConfigDefaults: DenonConfig = {
Expand All @@ -30,6 +32,8 @@ export const DenonConfigDefaults: DenonConfig = {
".js": ["deno", "run"],
".ts": ["deno", "run"],
},
fmt: false,
test: false,
};

export async function readConfig(file?: string): Promise<DenonConfig> {
Expand All @@ -38,11 +42,13 @@ export async function readConfig(file?: string): Promise<DenonConfig> {
}

if (!file) {
if (await exists(".denonrc")) {
if (await exists(".denon")) {
file = ".denon";
} else if (await exists(".denon.json")) {
file = ".denon.json";
} else if (await exists(".denonrc")) {
file = ".denonrc";
}

if (await exists(".denonrc.json")) {
} else if (await exists(".denonrc.json")) {
file = ".denonrc.json";
}
}
Expand Down
2 changes: 1 addition & 1 deletion log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
red,
setColorEnabled,
} from "./deps.ts";
import { DenonConfig } from "./denonrc.ts";
import { DenonConfig } from "./denon_config.ts";

setColorEnabled(true);

Expand Down

0 comments on commit c5d19c3

Please sign in to comment.