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

fix(cli): handle absolute paths as input filenames

refs #131
parent f3226b2c
......@@ -65,6 +65,16 @@ describe("expandFiles()", () => {
]);
});
it("should handle absolute paths", () => {
expect.assertions(1);
const patterns = [path.join(process.cwd(), "foo.html"), path.join(process.cwd(), "bar")];
expect(cli.expandFiles(patterns)).toEqual([
path.join(process.cwd(), path.normalize("foo.html")),
path.join(process.cwd(), path.normalize("bar/barney.html")),
path.join(process.cwd(), path.normalize("bar/fred.html")),
]);
});
it("should remove duplicates", () => {
expect.assertions(1);
expect(cli.expandFiles(["foo.html", "foo.html"])).toEqual(["foo.html"]);
......
......@@ -22,6 +22,14 @@ function isDirectory(filename: string): boolean {
return st.isDirectory();
}
function getFullPath(cwd: string, filename: string): string {
if (path.isAbsolute(filename)) {
return filename;
} else {
return path.join(cwd, filename);
}
}
function directoryPattern(extensions: string[]): string {
switch (extensions.length) {
case 0:
......@@ -50,7 +58,7 @@ export function expandFiles(patterns: string[], options: ExpandOptions): string[
for (const filename of glob.sync(pattern, { cwd })) {
/* if file is a directory recursively expand files from it */
const fullpath = path.join(cwd, filename);
const fullpath = getFullPath(cwd, filename);
if (isDirectory(fullpath)) {
const dir = expandFiles([directoryPattern(extensions)], { ...options, cwd: fullpath });
result = result.concat(dir.map((cur) => path.join(filename, cur)));
......
Supports Markdown
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