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

feat(cli): expand relative paths to absolute paths

API users are unaffected, if you need absolute paths you must resolve them
yourself.

fixes #131
parent c860af66
Pipeline #360374204 passed with stages
in 11 minutes and 7 seconds
......@@ -32,7 +32,10 @@ export class CLI {
}
/**
* Returns list of files matching patterns and are not ignored.
* Returns list of files matching patterns and are not ignored. Filenames will
* have absolute paths.
*
* @public
*/
public expandFiles(patterns: string[], options: ExpandOptions = {}): string[] {
return expandFiles(patterns, options).filter((filename) => !this.isIgnored(filename));
......
......@@ -15,7 +15,12 @@ declare module "glob" {
function resetMock(): void;
}
function resolve(cwd: string, filePath: string): string {
return path.normalize(path.join(cwd, filePath));
}
let cli: CLI;
const cwd = path.join(__dirname, "../..");
beforeEach(() => {
fs.mockReset();
......@@ -32,56 +37,61 @@ beforeEach(() => {
describe("expandFiles()", () => {
it("should expand globs", () => {
expect.assertions(1);
expect(cli.expandFiles(["foo.html", "bar/**/*.html"])).toEqual([
path.normalize("foo.html"),
path.normalize("bar/barney.html"),
path.normalize("bar/fred.html"),
expect(cli.expandFiles(["foo.html", "bar/**/*.html"], { cwd })).toEqual([
resolve(cwd, "foo.html"),
resolve(cwd, "bar/barney.html"),
resolve(cwd, "bar/fred.html"),
]);
});
it("should expand directories (default extensions)", () => {
expect.assertions(1);
expect(cli.expandFiles(["bar"])).toEqual([
path.normalize("bar/barney.html"),
path.normalize("bar/fred.html"),
expect(cli.expandFiles(["bar"], { cwd })).toEqual([
resolve(cwd, "bar/barney.html"),
resolve(cwd, "bar/fred.html"),
]);
});
it("should expand directories (explicit extensions)", () => {
expect.assertions(1);
expect(cli.expandFiles(["bar"], { extensions: ["js", "json"] })).toEqual([
path.normalize("bar/barney.js"),
path.normalize("bar/fred.json"),
resolve(cwd, "bar/barney.js"),
resolve(cwd, "bar/fred.json"),
]);
});
it("should expand directories (no extensions => all files)", () => {
expect.assertions(1);
expect(cli.expandFiles(["bar"], { extensions: [] })).toEqual([
path.normalize("bar/barney.html"),
path.normalize("bar/barney.js"),
path.normalize("bar/fred.html"),
path.normalize("bar/fred.json"),
expect(cli.expandFiles(["bar"], { extensions: [], cwd })).toEqual([
resolve(cwd, "bar/barney.html"),
resolve(cwd, "bar/barney.js"),
resolve(cwd, "bar/fred.html"),
resolve(cwd, "bar/fred.json"),
]);
});
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")),
const patterns = [path.join(cwd, "foo.html"), path.join(cwd, "bar")];
expect(cli.expandFiles(patterns, { cwd })).toEqual([
resolve(cwd, "foo.html"),
resolve(cwd, "bar/barney.html"),
resolve(cwd, "bar/fred.html"),
]);
});
it("should remove duplicates", () => {
expect.assertions(1);
expect(cli.expandFiles(["foo.html", "foo.html"])).toEqual(["foo.html"]);
expect(cli.expandFiles(["foo.html", "foo.html"], { cwd })).toEqual([resolve(cwd, "foo.html")]);
});
it("should fallback on process.cwd()", () => {
expect.assertions(1);
expect(cli.expandFiles(["foo.html"])).toEqual([resolve(process.cwd(), "foo.html")]);
});
it("should replace - placeholder", () => {
expect.assertions(1);
expect(cli.expandFiles(["-"])).toEqual(["/dev/stdin"]);
expect(cli.expandFiles(["-"], { cwd })).toEqual(["/dev/stdin"]);
});
});
......@@ -22,11 +22,11 @@ function isDirectory(filename: string): boolean {
return st.isDirectory();
}
function getFullPath(cwd: string, filename: string): string {
function join(stem: string, filename: string): string {
if (path.isAbsolute(filename)) {
return filename;
return path.normalize(filename);
} else {
return path.join(cwd, filename);
return path.normalize(path.join(stem, filename));
}
}
......@@ -58,14 +58,14 @@ 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 = getFullPath(cwd, filename);
const fullpath = join(cwd, filename);
if (isDirectory(fullpath)) {
const dir = expandFiles([directoryPattern(extensions)], { ...options, cwd: fullpath });
result = result.concat(dir.map((cur) => path.join(filename, cur)));
result = result.concat(dir.map((cur) => join(filename, cur)));
continue;
}
result.push(filename);
result.push(fullpath);
}
return result;
......
......@@ -60,12 +60,13 @@ describe("integration tests", () => {
it("expandFiles() should only return files not ignored", () => {
expect.assertions(1);
const cwd = path.join(__dirname, "../..");
const cli = new CLI();
const files = cli.expandFiles(["test-files/ignored"], { extensions: ["html", "vue"] });
const files = cli.expandFiles(["test-files/ignored"], { extensions: ["html", "vue"], cwd });
expect(files).toEqual([
"test-files/ignored/bar.html",
"test-files/ignored/included/file.html",
"test-files/ignored/subdir/foo.vue",
path.join(cwd, "test-files/ignored/bar.html"),
path.join(cwd, "test-files/ignored/included/file.html"),
path.join(cwd, "test-files/ignored/subdir/foo.vue"),
]);
});
......
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