diff --git a/.travis.yml b/.travis.yml index f98fed0..29ef4bf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,7 @@ +os: + - linux + - osx + - windows language: node_js node_js: - '12' 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/index.js b/index.js index cbdde5a..e1956b8 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 gracefulFs = require('graceful-fs'); const isPathCwd = require('is-path-cwd'); const isPathInside = require('is-path-inside'); @@ -36,6 +38,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, @@ -45,6 +61,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)); @@ -78,6 +96,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 df551d9..51b594f 100644 --- a/package.json +++ b/package.json @@ -47,8 +47,10 @@ "dependencies": { "globby": "^10.0.0", "graceful-fs": "^4.2.2", + "is-glob": "^4.0.1", "is-path-cwd": "^2.0.0", "is-path-inside": "^3.0.1", + "slash": "^3.0.0", "p-map": "^3.0.0", "rimraf": "^3.0.0" }, 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 diff --git a/test.js b/test.js index edf47c1..d5a4fcf 100644 --- a/test.js +++ b/test.js @@ -315,3 +315,37 @@ 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 filePath = path.resolve(t.context.tmp, '1.tmp'); + + const removeFiles = await del([filePath], {cwd: t.context.tmp, dryRun: true}); + + t.deepEqual(removeFiles, [filePath]); +}); + +test('windows can pass absolute paths with "\\" - sync', t => { + const filePath = path.resolve(t.context.tmp, '1.tmp'); + + const removeFiles = del.sync([filePath], {cwd: t.context.tmp, dryRun: true}); + + t.deepEqual(removeFiles, [filePath]); +}); + +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 removeFiles = await del([nestedFile], {cwd: t.context.tmp, dryRun: true}); + + t.deepEqual(removeFiles, [nestedFile]); +}); + +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 removeFiles = del.sync([nestedFile], {cwd: t.context.tmp, dryRun: true}); + + t.deepEqual(removeFiles, [nestedFile]); +});