Commit f152a128 authored by David Sveningsson's avatar David Sveningsson

feat(cli): handle passing directories

parent 420dc88a
import fs from "fs";
import glob from "glob";
import path from "path";
interface ExpandOptions {
cwd?: string;
}
function isDirectory(filename: string): boolean {
const st = fs.statSync(filename);
return st.isDirectory();
}
/**
* Takes a number of file patterns (globs) and returns array of expanded
* filenames.
......@@ -14,12 +21,28 @@ export function expandFiles(
): string[] {
const cwd = options.cwd || process.cwd();
const files = patterns.reduce((files: string[], pattern: string) => {
const files = patterns.reduce((result: string[], pattern: string) => {
/* process - as standard input */
if (pattern === "-") {
pattern = "/dev/stdin";
}
return files.concat(glob.sync(pattern, { cwd }));
for (const filename of glob.sync(pattern, { cwd })) {
/* if file is a directory recursively expand files from it */
const fullpath = path.join(cwd, filename);
if (isDirectory(fullpath)) {
const dir = expandFiles(
["**"],
Object.assign({}, options, { cwd: fullpath })
);
result = result.concat(dir.map(cur => path.join(filename, cur)));
continue;
}
result.push(filename);
}
return result;
}, []);
/* only return unique matches */
......
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