Commit edab9cfb authored by David Sveningsson's avatar David Sveningsson
Browse files

feat(cli): expose expandFiles function

parent 94ce2a85
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -6165,7 +6165,7 @@
      "dependencies": {
        "canonical-path": {
          "version": "0.0.2",
          "resolved": "http://registry.npmjs.org/canonical-path/-/canonical-path-0.0.2.tgz",
          "resolved": "https://registry.npmjs.org/canonical-path/-/canonical-path-0.0.2.tgz",
          "integrity": "sha1-4x65N6jJPuKgHfGDl5RyGQKHRXQ=",
          "dev": true
        }
+18 −0
Original line number Diff line number Diff line
import glob from "glob";

/**
 * Takes a number of file patterns (globs) and returns array of expanded
 * filenames.
 */
export function expandFiles(patterns: string[]): string[] {
	const files = patterns.reduce((files: string[], pattern: string) => {
		/* process - as standard input */
		if (pattern === "-") {
			pattern = "/dev/stdin";
		}
		return files.concat(glob.sync(pattern));
	}, []);

	/* only return unique matches */
	return Array.from(new Set(files));
}
+5 −13
Original line number Diff line number Diff line
@@ -5,13 +5,13 @@ import { TokenDump } from "../engine";
import { UserError } from "../error/user-error";
import HtmlValidate from "../htmlvalidate";
import { Report, Reporter, Result } from "../reporter";
import { expandFiles } from "./expand-files";
import { getFormatter } from "./formatter";
import { eventFormatter } from "./json";

const pkg = require("../../package.json");

import chalk from "chalk";
import glob from "glob";
import minimist from "minimist";

enum Mode {
@@ -193,23 +193,15 @@ if (isNaN(maxWarnings)) {
	process.exit(1);
}

const files = argv._.reduce((files: string[], pattern: string) => {
	/* process - as standard input */
	if (pattern === "-") {
		pattern = "/dev/stdin";
	}
	return files.concat(glob.sync(pattern));
}, []);
const unique = Array.from(new Set(files));

if (unique.length === 0) {
const files = expandFiles(argv._);
if (files.length === 0) {
	console.error("No files matching patterns", argv._);
	process.exit(1);
}

try {
	if (mode === Mode.LINT) {
		const result = lint(unique);
		const result = lint(files);

		/* rename stdin if an explicit filename was passed */
		if (argv["stdin-filename"]) {
@@ -231,7 +223,7 @@ try {
		const json = JSON.stringify(config.get(), null, 2);
		console.log(json);
	} else {
		const output = dump(unique, mode);
		const output = dump(files, mode);
		console.log(output);
		process.exit(0);
	}