From 9aa7c5050860489f290f8607453a40e395a88a98 Mon Sep 17 00:00:00 2001 From: popomore Date: Sat, 22 Jun 2019 03:57:11 +0800 Subject: [PATCH 1/2] Fix don't throw when specifying a non-existing cwd directory --- index.js | 13 +++++++++++-- test.js | 10 ++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 029a19e..881614b 100644 --- a/index.js +++ b/index.js @@ -19,8 +19,17 @@ const assertPatternsInput = patterns => { }; const checkCwdOption = options => { - if (options && options.cwd && !fs.statSync(options.cwd).isDirectory()) { - throw new Error('The `cwd` option must be a path to a directory'); + if (options && options.cwd) { + 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'); + } } }; diff --git a/test.js b/test.js index ffe776b..165d46a 100644 --- a/test.js +++ b/test.js @@ -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); +}); From 999bdf2bf10a7d7280a2ca65446bd8b40c5ce048 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 5 Jul 2019 13:24:05 +0700 Subject: [PATCH 2/2] Update index.js --- index.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 881614b..1f8d9aa 100644 --- a/index.js +++ b/index.js @@ -18,18 +18,20 @@ const assertPatternsInput = patterns => { } }; -const checkCwdOption = options => { - if (options && options.cwd) { - let stat; - try { - stat = fs.statSync(options.cwd); - } catch (_) { - return; - } +const checkCwdOption = (options = {}) => { + if (!options.cwd) { + return; + } - if (!stat.isDirectory()) { - throw new Error('The `cwd` option must be a path to a directory'); - } + 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'); } };