From e13416dd08785e73ec755fdfc011da6e62b7f018 Mon Sep 17 00:00:00 2001 From: medusalix Date: Tue, 19 Feb 2019 19:32:23 +0100 Subject: [PATCH 1/3] Fix glob for nested directories --- index.js | 3 ++- test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 334d0e1..8c2bb63 100644 --- a/index.js +++ b/index.js @@ -36,7 +36,8 @@ module.exports = (paths, options) => pTry(() => { throw error; } - }); + }) + .sort((a, b) => b.split(path.sep).length - a.split(path.sep).length); if (paths.length === 0) { return; diff --git a/test.js b/test.js index 569c7a4..ca88deb 100644 --- a/test.js +++ b/test.js @@ -168,3 +168,31 @@ test('non-existent files', async t => { t.false(fs.existsSync('fixture-enoent')); await t.notThrowsAsync(trash('fixture-enoent')); }); + +test('glob with nested directories', async t => { + const dir1 = 'foo'; + const file1 = path.join('foo', 'bar.txt'); + const file2 = path.join('foo', 'baz.txt'); + const dir2 = path.join('foo', 'bar'); + const dir3 = path.join('foo', 'baz'); + const file3 = path.join(dir1, 'foo.txt'); + const file4 = path.join(dir2, 'bar.txt'); + + fs.mkdirSync(dir1); + fs.mkdirSync(dir2); + fs.mkdirSync(dir3); + fs.writeFileSync(file1, ''); + fs.writeFileSync(file2, ''); + fs.writeFileSync(file3, ''); + fs.writeFileSync(file4, ''); + t.true(fs.existsSync(file1)); + t.true(fs.existsSync(file2)); + t.true(fs.existsSync(file3)); + t.true(fs.existsSync(file4)); + + await trash(`${dir1}/**`, {glob: true}); + + t.false(fs.existsSync(dir1)); + t.false(fs.existsSync(dir2)); + t.false(fs.existsSync(dir3)); +}); From e95b25bb6c5219c11838e04d9e79028b61a93a7e Mon Sep 17 00:00:00 2001 From: medusalix Date: Tue, 19 Feb 2019 21:32:09 +0100 Subject: [PATCH 2/3] Implement better solution --- index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 8c2bb63..c8bc3b0 100644 --- a/index.js +++ b/index.js @@ -25,6 +25,9 @@ module.exports = (paths, options) => pTry(() => { } paths = paths + .filter(filePath => !paths.some( + otherPath => filePath !== otherPath && path.dirname(filePath) === otherPath) + ) .map(filePath => path.resolve(filePath)) .filter(filePath => { try { @@ -36,8 +39,7 @@ module.exports = (paths, options) => pTry(() => { throw error; } - }) - .sort((a, b) => b.split(path.sep).length - a.split(path.sep).length); + }); if (paths.length === 0) { return; From 13a997abd3a27132dc484fa4b07888f48974981a Mon Sep 17 00:00:00 2001 From: medusalix Date: Sun, 31 Mar 2019 22:34:01 +0200 Subject: [PATCH 3/3] Use `is-path-inside` --- index.js | 31 ++++++++++++++++--------------- package.json | 1 + 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index c8bc3b0..979c284 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ const fs = require('fs'); const path = require('path'); const globby = require('globby'); const pTry = require('p-try'); +const isPathInside = require('is-path-inside'); const macos = require('./lib/macos'); const linux = require('./lib/linux'); const windows = require('./lib/windows'); @@ -24,22 +25,22 @@ module.exports = (paths, options) => pTry(() => { }); } - paths = paths - .filter(filePath => !paths.some( - otherPath => filePath !== otherPath && path.dirname(filePath) === otherPath) - ) - .map(filePath => path.resolve(filePath)) - .filter(filePath => { - try { - return fs.lstatSync(filePath); - } catch (error) { - if (error.code === 'ENOENT') { - return false; - } - - throw error; + paths = paths.map(filePath => path.resolve(filePath)); + paths = paths.filter(filePath => { + if (paths.some(otherPath => isPathInside(filePath, otherPath))) { + return false; + } + + try { + return fs.lstatSync(filePath); + } catch (error) { + if (error.code === 'ENOENT') { + return false; } - }); + + throw error; + } + }); if (paths.length === 0) { return; diff --git a/package.json b/package.json index f2d81e6..829536b 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "dependencies": { "escape-string-applescript": "^2.0.0", "globby": "^7.1.1", + "is-path-inside": "^2.0.0", "make-dir": "^1.3.0", "move-file": "^1.0.0", "p-map": "^2.0.0",