Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't throw when specifying a non-existing cwd directory #125

Merged
merged 2 commits into from Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions index.js
Expand Up @@ -18,8 +18,19 @@ const assertPatternsInput = patterns => {
}
};

const checkCwdOption = options => {
if (options && options.cwd && !fs.statSync(options.cwd).isDirectory()) {
const checkCwdOption = (options = {}) => {
if (!options.cwd) {
return;
}

let stat;
try {
stat = fs.statSync(options.cwd);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to do this synchronously?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want a separate async version of this check. I might have been too lazy to do that initially or just forgot. PR welcome :)

Copy link
Contributor

@kevva kevva Jul 5, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we'll need it since we use globby in a lot of modules and this might result in a small decrease in performance.

} catch (_) {
return;
}

if (!stat.isDirectory()) {
throw new Error('The `cwd` option must be a path to a directory');
}
};
Expand Down
10 changes: 10 additions & 0 deletions test.js
Expand Up @@ -362,3 +362,13 @@ test('throws when specifying a file as cwd - stream', t => {
globby.stream('*', {cwd: isFile});
}, 'The `cwd` option must be a path to a directory');
});

test('don\'t throw when specifying a non-existing cwd directory - async', async t => {
const actual = await globby('.', {cwd: '/unknown'});
t.is(actual.length, 0);
});

test('don\'t throw when specifying a non-existing cwd directory - sync', t => {
const actual = globby.sync('.', {cwd: '/unknown'});
t.is(actual.length, 0);
});