remove-glob 1.0.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -45,10 +45,14 @@ Options:
45
45
  -g, --glob Glob pattern(s) to find which files/dirs to remove [array]
46
46
  -s, --stat Show the stats of the items being removed [boolean]
47
47
  -V, --verbose If true, it will log each file or directory being removed [boolean]
48
+ -e, --exclude Pattern or glob to exclude (may be passed multiple times) [string|string[]]
48
49
  -h, --help Show help [boolean]
49
50
  -v, --version Show version number [boolean]
50
51
  ```
51
52
 
53
+ > [!NOTE]
54
+ > When `exclude` glob pattern(s) are provided, it will override the default exclude of: [`"**/.git/**", "**/.git", "**/node_modules/**", "**/node_modules"`]
55
+
52
56
  Remove files or directories. Note: on Windows globs must be **double quoted**, everybody else can quote however they please.
53
57
 
54
58
  ```sh
@@ -66,7 +70,15 @@ $ remove --glob \"dist/**/*.{js,map}\"
66
70
  ```
67
71
 
68
72
  > [!NOTE]
69
- > When using the `--glob` option, it will skip `.git/` and `node_modules/` directories by default.
73
+ > When using the `--glob` option, it will skip `.git/` and `node_modules/` directories by default (using the default `--exclude` patterns).
74
+ > If you want to allow deletion of these directories, you can override the default by providing your own `--exclude` option (including an empty array to disable exclusion):
75
+ >
76
+ > ```sh
77
+ > # Remove everything, including .git and node_modules
78
+ > $ npx remove --glob "**/*" --exclude ""
79
+ > # Or exclude only .git, but allow node_modules to be deleted
80
+ > $ npx remove --glob "**/*" --exclude "**/.git/**" --exclude "**/.git"
81
+ > ```
70
82
 
71
83
  ### Usage
72
84
 
@@ -102,6 +114,7 @@ The first argument is an object holding any of the options shown below. The last
102
114
  dryRun: boolean; // show what would be copied, without actually copying anything
103
115
  paths: string | string[]; // filepath(s) to remove – may be a file or a directory.
104
116
  glob: string; // glob pattern to find which files/directories to remove
117
+ exclude: string[]; // glob pattern(s) to exclude from deletion
105
118
  stats: boolean; // show some statistics after execution (time + file count)
106
119
  verbose: boolean; // print more information to console when executing the removal
107
120
  }
@@ -109,6 +122,7 @@ The first argument is an object holding any of the options shown below. The last
109
122
 
110
123
  > [!NOTE]
111
124
  > The first argument is required and it **must** include either a `paths` or a `glob`, but it cannot include both options simultaneously.
125
+ > Also when `exclude` glob pattern(s) are provided, it will override the default exclude of: [`"**/.git/**", "**/.git", "**/node_modules/**", "**/node_modules"`]
112
126
 
113
127
  ### Used by
114
128
 
package/dist/cli.js CHANGED
@@ -70,6 +70,11 @@ try {
70
70
  default: false,
71
71
  describe: 'If true, it will log each file or directory being removed',
72
72
  },
73
+ exclude: {
74
+ alias: 'e',
75
+ type: 'array',
76
+ describe: 'Glob pattern(s) to exclude from deletion (overrides the default patterns)',
77
+ },
73
78
  },
74
79
  version: readPackage().version,
75
80
  };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGrD;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,GAAE,aAAkB,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,KAAK,IAAI,qBAwElF"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGrD;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,GAAE,aAAkB,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,KAAK,IAAI,qBAgFlF"}
package/dist/index.js CHANGED
@@ -8,24 +8,38 @@ import { throwOrCallback } from './utils.js';
8
8
  */
9
9
  export function removeSync(opts = {}, callback) {
10
10
  const cb = callback || opts.callback;
11
- let paths = opts.paths || [];
11
+ let paths = [];
12
+ if (typeof opts.paths === 'string' && opts.paths.length > 0) {
13
+ paths = [opts.paths];
14
+ }
15
+ else if (Array.isArray(opts.paths)) {
16
+ paths = opts.paths.filter(p => typeof p === 'string' && p.length > 0);
17
+ }
18
+ let errorMsg = '';
12
19
  if (!paths.length && !opts.glob) {
13
- throwOrCallback(new Error('Please make sure to provide file paths via command arguments or via `--glob` pattern, i.e.: "remove dir" or "remove --glob dir/**/*.js"'), cb);
14
- return;
20
+ errorMsg =
21
+ 'Please make sure to provide file paths via command arguments or via `--glob` pattern, i.e.: "remove dir" or "remove --glob dir/**/*.js"';
15
22
  }
16
- if (paths.length && opts.glob) {
17
- throwOrCallback(new Error('Providing both `--paths` and `--glob` pattern at the same time is not supported, you must chose only one.'), cb);
23
+ else if (paths.length > 0 && opts.glob) {
24
+ errorMsg = 'Providing both `--paths` and `--glob` pattern at the same time is not supported, you must chose only one.';
25
+ }
26
+ if (errorMsg) {
27
+ throwOrCallback(new Error(errorMsg), cb);
18
28
  return;
19
29
  }
20
30
  let pathExists = false;
21
- if (!Array.isArray(paths)) {
22
- paths = paths.length ? [paths] : [];
23
- }
31
+ // paths is always an array now
24
32
  const requiresCwdChange = !!(paths.length && opts.cwd);
25
33
  if (!paths.length && opts.glob) {
26
34
  // Use fs.globSync to match files. Dotfiles and dot-directories are always included.
27
- paths = globSync(opts.glob, { cwd: opts.cwd });
28
- // Manually resolve to absolute paths if cwd is set
35
+ const defaultExclude = ['**/.git/**', '**/.git', '**/node_modules/**', '**/node_modules'];
36
+ const globOptions = { cwd: opts.cwd, withFileTypes: false };
37
+ globOptions.exclude = Array.isArray(opts.exclude) ? opts.exclude : defaultExclude;
38
+ const globResult = globSync(opts.glob, globOptions);
39
+ // We reassign 'paths' here to hold the final list of files matched by glob,
40
+ // after filtering for strings and resolving to absolute paths if needed.
41
+ paths =
42
+ Array.isArray(globResult) && globResult.length > 0 ? globResult.filter((v) => typeof v === 'string') : [];
29
43
  if (opts.cwd) {
30
44
  paths = paths.map(p => resolve(opts.cwd, p));
31
45
  }
@@ -21,5 +21,10 @@ export interface RemoveOptions {
21
21
  stat?: boolean;
22
22
  /** Print more information to console */
23
23
  verbose?: boolean;
24
+ /**
25
+ * Glob pattern(s) to exclude from deletion. If not provided, defaults to `["**\/.git/**", "**\/.git", "**\/node_modules/**", "**\/node_modules"]`
26
+ * To disable exclusion and allow deleting everything, pass an empty array ([]).
27
+ */
28
+ exclude?: string | string[];
24
29
  }
25
30
  //# sourceMappingURL=interfaces.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"interfaces.d.ts","sourceRoot":"","sources":["../src/interfaces.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,sEAAsE;IACtE,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,KAAK,IAAI,CAAC;IAE/B;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb,2EAA2E;IAC3E,MAAM,CAAC,EAAE,OAAO,CAAC;IAEjB,6DAA6D;IAC7D,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAEzB;;;;;OAKG;IACH,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAE1B,0CAA0C;IAC1C,IAAI,CAAC,EAAE,OAAO,CAAC;IAEf,wCAAwC;IACxC,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB"}
1
+ {"version":3,"file":"interfaces.d.ts","sourceRoot":"","sources":["../src/interfaces.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC5B,sEAAsE;IACtE,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,KAAK,IAAI,CAAC;IAE/B;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IAEb,2EAA2E;IAC3E,MAAM,CAAC,EAAE,OAAO,CAAC;IAEjB,6DAA6D;IAC7D,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAEzB;;;;;OAKG;IACH,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAE1B,0CAA0C;IAC1C,IAAI,CAAC,EAAE,OAAO,CAAC;IAEf,wCAAwC;IACxC,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CAC7B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remove-glob",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "A tiny utility to remove items or directories recursively, also supports glob",
5
5
  "bin": {
6
6
  "remove": "dist/cli.js"
package/src/cli.ts CHANGED
@@ -75,6 +75,11 @@ try {
75
75
  default: false,
76
76
  describe: 'If true, it will log each file or directory being removed',
77
77
  },
78
+ exclude: {
79
+ alias: 'e',
80
+ type: 'array',
81
+ describe: 'Glob pattern(s) to exclude from deletion (overrides the default patterns)',
82
+ },
78
83
  },
79
84
  version: readPackage().version,
80
85
  } as const;
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { GlobOptions } from 'node:fs';
1
2
  import { existsSync, globSync, rmSync, statSync, unlinkSync } from 'node:fs';
2
3
  import { resolve } from 'node:path';
3
4
  import type { RemoveOptions } from './interfaces.js';
@@ -10,37 +11,45 @@ import { throwOrCallback } from './utils.js';
10
11
  */
11
12
  export function removeSync(opts: RemoveOptions = {}, callback?: (e?: Error) => void) {
12
13
  const cb = callback || opts.callback;
13
- let paths = opts.paths || [];
14
+ let paths: string[] = [];
15
+ if (typeof opts.paths === 'string' && opts.paths.length > 0) {
16
+ paths = [opts.paths];
17
+ } else if (Array.isArray(opts.paths)) {
18
+ paths = opts.paths.filter(p => typeof p === 'string' && p.length > 0);
19
+ }
20
+
21
+ let errorMsg = '';
14
22
  if (!paths.length && !opts.glob) {
15
- throwOrCallback(
16
- new Error(
17
- 'Please make sure to provide file paths via command arguments or via `--glob` pattern, i.e.: "remove dir" or "remove --glob dir/**/*.js"',
18
- ),
19
- cb,
20
- );
21
- return;
23
+ errorMsg =
24
+ 'Please make sure to provide file paths via command arguments or via `--glob` pattern, i.e.: "remove dir" or "remove --glob dir/**/*.js"';
25
+ } else if (paths.length > 0 && opts.glob) {
26
+ errorMsg = 'Providing both `--paths` and `--glob` pattern at the same time is not supported, you must chose only one.';
22
27
  }
23
- if (paths.length && opts.glob) {
24
- throwOrCallback(
25
- new Error('Providing both `--paths` and `--glob` pattern at the same time is not supported, you must chose only one.'),
26
- cb,
27
- );
28
+
29
+ if (errorMsg) {
30
+ throwOrCallback(new Error(errorMsg), cb);
28
31
  return;
29
32
  }
33
+
30
34
  let pathExists = false;
31
- if (!Array.isArray(paths)) {
32
- paths = paths.length ? [paths] : [];
33
- }
35
+ // paths is always an array now
34
36
  const requiresCwdChange = !!(paths.length && opts.cwd);
35
37
  if (!paths.length && opts.glob) {
36
38
  // Use fs.globSync to match files. Dotfiles and dot-directories are always included.
37
- paths = globSync(opts.glob, { cwd: opts.cwd });
39
+ const defaultExclude = ['**/.git/**', '**/.git', '**/node_modules/**', '**/node_modules'];
40
+ const globOptions: GlobOptions = { cwd: opts.cwd, withFileTypes: false };
41
+ globOptions.exclude = Array.isArray(opts.exclude) ? opts.exclude : defaultExclude;
38
42
 
39
- // Manually resolve to absolute paths if cwd is set
43
+ const globResult = globSync(opts.glob, globOptions);
44
+ // We reassign 'paths' here to hold the final list of files matched by glob,
45
+ // after filtering for strings and resolving to absolute paths if needed.
46
+ paths =
47
+ Array.isArray(globResult) && globResult.length > 0 ? (globResult as unknown[]).filter((v): v is string => typeof v === 'string') : [];
40
48
  if (opts.cwd) {
41
49
  paths = paths.map(p => resolve(opts.cwd as string, p));
42
50
  }
43
51
  }
52
+
44
53
  if (opts.stat || opts.verbose) {
45
54
  console.time('Duration');
46
55
  }
package/src/interfaces.ts CHANGED
@@ -27,4 +27,10 @@ export interface RemoveOptions {
27
27
 
28
28
  /** Print more information to console */
29
29
  verbose?: boolean;
30
+
31
+ /**
32
+ * Glob pattern(s) to exclude from deletion. If not provided, defaults to `["**\/.git/**", "**\/.git", "**\/node_modules/**", "**\/node_modules"]`
33
+ * To disable exclusion and allow deleting everything, pass an empty array ([]).
34
+ */
35
+ exclude?: string | string[];
30
36
  }