Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes: #138 - Update index.js #139

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
141 changes: 59 additions & 82 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,84 +1,61 @@
import {GlobbyOptions} from 'globby';

declare namespace del {
interface Options extends GlobbyOptions {
/**
Allow deleting the current working directory and outside.

@default false
*/
readonly force?: boolean;

/**
See what would be deleted.

@default false

@example
```
import del = require('del');

(async () => {
const deletedPaths = await del(['temp/*.js'], {dryRun: true});

console.log('Files and directories that would be deleted:\n', deletedPaths.join('\n'));
})();
```
*/
readonly dryRun?: boolean;

/**
Concurrency limit. Minimum: `1`.

@default Infinity
*/
readonly concurrency?: number;
}
}

declare const del: {
// v13.1.1 export type GlobbyOptions = import('globby').Options;
export type GlobbyOptions = import('globby').GlobbyOptions;
export interface delOptionPropertys {
/**
Synchronously delete files and directories using glob patterns.

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/main/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 contrast to the `globby` defaults, `expandDirectories`, `onlyFiles`, and `followSymbolicLinks` are `false` by default.
@returns The deleted paths.
*/
sync: (
patterns: string | readonly string[],
options?: del.Options
) => string[];

* default false
*/
force?: boolean;
/**
Delete files and directories using glob patterns.

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/main/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 contrast to the `globby` defaults, `expandDirectories`, `onlyFiles`, and `followSymbolicLinks` are `false` by default.
@returns The deleted paths.

@example
```
import del = require('del');

(async () => {
const deletedPaths = await del(['temp/*.js', '!temp/unicorn.js']);

console.log('Deleted files and directories:\n', deletedPaths.join('\n'));
})();
```
*/
(
patterns: string | readonly string[],
options?: del.Options
): Promise<string[]>;
};

export = del;
* default false
*/
dryRun?: boolean;
/**
* default false
*/
cwd?: string;
}
export type delOptions = delOptionPropertys & GlobbyOptions;
export type patterns = string | readonly string[];

/**
* Synchronously delete files and directories using glob patterns.
* 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} patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
* - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/main/test/test.js)
* - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns)
* @param {delOptions} delOptions - You can specify any of the
* [`globby` options](https://github.com/sindresorhus/globby#options) in addition to the `delOptions`. In contrast to the `globby` defaults, `expandDirectories`, `onlyFiles`, and `followSymbolicLinks` are `false` by default.
* @returns {string[]} The deleted paths.
* @example
* ```
* const { delSync } = require('del');
* console.log('Deleted files and directories:\n', delSync(['temp/*.js', '!temp/unicorn.js']).join('\n'));
* ```
*/
export function delSync(patterns: patterns, delOptions: delOptions): string[];
/**
* delete files and directories using glob patterns.
* 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} patterns - See the supported [glob patterns](https://github.com/sindresorhus/globby#globbing-patterns).
* - [Pattern examples with expected matches](https://github.com/sindresorhus/multimatch/blob/main/test/test.js)
* - [Quick globbing pattern overview](https://github.com/sindresorhus/multimatch#globbing-patterns)
* @param {delOptions} delOptions - You can specify any of the
* [`globby` options](https://github.com/sindresorhus/globby#options) in addition to the `delOptions`. In contrast to the `globby` defaults, `expandDirectories`, `onlyFiles`, and `followSymbolicLinks` are `false` by default.
* @returns {Promise<string[]>} The deleted paths.
* @example
* ```
* const { delAsync } = require('del');
* const deleteAndLogPathes = async (pathesToDelete) => {
* const deletedPaths = await del(pathesToDelete);
* console.log('Deleted files and directories:\n', deletedPaths.join('\n'));
* };
* deleteAndLogPathes(['temp/*.js', '!temp/unicorn.js']);
* ```
*/
export function delAsync(patterns: patterns, delOptions: delOptions): Promise<string[]>;
62 changes: 41 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const {promisify} = require('util');
const { promisify } = require('util');
const path = require('path');

