Skip to content

Commit

Permalink
Allow non-glob patterns with backslash on Windows (#100)
Browse files Browse the repository at this point in the history
This reverts commit 60e9d54
  • Loading branch information
chrisblossom authored and sindresorhus committed Aug 23, 2019
1 parent 9c72270 commit 01da91f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
@@ -1,3 +1,7 @@
os:
- linux
- osx
- windows
language: node_js
node_js:
- '12'
Expand Down
8 changes: 4 additions & 4 deletions index.d.ts
Expand Up @@ -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
Expand All @@ -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(
Expand Down
20 changes: 20 additions & 0 deletions index.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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,
Expand All @@ -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));

Expand Down Expand Up @@ -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));

Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -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"
},
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -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?)

Expand All @@ -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

Expand Down
34 changes: 34 additions & 0 deletions test.js
Expand Up @@ -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]);
});

0 comments on commit 01da91f

Please sign in to comment.