Skip to content

Commit

Permalink
Don't throw when specifying a non-existing cwd directory (#125)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
popomore and sindresorhus committed Jul 5, 2019
1 parent 5b0834a commit a226f5d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
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);
} 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
Original file line number Diff line number Diff line change
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);
});

0 comments on commit a226f5d

Please sign in to comment.