const globby = require('globby');
const isGlob = require('is-glob');
const slash = require('slash');
Expand Down Expand Up @@ -30,18 +31,22 @@ const rimrafOptions = {

function safeCheck(file, cwd) {
if (isPathCwd(file)) {
throw new Error('Cannot delete the current working directory. Can be overridden with the `force` option.');
throw new Error(
'Cannot delete the current working directory. Can be overridden with the `force` option.'
);
}

if (!isPathInside(file, cwd)) {
throw new Error('Cannot delete files/directories outside the current working directory. Can be overridden with the `force` option.');
throw new Error(
'Cannot delete files/directories outside the current working directory. Can be overridden with the `force` option.'
);
}
}

function normalizePatterns(patterns) {
patterns = Array.isArray(patterns) ? patterns : [patterns];

patterns = patterns.map(pattern => {
patterns = patterns.map((pattern) => {
if (process.platform === 'win32' && isGlob(pattern) === false) {
return slash(pattern);
}
Expand All @@ -52,21 +57,38 @@ function normalizePatterns(patterns) {
return patterns;
}

module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) => {
options = {
/**
* @typedef {import('globby').GlobbyOptions} GlobbyOptions // Globby 13.1.1 version @ typedef {import('globby').Options} GlobbyOptions
* @typedef delOptionPropertys
* @property {boolean} [force] default false
* @property {boolean} [dryRun] default false
* @property {string} [cwd] default false
* @typedef {delOptionPropertys & GlobbyOptions} delOptions
* @typedef {string | readonly string[]} patterns
* @typedef {typeof delAsync & { sync: typeof delSync }} deprecatedCjsCompatModuleObject
*/
const normalizeDelOptions = (/** @type {delOptions} */ delOptions) => {
const { force = false, dryRun = false, cwd = process.cwd(), ...inputGlobbyOptions } = delOptions;

/** @type {GlobbyOptions} */
const globbyOptions = {
expandDirectories: false,
onlyFiles: false,
followSymbolicLinks: false,
cwd,
...options
...inputGlobbyOptions
};
return { force, dryRun, cwd, globbyOptions };
};

const delAsync = async (patterns, delOptions) => {
const { force, dryRun, cwd, globbyOptions: options } = normalizeDelOptions(delOptions);

patterns = normalizePatterns(patterns);

const files = (await globby(patterns, options))
.sort((a, b) => b.localeCompare(a));
const files = (await globby(patterns, options)).sort((a, b) => b.localeCompare(a));

const mapper = async file => {
const mapper = async (file) => {
file = path.resolve(cwd, file);

if (!force) {
Expand All @@ -87,21 +109,14 @@ module.exports = async (patterns, {force, dryRun, cwd = process.cwd(), ...option
return removedFiles;
};

module.exports.sync = (patterns, {force, dryRun, cwd = process.cwd(), ...options} = {}) => {
options = {
expandDirectories: false,
onlyFiles: false,
followSymbolicLinks: false,
cwd,
...options
};
const delSync = (patterns, delOptions) => {
const { force, dryRun, cwd, globbyOptions: options } = normalizeDelOptions(delOptions);

patterns = normalizePatterns(patterns);

const files = globby.sync(patterns, options)
.sort((a, b) => b.localeCompare(a));
const files = globby.sync(patterns, options).sort((a, b) => b.localeCompare(a));

const removedFiles = files.map(file => {
const removedFiles = files.map((file) => {
file = path.resolve(cwd, file);

if (!force) {
Expand All @@ -119,3 +134,8 @@ module.exports.sync = (patterns, {force, dryRun, cwd = process.cwd(), ...options

return removedFiles;
};

exports.delAsync = delAsync;
exports.delSync = delSync;

module.exports = Object.assign(() => {}, delAsync, exports, { sync: delSync });