Skip to content

Commit a226f5d

Browse files
Don't throw when specifying a non-existing cwd directory (#125)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 5b0834a commit a226f5d

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,19 @@ const assertPatternsInput = patterns => {
1818
}
1919
};
2020

21-
const checkCwdOption = options => {
22-
if (options && options.cwd && !fs.statSync(options.cwd).isDirectory()) {
21+
const checkCwdOption = (options = {}) => {
22+
if (!options.cwd) {
23+
return;
24+
}
25+
26+
let stat;
27+
try {
28+
stat = fs.statSync(options.cwd);
29+
} catch (_) {
30+
return;
31+
}
32+
33+
if (!stat.isDirectory()) {
2334
throw new Error('The `cwd` option must be a path to a directory');
2435
}
2536
};

test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,13 @@ test('throws when specifying a file as cwd - stream', t => {
362362
globby.stream('*', {cwd: isFile});
363363
}, 'The `cwd` option must be a path to a directory');
364364
});
365+
366+
test('don\'t throw when specifying a non-existing cwd directory - async', async t => {
367+
const actual = await globby('.', {cwd: '/unknown'});
368+
t.is(actual.length, 0);
369+
});
370+
371+
test('don\'t throw when specifying a non-existing cwd directory - sync', t => {
372+
const actual = globby.sync('.', {cwd: '/unknown'});
373+
t.is(actual.length, 0);
374+
});

0 commit comments

Comments
 (0)