From e377a77baaad7a24a1e86f8be436804e4417037c Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Fri, 12 Jul 2019 13:55:23 -0700 Subject: [PATCH 1/6] Allow windows non-glob patterns with \ --- index.js | 20 ++++++++++++++++++ package.json | 4 +++- test.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 9d8251c..fbd8b2b 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,8 @@ const {promisify} = require('util'); const path = require('path'); const globby = require('globby'); +const isGlob = require('is-glob'); +const slash = require('slash'); const isPathCwd = require('is-path-cwd'); const isPathInside = require('is-path-inside'); const rimraf = require('rimraf'); @@ -19,6 +21,20 @@ function safeCheck(file, cwd) { } } +function normalizePatterns(patterns) { + patterns = Array.isArray(patterns) ? patterns : [patterns]; + + patterns = patterns.map(pattern => { + if (process.platform === 'win32' && isGlob(pattern) === false) { + return slash(pattern); + } + + return pattern; + }); + + return patterns; +} + module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) => { options = { expandDirectories: false, @@ -28,6 +44,8 @@ module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), ...option ...options }; + patterns = normalizePatterns(patterns); + const files = (await globby(patterns, options)) .sort((a, b) => b.localeCompare(a)); @@ -57,6 +75,8 @@ module.exports.sync = (patterns, {force, dryRun, cwd = process.cwd(), ...options ...options }; + patterns = normalizePatterns(patterns); + const files = globby.sync(patterns, options) .sort((a, b) => b.localeCompare(a)); diff --git a/package.json b/package.json index 054bfb2..9bf9e6c 100644 --- a/package.json +++ b/package.json @@ -45,10 +45,12 @@ ], "dependencies": { "globby": "^10.0.0", + "is-glob": "^4.0.1", "is-path-cwd": "^2.0.0", "is-path-inside": "^3.0.1", "p-map": "^2.0.0", - "rimraf": "^2.6.3" + "rimraf": "^2.6.3", + "slash": "^3.0.0" }, "devDependencies": { "ava": "^2.1.0", diff --git a/test.js b/test.js index 8b8632a..99045ae 100644 --- a/test.js +++ b/test.js @@ -315,3 +315,61 @@ test('cannot delete files inside process.cwd when outside cwd without force: tru exists(t, ['1.tmp', '2.tmp', '3.tmp', '4.tmp', '.dot.tmp']); process.chdir(processCwd); }); + +test('windows can pass absolute paths with "\\" - async', async t => { + const processPlatform = process.platform; + Object.defineProperty(process, 'platform', {value: 'win32'}); + + const filePath = path.resolve(t.context.tmp, '1.tmp'); + const win32FilePath = path.win32.normalize(filePath); + + const removeFiles = await del([win32FilePath], {cwd: t.context.tmp, dryRun: true}); + + t.deepEqual(removeFiles, [filePath]); + + Object.defineProperty(process, 'platform', {value: processPlatform}); +}); + +test('windows can pass absolute paths with "\\" - sync', t => { + const processPlatform = process.platform; + Object.defineProperty(process, 'platform', {value: 'win32'}); + + const filePath = path.resolve(t.context.tmp, '1.tmp'); + const win32FilePath = path.win32.normalize(filePath); + + const removeFiles = del.sync([win32FilePath], {cwd: t.context.tmp, dryRun: true}); + + t.deepEqual(removeFiles, [filePath]); + + Object.defineProperty(process, 'platform', {value: processPlatform}); +}); + +test('windows can pass relative paths with "\\" - async', async t => { + const nestedFile = path.resolve(t.context.tmp, 'a/b/c/nested.js'); + makeDir.sync(nestedFile); + + const processPlatform = process.platform; + Object.defineProperty(process, 'platform', {value: 'win32'}); + + const win32FilePath = path.win32.normalize(nestedFile); + const removeFiles = await del([win32FilePath], {cwd: t.context.tmp, dryRun: true}); + + t.deepEqual(removeFiles, [nestedFile]); + + Object.defineProperty(process, 'platform', {value: processPlatform}); +}); + +test('windows can pass relative paths with "\\" - sync', t => { + const nestedFile = path.resolve(t.context.tmp, 'a/b/c/nested.js'); + makeDir.sync(nestedFile); + + const processPlatform = process.platform; + Object.defineProperty(process, 'platform', {value: 'win32'}); + + const win32FilePath = path.win32.normalize(nestedFile); + const removeFiles = del.sync([win32FilePath], {cwd: t.context.tmp, dryRun: true}); + + t.deepEqual(removeFiles, [nestedFile]); + + Object.defineProperty(process, 'platform', {value: processPlatform}); +}); From d7d9dd22ebbc1c60b2bfd55c88c751b1ef871163 Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Fri, 12 Jul 2019 14:18:09 -0700 Subject: [PATCH 2/6] test directly on windows instead of mocking windows --- .travis.yml | 4 ++++ test.js | 32 ++++---------------------------- 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/.travis.yml b/.travis.yml index f98fed0..38ca099 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,3 +3,7 @@ node_js: - '12' - '10' - '8' +os: + - windows + - linux + - osx diff --git a/test.js b/test.js index 99045ae..570d32c 100644 --- a/test.js +++ b/test.js @@ -317,59 +317,35 @@ test('cannot delete files inside process.cwd when outside cwd without force: tru }); test('windows can pass absolute paths with "\\" - async', async t => { - const processPlatform = process.platform; - Object.defineProperty(process, 'platform', {value: 'win32'}); - const filePath = path.resolve(t.context.tmp, '1.tmp'); - const win32FilePath = path.win32.normalize(filePath); - const removeFiles = await del([win32FilePath], {cwd: t.context.tmp, dryRun: true}); + const removeFiles = await del([filePath], {cwd: t.context.tmp, dryRun: true}); t.deepEqual(removeFiles, [filePath]); - - Object.defineProperty(process, 'platform', {value: processPlatform}); }); test('windows can pass absolute paths with "\\" - sync', t => { - const processPlatform = process.platform; - Object.defineProperty(process, 'platform', {value: 'win32'}); - const filePath = path.resolve(t.context.tmp, '1.tmp'); - const win32FilePath = path.win32.normalize(filePath); - const removeFiles = del.sync([win32FilePath], {cwd: t.context.tmp, dryRun: true}); + const removeFiles = del.sync([filePath], {cwd: t.context.tmp, dryRun: true}); t.deepEqual(removeFiles, [filePath]); - - Object.defineProperty(process, 'platform', {value: processPlatform}); }); test('windows can pass relative paths with "\\" - async', async t => { const nestedFile = path.resolve(t.context.tmp, 'a/b/c/nested.js'); makeDir.sync(nestedFile); - const processPlatform = process.platform; - Object.defineProperty(process, 'platform', {value: 'win32'}); - - const win32FilePath = path.win32.normalize(nestedFile); - const removeFiles = await del([win32FilePath], {cwd: t.context.tmp, dryRun: true}); + const removeFiles = await del([nestedFile], {cwd: t.context.tmp, dryRun: true}); t.deepEqual(removeFiles, [nestedFile]); - - Object.defineProperty(process, 'platform', {value: processPlatform}); }); test('windows can pass relative paths with "\\" - sync', t => { const nestedFile = path.resolve(t.context.tmp, 'a/b/c/nested.js'); makeDir.sync(nestedFile); - const processPlatform = process.platform; - Object.defineProperty(process, 'platform', {value: 'win32'}); - - const win32FilePath = path.win32.normalize(nestedFile); - const removeFiles = del.sync([win32FilePath], {cwd: t.context.tmp, dryRun: true}); + const removeFiles = del.sync([nestedFile], {cwd: t.context.tmp, dryRun: true}); t.deepEqual(removeFiles, [nestedFile]); - - Object.defineProperty(process, 'platform', {value: processPlatform}); }); From 3f649db3d384773833b1dbb0047f8755ce0e0980 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Sun, 18 Aug 2019 19:59:59 +0200 Subject: [PATCH 3/6] Update .travis.yml --- .travis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 38ca099..29ef4bf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ +os: + - linux + - osx + - windows language: node_js node_js: - '12' - '10' - '8' -os: - - windows - - linux - - osx From 60e9d54446c2be7a718209f8a1e5bf92fa2dc99c Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Wed, 21 Aug 2019 17:04:14 -0700 Subject: [PATCH 4/6] Remove note about backward-slashes --- index.d.ts | 4 ---- readme.md | 2 -- 2 files changed, 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index f6366c3..2ef233d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -40,8 +40,6 @@ declare const del: { /** Delete files and directories using glob patterns. - Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`. - @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js) - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns) @@ -67,8 +65,6 @@ declare const del: { /** Synchronously delete files and directories using glob patterns. - Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`. - @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js) - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns) diff --git a/readme.md b/readme.md index 79d5280..59f83a1 100644 --- a/readme.md +++ b/readme.md @@ -46,8 +46,6 @@ Suggestions on how to improve this welcome! ## API -Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`. - ### del(patterns, options?) Returns `Promise` with the deleted paths. From 047b778224751faf10e0acdf70bf6948ae7763bd Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Wed, 21 Aug 2019 17:21:27 -0700 Subject: [PATCH 5/6] Revert "Remove note about backward-slashes" This reverts commit 60e9d54446c2be7a718209f8a1e5bf92fa2dc99c. --- index.d.ts | 4 ++++ readme.md | 2 ++ 2 files changed, 6 insertions(+) diff --git a/index.d.ts b/index.d.ts index 2ef233d..f6366c3 100644 --- a/index.d.ts +++ b/index.d.ts @@ -40,6 +40,8 @@ declare const del: { /** Delete files and directories using glob patterns. + Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`. + @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js) - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns) @@ -65,6 +67,8 @@ declare const del: { /** Synchronously delete files and directories using glob patterns. + Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`. + @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js) - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns) diff --git a/readme.md b/readme.md index 59f83a1..79d5280 100644 --- a/readme.md +++ b/readme.md @@ -46,6 +46,8 @@ Suggestions on how to improve this welcome! ## API +Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`. + ### del(patterns, options?) Returns `Promise` with the deleted paths. From 3d1868e2f83e0a46825f6cdca6b83142b834d5ae Mon Sep 17 00:00:00 2001 From: Chris Blossom Date: Wed, 21 Aug 2019 17:32:41 -0700 Subject: [PATCH 6/6] update note about backward slashes and correctly spell contrast --- index.d.ts | 8 ++++---- readme.md | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index f6366c3..b4d9767 100644 --- a/index.d.ts +++ b/index.d.ts @@ -40,12 +40,12 @@ declare const del: { /** Delete files and directories using glob patterns. - Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`. + Note that glob patterns can only contain forward-slashes, not backward-slashes. Windows file paths can use backward-slashes as long as the path does not contain any glob-like characters, otherwise use `path.posix.join()` instead of `path.join()`. @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js) - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns) - @param options - You can specify any of the [`globby` options](https://github.com/sindresorhus/globby#options) in addition to the `del` options. In constrast to the `globby` defaults, `expandDirectories`, `onlyFiles`, and `followSymbolicLinks` are `false` by default. + @param options - You can specify any of the [`globby` options](https://github.com/sindresorhus/globby#options) in addition to the `del` options. In contrast to the `globby` defaults, `expandDirectories`, `onlyFiles`, and `followSymbolicLinks` are `false` by default. @returns The deleted paths. @example @@ -67,12 +67,12 @@ declare const del: { /** Synchronously delete files and directories using glob patterns. - Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`. + Note that glob patterns can only contain forward-slashes, not backward-slashes. Windows file paths can use backward-slashes as long as the path does not contain any glob-like characters, otherwise use `path.posix.join()` instead of `path.join()`. @param patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns). - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/master/test/test.js) - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns) - @param options - You can specify any of the [`globby` options](https://github.com/sindresorhus/globby#options) in addition to the `del` options. In constrast to the `globby` defaults, `expandDirectories`, `onlyFiles`, and `followSymbolicLinks` are `false` by default. + @param options - You can specify any of the [`globby` options](https://github.com/sindresorhus/globby#options) in addition to the `del` options. In contrast to the `globby` defaults, `expandDirectories`, `onlyFiles`, and `followSymbolicLinks` are `false` by default. @returns The deleted paths. */ sync( diff --git a/readme.md b/readme.md index 79d5280..36c0f06 100644 --- a/readme.md +++ b/readme.md @@ -46,7 +46,7 @@ Suggestions on how to improve this welcome! ## API -Note that glob patterns can only contain forward-slashes, not backward-slashes, so if you want to construct a glob pattern from path components, you need to use `path.posix.join()` instead of `path.join()`. +Note that glob patterns can only contain forward-slashes, not backward-slashes. Windows file paths can use backward-slashes as long as the path does not contain any glob-like characters, otherwise use `path.posix.join()` instead of `path.join()`. ### del(patterns, options?) @@ -69,7 +69,7 @@ See the supported [glob patterns](https://github.com/sindresorhus/globby#globbin Type: `object` -You can specify any of the [`globby` options](https://github.com/sindresorhus/globby#options) in addition to the below options. In constrast to the `globby` defaults, `expandDirectories`, `onlyFiles`, and `followSymbolicLinks` are `false` by default. +You can specify any of the [`globby` options](https://github.com/sindresorhus/globby#options) in addition to the below options. In contrast to the `globby` defaults, `expandDirectories`, `onlyFiles`, and `followSymbolicLinks` are `false` by default. ##### force