From a226f5dbf42e1c8b12bbf8d9eecc809371a6d9fd Mon Sep 17 00:00:00 2001 From: Haoliang Gao Date: Fri, 5 Jul 2019 14:24:32 +0800 Subject: [PATCH] Don't throw when specifying a non-existing `cwd` directory (#125) Co-authored-by: Sindre Sorhus --- index.js | 15 +++++++++++++-- test.js | 10 ++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 904338d..764441c 100644 --- a/index.js +++ b/index.js @@ -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'); } }; 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); +});