Skip to content

Commit

Permalink
Changed behaviour for symlinks finding
Browse files Browse the repository at this point in the history
  • Loading branch information
szwacz committed Jul 10, 2018
1 parent 36c5e8a commit 48b067b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 60 deletions.
4 changes: 0 additions & 4 deletions README.md
Expand Up @@ -266,7 +266,6 @@ Finds in directory specified by `path` all files fulfilling `searchOptions`. Ret
* `files` (default `true`) whether or not should search for files.
* `directories` (default `false`) whether or not should search for directories.
* `recursive` (default `true`) whether the whole directory tree should be searched recursively, or only one-level of given directory (excluding it's subdirectories).
* `symlinks` (default `false`) whether or not should report symlinks found.

**returns:**
`Array` of found paths.
Expand All @@ -293,9 +292,6 @@ jetpack.find('foo', { matching: './*.txt' });
// (skips looking in subdirectories)
jetpack.find('foo', { matching: '*.txt', recursive: false });

// finds all dirs (including symlinked dirs) beginning with `plugin-*` in `node_modules`
jetpack.find('node_modules', { matching: 'plugin-*', recursive: false, files: false, directories: true, symlinks: true });

// Path parameter might be omitted and CWD is used as path in that case.
const myStuffDir = jetpack.cwd('my-stuff');
myStuffDir.find({ matching: ['*.md'] });
Expand Down
22 changes: 9 additions & 13 deletions lib/find.js
Expand Up @@ -13,8 +13,7 @@ const validateInput = (methodName, path, options) => {
matching: ["string", "array of string"],
files: ["boolean"],
directories: ["boolean"],
recursive: ["boolean"],
symlinks: ["boolean"]
recursive: ["boolean"]
});
};

Expand All @@ -30,9 +29,6 @@ const normalizeOptions = options => {
if (opts.recursive === undefined) {
opts.recursive = true;
}
if (opts.symlinks === undefined) {
opts.symlinks = false;
}
return opts;
};

Expand Down Expand Up @@ -74,15 +70,15 @@ const findSync = (path, options) => {
{
maxLevelsDeep,
inspectOptions: {
absolutePath: true
absolutePath: true,
symlinks: "follow"
}
},
(itemPath, item) => {
if (itemPath !== path && matchesAnyOfGlobs(itemPath)) {
if (
(item.type === "file" && options.files === true) ||
(item.type === "dir" && options.directories === true) ||
(item.type === "symlink" && options.symlinks === true)
(item.type === "dir" && options.directories === true)
) {
foundInspectObjects.push(item);
}
Expand All @@ -94,7 +90,7 @@ const findSync = (path, options) => {
};

const findSyncInit = (path, options) => {
const entryPointInspect = inspect.sync(path);
const entryPointInspect = inspect.sync(path, { symlinks: "follow" });
if (entryPointInspect === undefined) {
throw generatePathDoesntExistError(path);
} else if (entryPointInspect.type !== "dir") {
Expand Down Expand Up @@ -122,7 +118,8 @@ const findAsync = (path, options) => {
.stream(path, {
maxLevelsDeep,
inspectOptions: {
absolutePath: true
absolutePath: true,
symlinks: "follow"
}
})
.on("readable", () => {
Expand All @@ -131,8 +128,7 @@ const findAsync = (path, options) => {
const item = data.item;
if (
(item.type === "file" && options.files === true) ||
(item.type === "dir" && options.directories === true) ||
(item.type === "symlink" && options.symlinks === true)
(item.type === "dir" && options.directories === true)
) {
foundInspectObjects.push(item);
}
Expand All @@ -146,7 +142,7 @@ const findAsync = (path, options) => {
};

const findAsyncInit = (path, options) => {
return inspect.async(path).then(entryPointInspect => {
return inspect.async(path, { symlinks: "follow" }).then(entryPointInspect => {
if (entryPointInspect === undefined) {
throw generatePathDoesntExistError(path);
} else if (entryPointInspect.type !== "dir") {
Expand Down
72 changes: 29 additions & 43 deletions spec/find.spec.js
Expand Up @@ -262,16 +262,16 @@ describe("find", () => {
});
});

describe("doesn't look for symlinks by default", () => {
describe("treats symlinks like real files", () => {
const preparations = () => {
fse.outputFileSync("file", "abc");
fse.mkdirsSync("dir");
jetpack.symlink("file", "symfile");
fse.outputFileSync("file", "abc");
jetpack.symlink("dir", "symdir");
jetpack.symlink("file", "symfile");
};

const expectations = found => {
expect(found).to.eql(["file"]);
expect(found).to.eql(["file", "symfile"]);
};

it("sync", () => {
Expand All @@ -288,6 +288,31 @@ describe("find", () => {
});
});

describe("follows to symlinked directories", () => {
const preparations = () => {
fse.outputFileSync("dir1/dir2/file.txt", "abc");
jetpack.symlink("../dir1", "foo/symlink_to_dir1");
expect(jetpack.read("foo/symlink_to_dir1/dir2/file.txt")).to.eql("abc");
};

const expectations = found => {
expect(found).to.eql(["foo/symlink_to_dir1/dir2/file.txt"]);
};

it("sync", () => {
preparations();
expectations(jetpack.find("foo", { matching: "file*" }));
});

it("async", done => {
preparations();
jetpack.findAsync("foo", { matching: "file*" }).then(found => {
expectations(found);
done();
});
});
});

describe("can look for files and directories", () => {
const preparations = () => {
fse.outputFileSync("a/b/foo1", "abc");
Expand Down Expand Up @@ -434,32 +459,6 @@ describe("find", () => {
});
});

describe("can look for symlinks", () => {
const preparations = () => {
fse.outputFileSync("file", "abc");
fse.mkdirsSync("dir");
jetpack.symlink("file", "symfile");
jetpack.symlink("dir", "symdir");
};

const expectations = found => {
expect(found).to.eql(["file", "symdir", "symfile"]);
};

it("sync", () => {
preparations();
expectations(jetpack.find({ matching: "*", symlinks: true }));
});

it("async", done => {
preparations();
jetpack.findAsync({ matching: "*", symlinks: true }).then(found => {
expectations(found);
done();
});
});
});

describe("throws if path doesn't exist", () => {
const expectations = err => {
expect(err.code).to.equal("ENOENT");
Expand Down Expand Up @@ -651,19 +650,6 @@ describe("find", () => {
});
});
});
describe('"symlinks" argument', () => {
tests.forEach(test => {
it(test.type, () => {
expect(() => {
test.method("abc", { symlinks: 1 });
}).to.throw(
`Argument "options.symlinks" passed to ${
test.methodName
}([path], options) must be a boolean. Received number`
);
});
});
});
});
});
});

0 comments on commit 48b067b

Please sign in to comment.