Commit 2bdd75fb authored by David Sveningsson's avatar David Sveningsson

feat(cli): allow specifying extensions

parent f152a128
......@@ -2,8 +2,19 @@ import fs from "fs";
import glob from "glob";
import path from "path";
const DEFAULT_EXTENSIONS = ["html"];
interface ExpandOptions {
/**
* Working directory. Defaults to `process.cwd()`.
*/
cwd?: string;
/**
* List of extensions to search for when expanding directories. Extensions
* should be passed without leading dot, e.g. "html" instead of ".html".
*/
extensions?: string[];
}
function isDirectory(filename: string): boolean {
......@@ -11,6 +22,17 @@ function isDirectory(filename: string): boolean {
return st.isDirectory();
}
function directoryPattern(extensions: string[]): string {
switch (extensions.length) {
case 0:
return "**";
case 1:
return `**/*.${extensions[0]}`;
default:
return `**/*.{${extensions.join(",")}}`;
}
}
/**
* Takes a number of file patterns (globs) and returns array of expanded
* filenames.
......@@ -20,6 +42,7 @@ export function expandFiles(
options: ExpandOptions = {}
): string[] {
const cwd = options.cwd || process.cwd();
const extensions = options.extensions || DEFAULT_EXTENSIONS;
const files = patterns.reduce((result: string[], pattern: string) => {
/* process - as standard input */
......@@ -32,7 +55,7 @@ export function expandFiles(
const fullpath = path.join(cwd, filename);
if (isDirectory(fullpath)) {
const dir = expandFiles(
["**"],
[directoryPattern(extensions)],
Object.assign({}, options, { cwd: fullpath })
);
result = result.concat(dir.map(cur => path.join(filename, cur)));
......
......@@ -122,6 +122,7 @@ const argv: minimist.ParsedArgs = minimist(process.argv.slice(2), {
string: [
"c",
"config",
"ext",
"f",
"formatter",
"max-warnings",
......@@ -144,6 +145,7 @@ function showUsage(): void {
Usage: html-validate [OPTIONS] [FILENAME..] [DIR..]
Common options:
--ext=STRING specify file extensions (commaseparated).
-f, --formatter=FORMATTER specify the formatter to use.
--max-warnings=INT number of warnings to trigger nonzero exit code
--rule=RULE:SEVERITY set additional rule, use comma separator for
......@@ -193,7 +195,12 @@ if (isNaN(maxWarnings)) {
process.exit(1);
}
const files = expandFiles(argv._);
/* parse extensions (used when expanding directories) */
const extensions = (argv.ext || "html").split(",").map((cur: string) => {
return cur[0] === "." ? cur.slice(1) : cur;
});
const files = expandFiles(argv._, { extensions });
if (files.length === 0) {
console.error("No files matching patterns", argv._);
process.exit(1);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment