@sapphire/node-utilities 1.1.0-next.d17bd10.0 β†’ 1.1.0-pr-434.34ba851.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
1
  # Changelog
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
+
5
+ # [@sapphire/node-utilities@1.0.0](https://github.com/sapphiredev/utilities/tree/@sapphire/node-utilities@1.0.0) - (2022-10-02)
6
+
7
+ ## πŸš€ Features
8
+
9
+ - Add `@sapphire/node-utilities` (#468) ([1606208](https://github.com/sapphiredev/utilities/commit/1606208ce2e1ca8a6b307d14ce7f119e6c914a97))
10
+
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  # @sapphire/node-utilities
6
6
 
7
- **Common JavaScript utilities for the Sapphire Community.**
7
+ **Common NodeJS utilities for the Sapphire Community.**
8
8
 
9
9
  [![GitHub](https://img.shields.io/github/license/sapphiredev/utilities)](https://github.com/sapphiredev/utilities/blob/main/LICENSE.md)
10
10
  [![codecov](https://codecov.io/gh/sapphiredev/utilities/branch/main/graph/badge.svg?token=OEGIV6RFDO)](https://codecov.io/gh/sapphiredev/utilities)
@@ -13,6 +13,15 @@
13
13
 
14
14
  </div>
15
15
 
16
+ **Table of Contents**
17
+
18
+ - [Features](#features)
19
+ - [Installation](#installation)
20
+ - [Usage](#usage)
21
+ - [`findFilesRecursively`](#findfilesrecursively)
22
+ - [Buy us some doughnuts](#buy-us-some-doughnuts)
23
+ - [Contributors ✨](#contributors-)
24
+
16
25
  ## Features
17
26
 
18
27
  - Written in TypeScript
@@ -27,6 +36,32 @@ You can use the following command to install this package, or replace `npm insta
27
36
  npm install @sapphire/node-utilities
28
37
  ```
29
38
 
39
+ ## Usage
40
+
41
+ ### `findFilesRecursively`
42
+
43
+ Recursively searches for files in a directory and returns an [`AsyncIterableIterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) of the found paths.
44
+
45
+ ```ts
46
+ import { findFilesRecursively } from '@sapphire/node-utilities';
47
+
48
+ const files = findFilesRecursively('./src');
49
+ for await (const file of files) {
50
+ console.log(file);
51
+ }
52
+
53
+ // This, for example, could log:
54
+ // /root/src/index.js
55
+ // /root/src/database.csv
56
+ // /root/src/lib/utils/constants.json
57
+
58
+ // Alternate forms:
59
+ const files = findFilesRecursivelyStringStartsWith('./src', 'index'); // filename starts with "index"
60
+ const files = findFilesRecursivelyStringEndsWith('./src', '.js'); // filename ends with ".js"
61
+ const files = findFilesRecursivelyStringIncludes('./src', 'database'); // filename includes "database"
62
+ const files = findFilesRecursivelyRegex('./src', /\.[tj]sx?$/); // filename matches the regex
63
+ ```
64
+
30
65
  ---
31
66
 
32
67
  ## Buy us some doughnuts
@@ -100,6 +135,14 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
100
135
  <td align="center"><a href="https://github.com/EvolutionX-10"><img src="https://avatars.githubusercontent.com/u/85353424?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Evo</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=EvolutionX-10" title="Code">πŸ’»</a></td>
101
136
  <td align="center"><a href="https://enes.ovh/"><img src="https://avatars.githubusercontent.com/u/61084101?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Enes GenΓ§</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=enxg" title="Code">πŸ’»</a></td>
102
137
  <td align="center"><a href="https://github.com/muchnameless"><img src="https://avatars.githubusercontent.com/u/12682826?v=4?s=100" width="100px;" alt=""/><br /><sub><b>muchnameless</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=muchnameless" title="Code">πŸ’»</a></td>
138
+ <td align="center"><a href="https://github.com/r-priyam"><img src="https://avatars.githubusercontent.com/u/50884372?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Priyam</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=r-priyam" title="Code">πŸ’»</a></td>
139
+ <td align="center"><a href="https://github.com/legendhimslef"><img src="https://avatars.githubusercontent.com/u/69213593?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Voxelli</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=legendhimslef" title="Code">πŸ’»</a></td>
140
+ <td align="center"><a href="https://github.com/CitTheDev"><img src="https://avatars.githubusercontent.com/u/94020875?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Cit The Dev</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=CitTheDev" title="Code">πŸ’»</a></td>
141
+ </tr>
142
+ <tr>
143
+ <td align="center"><a href="https://www.goestav.com/"><img src="https://avatars.githubusercontent.com/u/27970303?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Goestav</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=goestav" title="Code">πŸ’»</a></td>
144
+ <td align="center"><a href="https://github.com/didinele"><img src="https://avatars.githubusercontent.com/u/27137376?v=4?s=100" width="100px;" alt=""/><br /><sub><b>DD</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=didinele" title="Code">πŸ’»</a></td>
145
+ <td align="center"><a href="https://steamcommunity.com/id/06000208"><img src="https://avatars.githubusercontent.com/u/52764066?v=4?s=100" width="100px;" alt=""/><br /><sub><b>amber</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=06000208" title="Code">πŸ’»</a></td>
103
146
  </tr>
104
147
  </table>
105
148
 
package/dist/index.js CHANGED
@@ -1,45 +1,17 @@
1
- "use strict";
2
- "use strict";
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
+ 'use strict';
21
2
 
22
- // src/index.ts
23
- var src_exports = {};
24
- __export(src_exports, {
25
- findFilesRecursively: () => findFilesRecursively,
26
- findFilesRecursivelyRegex: () => findFilesRecursivelyRegex,
27
- findFilesRecursivelyStringEndsWith: () => findFilesRecursivelyStringEndsWith,
28
- findFilesRecursivelyStringIncludes: () => findFilesRecursivelyStringIncludes,
29
- findFilesRecursivelyStringStartsWith: () => findFilesRecursivelyStringStartsWith
30
- });
31
- module.exports = __toCommonJS(src_exports);
3
+ var promises = require('fs/promises');
4
+ var path = require('path');
32
5
 
33
- // src/lib/findFilesRecursively.ts
34
- var import_promises = require("fs/promises");
35
- var import_node_path = require("path");
36
- async function* findFilesRecursively(path, predicate = () => true) {
37
- const dir = await (0, import_promises.opendir)(path);
6
+ var __defProp = Object.defineProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ async function* findFilesRecursively(path$1, predicate = () => true) {
9
+ const dir = await promises.opendir(path$1);
38
10
  for await (const item of dir) {
39
11
  if (item.isFile() && predicate(item.name)) {
40
- yield (0, import_node_path.join)(dir.path, item.name);
12
+ yield path.join(dir.path, item.name);
41
13
  } else if (item.isDirectory()) {
42
- yield* findFilesRecursively((0, import_node_path.join)(dir.path, item.name), predicate);
14
+ yield* findFilesRecursively(path.join(dir.path, item.name), predicate);
43
15
  }
44
16
  }
45
17
  }
@@ -60,12 +32,11 @@ function findFilesRecursivelyRegex(path, regex) {
60
32
  return findFilesRecursively(path, (filePath) => regex.test(filePath));
61
33
  }
62
34
  __name(findFilesRecursivelyRegex, "findFilesRecursivelyRegex");
63
- // Annotate the CommonJS export names for ESM import in node:
64
- 0 && (module.exports = {
65
- findFilesRecursively,
66
- findFilesRecursivelyRegex,
67
- findFilesRecursivelyStringEndsWith,
68
- findFilesRecursivelyStringIncludes,
69
- findFilesRecursivelyStringStartsWith
70
- });
35
+
36
+ exports.findFilesRecursively = findFilesRecursively;
37
+ exports.findFilesRecursivelyRegex = findFilesRecursivelyRegex;
38
+ exports.findFilesRecursivelyStringEndsWith = findFilesRecursivelyStringEndsWith;
39
+ exports.findFilesRecursivelyStringIncludes = findFilesRecursivelyStringIncludes;
40
+ exports.findFilesRecursivelyStringStartsWith = findFilesRecursivelyStringStartsWith;
41
+ //# sourceMappingURL=out.js.map
71
42
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/lib/findFilesRecursively.ts"],"sourcesContent":["export * from './lib/findFilesRecursively';\n","import type { PathLike } from 'node:fs';\nimport { opendir } from 'node:fs/promises';\nimport { join } from 'node:path';\n\n/**\n *\n * @param path The path in which to find files.\n * @param predicate A predicate function receives the path as a parameter. Truthy values will have the path included, falsey values will have the file excluded.\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursively(path, predicate)) {}`\n *\n * @example\n * ```typescript\n * // With CommonJS: To find all files ending with `.ts` in the src directory:\n * const path = require('node:path');\n *\n * for await (const file of findFilesRecursively(path.join(__dirname, 'src'), (filePath) => filePath.endsWith('.ts'))) {\n * console.log(file);\n * }\n * ```\n *\n * @example\n * ```typescript\n * // With ESM: To find all files ending with `.ts` in the src directory:\n * for await (const file of findFilesRecursively(new URL('src', import.meta.url), (filePath) => filePath.endsWith('.ts'))) {\n * console.log(file);\n * }\n * ```\n */\nexport async function* findFilesRecursively(path: PathLike, predicate: (filePath: string) => boolean = () => true): AsyncIterableIterator<string> {\n\tconst dir = await opendir(path);\n\n\tfor await (const item of dir) {\n\t\tif (item.isFile() && predicate(item.name)) {\n\t\t\tyield join(dir.path, item.name);\n\t\t} else if (item.isDirectory()) {\n\t\t\tyield* findFilesRecursively(join(dir.path, item.name), predicate);\n\t\t}\n\t}\n}\n\n/**\n *\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param fileStartsWith The string pattern with which the file name must start.\n *\n * Note that we do **not** support a full globby pattern using asterisk for wildcards. It has to be an exact match with {@link String.startsWith}\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyStringStartsWith(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyStringStartsWith(path: PathLike, fileStartsWith: string) {\n\treturn findFilesRecursively(path, (filePath) => filePath.startsWith(fileStartsWith));\n}\n\n/**\n *\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param fileEndsWith The string pattern with which the file name must end.\n * Ideally this is a file extension, however you can also provide more parts of the end of the file.\n *\n * Note that we do **not** support a full globby pattern using asterisk for wildcards. It has to be an exact match with {@link String.endsWith}\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyStringEndsWith(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyStringEndsWith(path: PathLike, fileEndsWith: string) {\n\treturn findFilesRecursively(path, (filePath) => filePath.endsWith(fileEndsWith));\n}\n\n/**\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param include The string pattern which must be present in the file name.\n *\n * Note that we do **not** support a full globby pattern using asterisk for wildcards. It has to be an exact match with {@link String.includes}\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyStringIncludes(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyStringIncludes(path: PathLike, include: string) {\n\treturn findFilesRecursively(path, (filePath) => filePath.includes(include));\n}\n\n/**\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param regex The regex pattern that the file name must match.\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyRegex(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyRegex(path: PathLike, regex: RegExp) {\n\treturn findFilesRecursively(path, (filePath) => regex.test(filePath));\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,sBAAwB;AACxB,uBAAqB;AA2BrB,gBAAuB,qBAAqB,MAAgB,YAA2C,MAAM,MAAqC;AACjJ,QAAM,MAAM,UAAM,yBAAQ,IAAI;AAE9B,mBAAiB,QAAQ,KAAK;AAC7B,QAAI,KAAK,OAAO,KAAK,UAAU,KAAK,IAAI,GAAG;AAC1C,gBAAM,uBAAK,IAAI,MAAM,KAAK,IAAI;AAAA,IAC/B,WAAW,KAAK,YAAY,GAAG;AAC9B,aAAO,yBAAqB,uBAAK,IAAI,MAAM,KAAK,IAAI,GAAG,SAAS;AAAA,IACjE;AAAA,EACD;AACD;AAVuB;AAqBhB,SAAS,qCAAqC,MAAgB,gBAAwB;AAC5F,SAAO,qBAAqB,MAAM,CAAC,aAAa,SAAS,WAAW,cAAc,CAAC;AACpF;AAFgB;AAcT,SAAS,mCAAmC,MAAgB,cAAsB;AACxF,SAAO,qBAAqB,MAAM,CAAC,aAAa,SAAS,SAAS,YAAY,CAAC;AAChF;AAFgB;AAYT,SAAS,mCAAmC,MAAgB,SAAiB;AACnF,SAAO,qBAAqB,MAAM,CAAC,aAAa,SAAS,SAAS,OAAO,CAAC;AAC3E;AAFgB;AAUT,SAAS,0BAA0B,MAAgB,OAAe;AACxE,SAAO,qBAAqB,MAAM,CAAC,aAAa,MAAM,KAAK,QAAQ,CAAC;AACrE;AAFgB;","names":[]}
1
+ {"version":3,"sources":["../src/lib/findFilesRecursively.ts"],"names":[],"mappings":";;;;AACA,SAAS,eAAe;AACxB,SAAS,YAAY;AA2BrB,gBAAuB,qBAAqB,MAAgB,YAA2C,MAAM,MAAqC;AACjJ,QAAM,MAAM,MAAM,QAAQ,IAAI;AAE9B,mBAAiB,QAAQ,KAAK;AAC7B,QAAI,KAAK,OAAO,KAAK,UAAU,KAAK,IAAI,GAAG;AAC1C,YAAM,KAAK,IAAI,MAAM,KAAK,IAAI;AAAA,IAC/B,WAAW,KAAK,YAAY,GAAG;AAC9B,aAAO,qBAAqB,KAAK,IAAI,MAAM,KAAK,IAAI,GAAG,SAAS;AAAA,IACjE;AAAA,EACD;AACD;AAVuB;AAqBhB,SAAS,qCAAqC,MAAgB,gBAAwB;AAC5F,SAAO,qBAAqB,MAAM,CAAC,aAAa,SAAS,WAAW,cAAc,CAAC;AACpF;AAFgB;AAcT,SAAS,mCAAmC,MAAgB,cAAsB;AACxF,SAAO,qBAAqB,MAAM,CAAC,aAAa,SAAS,SAAS,YAAY,CAAC;AAChF;AAFgB;AAYT,SAAS,mCAAmC,MAAgB,SAAiB;AACnF,SAAO,qBAAqB,MAAM,CAAC,aAAa,SAAS,SAAS,OAAO,CAAC;AAC3E;AAFgB;AAUT,SAAS,0BAA0B,MAAgB,OAAe;AACxE,SAAO,qBAAqB,MAAM,CAAC,aAAa,MAAM,KAAK,QAAQ,CAAC;AACrE;AAFgB","sourcesContent":["import type { PathLike } from 'node:fs';\nimport { opendir } from 'node:fs/promises';\nimport { join } from 'node:path';\n\n/**\n *\n * @param path The path in which to find files.\n * @param predicate A predicate function receives the path as a parameter. Truthy values will have the path included, falsey values will have the file excluded.\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursively(path, predicate)) {}`\n *\n * @example\n * ```typescript\n * // With CommonJS: To find all files ending with `.ts` in the src directory:\n * const path = require('node:path');\n *\n * for await (const file of findFilesRecursively(path.join(__dirname, 'src'), (filePath) => filePath.endsWith('.ts'))) {\n * console.log(file);\n * }\n * ```\n *\n * @example\n * ```typescript\n * // With ESM: To find all files ending with `.ts` in the src directory:\n * for await (const file of findFilesRecursively(new URL('src', import.meta.url), (filePath) => filePath.endsWith('.ts'))) {\n * console.log(file);\n * }\n * ```\n */\nexport async function* findFilesRecursively(path: PathLike, predicate: (filePath: string) => boolean = () => true): AsyncIterableIterator<string> {\n\tconst dir = await opendir(path);\n\n\tfor await (const item of dir) {\n\t\tif (item.isFile() && predicate(item.name)) {\n\t\t\tyield join(dir.path, item.name);\n\t\t} else if (item.isDirectory()) {\n\t\t\tyield* findFilesRecursively(join(dir.path, item.name), predicate);\n\t\t}\n\t}\n}\n\n/**\n *\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param fileStartsWith The string pattern with which the file name must start.\n *\n * Note that we do **not** support a full globby pattern using asterisk for wildcards. It has to be an exact match with {@link String.startsWith}\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyStringStartsWith(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyStringStartsWith(path: PathLike, fileStartsWith: string) {\n\treturn findFilesRecursively(path, (filePath) => filePath.startsWith(fileStartsWith));\n}\n\n/**\n *\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param fileEndsWith The string pattern with which the file name must end.\n * Ideally this is a file extension, however you can also provide more parts of the end of the file.\n *\n * Note that we do **not** support a full globby pattern using asterisk for wildcards. It has to be an exact match with {@link String.endsWith}\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyStringEndsWith(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyStringEndsWith(path: PathLike, fileEndsWith: string) {\n\treturn findFilesRecursively(path, (filePath) => filePath.endsWith(fileEndsWith));\n}\n\n/**\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param include The string pattern which must be present in the file name.\n *\n * Note that we do **not** support a full globby pattern using asterisk for wildcards. It has to be an exact match with {@link String.includes}\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyStringIncludes(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyStringIncludes(path: PathLike, include: string) {\n\treturn findFilesRecursively(path, (filePath) => filePath.includes(include));\n}\n\n/**\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param regex The regex pattern that the file name must match.\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyRegex(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyRegex(path: PathLike, regex: RegExp) {\n\treturn findFilesRecursively(path, (filePath) => regex.test(filePath));\n}\n"]}
package/dist/index.mjs CHANGED
@@ -1,9 +1,8 @@
1
+ import { opendir } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+
1
4
  var __defProp = Object.defineProperty;
2
5
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
-
4
- // src/lib/findFilesRecursively.ts
5
- import { opendir } from "node:fs/promises";
6
- import { join } from "node:path";
7
6
  async function* findFilesRecursively(path, predicate = () => true) {
8
7
  const dir = await opendir(path);
9
8
  for await (const item of dir) {
@@ -31,11 +30,7 @@ function findFilesRecursivelyRegex(path, regex) {
31
30
  return findFilesRecursively(path, (filePath) => regex.test(filePath));
32
31
  }
33
32
  __name(findFilesRecursivelyRegex, "findFilesRecursivelyRegex");
34
- export {
35
- findFilesRecursively,
36
- findFilesRecursivelyRegex,
37
- findFilesRecursivelyStringEndsWith,
38
- findFilesRecursivelyStringIncludes,
39
- findFilesRecursivelyStringStartsWith
40
- };
33
+
34
+ export { findFilesRecursively, findFilesRecursivelyRegex, findFilesRecursivelyStringEndsWith, findFilesRecursivelyStringIncludes, findFilesRecursivelyStringStartsWith };
35
+ //# sourceMappingURL=out.js.map
41
36
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/lib/findFilesRecursively.ts"],"sourcesContent":["import type { PathLike } from 'node:fs';\nimport { opendir } from 'node:fs/promises';\nimport { join } from 'node:path';\n\n/**\n *\n * @param path The path in which to find files.\n * @param predicate A predicate function receives the path as a parameter. Truthy values will have the path included, falsey values will have the file excluded.\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursively(path, predicate)) {}`\n *\n * @example\n * ```typescript\n * // With CommonJS: To find all files ending with `.ts` in the src directory:\n * const path = require('node:path');\n *\n * for await (const file of findFilesRecursively(path.join(__dirname, 'src'), (filePath) => filePath.endsWith('.ts'))) {\n * console.log(file);\n * }\n * ```\n *\n * @example\n * ```typescript\n * // With ESM: To find all files ending with `.ts` in the src directory:\n * for await (const file of findFilesRecursively(new URL('src', import.meta.url), (filePath) => filePath.endsWith('.ts'))) {\n * console.log(file);\n * }\n * ```\n */\nexport async function* findFilesRecursively(path: PathLike, predicate: (filePath: string) => boolean = () => true): AsyncIterableIterator<string> {\n\tconst dir = await opendir(path);\n\n\tfor await (const item of dir) {\n\t\tif (item.isFile() && predicate(item.name)) {\n\t\t\tyield join(dir.path, item.name);\n\t\t} else if (item.isDirectory()) {\n\t\t\tyield* findFilesRecursively(join(dir.path, item.name), predicate);\n\t\t}\n\t}\n}\n\n/**\n *\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param fileStartsWith The string pattern with which the file name must start.\n *\n * Note that we do **not** support a full globby pattern using asterisk for wildcards. It has to be an exact match with {@link String.startsWith}\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyStringStartsWith(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyStringStartsWith(path: PathLike, fileStartsWith: string) {\n\treturn findFilesRecursively(path, (filePath) => filePath.startsWith(fileStartsWith));\n}\n\n/**\n *\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param fileEndsWith The string pattern with which the file name must end.\n * Ideally this is a file extension, however you can also provide more parts of the end of the file.\n *\n * Note that we do **not** support a full globby pattern using asterisk for wildcards. It has to be an exact match with {@link String.endsWith}\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyStringEndsWith(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyStringEndsWith(path: PathLike, fileEndsWith: string) {\n\treturn findFilesRecursively(path, (filePath) => filePath.endsWith(fileEndsWith));\n}\n\n/**\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param include The string pattern which must be present in the file name.\n *\n * Note that we do **not** support a full globby pattern using asterisk for wildcards. It has to be an exact match with {@link String.includes}\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyStringIncludes(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyStringIncludes(path: PathLike, include: string) {\n\treturn findFilesRecursively(path, (filePath) => filePath.includes(include));\n}\n\n/**\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param regex The regex pattern that the file name must match.\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyRegex(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyRegex(path: PathLike, regex: RegExp) {\n\treturn findFilesRecursively(path, (filePath) => regex.test(filePath));\n}\n"],"mappings":";;;;AACA,SAAS,eAAe;AACxB,SAAS,YAAY;AA2BrB,gBAAuB,qBAAqB,MAAgB,YAA2C,MAAM,MAAqC;AACjJ,QAAM,MAAM,MAAM,QAAQ,IAAI;AAE9B,mBAAiB,QAAQ,KAAK;AAC7B,QAAI,KAAK,OAAO,KAAK,UAAU,KAAK,IAAI,GAAG;AAC1C,YAAM,KAAK,IAAI,MAAM,KAAK,IAAI;AAAA,IAC/B,WAAW,KAAK,YAAY,GAAG;AAC9B,aAAO,qBAAqB,KAAK,IAAI,MAAM,KAAK,IAAI,GAAG,SAAS;AAAA,IACjE;AAAA,EACD;AACD;AAVuB;AAqBhB,SAAS,qCAAqC,MAAgB,gBAAwB;AAC5F,SAAO,qBAAqB,MAAM,CAAC,aAAa,SAAS,WAAW,cAAc,CAAC;AACpF;AAFgB;AAcT,SAAS,mCAAmC,MAAgB,cAAsB;AACxF,SAAO,qBAAqB,MAAM,CAAC,aAAa,SAAS,SAAS,YAAY,CAAC;AAChF;AAFgB;AAYT,SAAS,mCAAmC,MAAgB,SAAiB;AACnF,SAAO,qBAAqB,MAAM,CAAC,aAAa,SAAS,SAAS,OAAO,CAAC;AAC3E;AAFgB;AAUT,SAAS,0BAA0B,MAAgB,OAAe;AACxE,SAAO,qBAAqB,MAAM,CAAC,aAAa,MAAM,KAAK,QAAQ,CAAC;AACrE;AAFgB;","names":[]}
1
+ {"version":3,"sources":["../src/lib/findFilesRecursively.ts"],"names":[],"mappings":";;;;AACA,SAAS,eAAe;AACxB,SAAS,YAAY;AA2BrB,gBAAuB,qBAAqB,MAAgB,YAA2C,MAAM,MAAqC;AACjJ,QAAM,MAAM,MAAM,QAAQ,IAAI;AAE9B,mBAAiB,QAAQ,KAAK;AAC7B,QAAI,KAAK,OAAO,KAAK,UAAU,KAAK,IAAI,GAAG;AAC1C,YAAM,KAAK,IAAI,MAAM,KAAK,IAAI;AAAA,IAC/B,WAAW,KAAK,YAAY,GAAG;AAC9B,aAAO,qBAAqB,KAAK,IAAI,MAAM,KAAK,IAAI,GAAG,SAAS;AAAA,IACjE;AAAA,EACD;AACD;AAVuB;AAqBhB,SAAS,qCAAqC,MAAgB,gBAAwB;AAC5F,SAAO,qBAAqB,MAAM,CAAC,aAAa,SAAS,WAAW,cAAc,CAAC;AACpF;AAFgB;AAcT,SAAS,mCAAmC,MAAgB,cAAsB;AACxF,SAAO,qBAAqB,MAAM,CAAC,aAAa,SAAS,SAAS,YAAY,CAAC;AAChF;AAFgB;AAYT,SAAS,mCAAmC,MAAgB,SAAiB;AACnF,SAAO,qBAAqB,MAAM,CAAC,aAAa,SAAS,SAAS,OAAO,CAAC;AAC3E;AAFgB;AAUT,SAAS,0BAA0B,MAAgB,OAAe;AACxE,SAAO,qBAAqB,MAAM,CAAC,aAAa,MAAM,KAAK,QAAQ,CAAC;AACrE;AAFgB","sourcesContent":["import type { PathLike } from 'node:fs';\nimport { opendir } from 'node:fs/promises';\nimport { join } from 'node:path';\n\n/**\n *\n * @param path The path in which to find files.\n * @param predicate A predicate function receives the path as a parameter. Truthy values will have the path included, falsey values will have the file excluded.\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursively(path, predicate)) {}`\n *\n * @example\n * ```typescript\n * // With CommonJS: To find all files ending with `.ts` in the src directory:\n * const path = require('node:path');\n *\n * for await (const file of findFilesRecursively(path.join(__dirname, 'src'), (filePath) => filePath.endsWith('.ts'))) {\n * console.log(file);\n * }\n * ```\n *\n * @example\n * ```typescript\n * // With ESM: To find all files ending with `.ts` in the src directory:\n * for await (const file of findFilesRecursively(new URL('src', import.meta.url), (filePath) => filePath.endsWith('.ts'))) {\n * console.log(file);\n * }\n * ```\n */\nexport async function* findFilesRecursively(path: PathLike, predicate: (filePath: string) => boolean = () => true): AsyncIterableIterator<string> {\n\tconst dir = await opendir(path);\n\n\tfor await (const item of dir) {\n\t\tif (item.isFile() && predicate(item.name)) {\n\t\t\tyield join(dir.path, item.name);\n\t\t} else if (item.isDirectory()) {\n\t\t\tyield* findFilesRecursively(join(dir.path, item.name), predicate);\n\t\t}\n\t}\n}\n\n/**\n *\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param fileStartsWith The string pattern with which the file name must start.\n *\n * Note that we do **not** support a full globby pattern using asterisk for wildcards. It has to be an exact match with {@link String.startsWith}\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyStringStartsWith(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyStringStartsWith(path: PathLike, fileStartsWith: string) {\n\treturn findFilesRecursively(path, (filePath) => filePath.startsWith(fileStartsWith));\n}\n\n/**\n *\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param fileEndsWith The string pattern with which the file name must end.\n * Ideally this is a file extension, however you can also provide more parts of the end of the file.\n *\n * Note that we do **not** support a full globby pattern using asterisk for wildcards. It has to be an exact match with {@link String.endsWith}\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyStringEndsWith(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyStringEndsWith(path: PathLike, fileEndsWith: string) {\n\treturn findFilesRecursively(path, (filePath) => filePath.endsWith(fileEndsWith));\n}\n\n/**\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param include The string pattern which must be present in the file name.\n *\n * Note that we do **not** support a full globby pattern using asterisk for wildcards. It has to be an exact match with {@link String.includes}\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyStringIncludes(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyStringIncludes(path: PathLike, include: string) {\n\treturn findFilesRecursively(path, (filePath) => filePath.includes(include));\n}\n\n/**\n * @param path The path in which to find files. This can be a string, buffer, or {@link URL}.\n * @param regex The regex pattern that the file name must match.\n *\n * @return An {@link AsyncIterableIterator<string>} of all the files. To loop over these use `for await (const file of findFilesRecursivelyRegex(path, fileNameEndsWith)) {}`\n */\nexport function findFilesRecursivelyRegex(path: PathLike, regex: RegExp) {\n\treturn findFilesRecursively(path, (filePath) => regex.test(filePath));\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sapphire/node-utilities",
3
- "version": "1.1.0-next.d17bd10.0",
3
+ "version": "1.1.0-pr-434.34ba851.0",
4
4
  "description": "Node specific JavaScript utilities for the Sapphire Community",
5
5
  "author": "@sapphire",
6
6
  "license": "MIT",
@@ -55,12 +55,12 @@
55
55
  "access": "public"
56
56
  },
57
57
  "devDependencies": {
58
- "@favware/cliff-jumper": "^1.8.7",
59
- "@vitest/coverage-c8": "^0.23.4",
60
- "tsup": "^6.2.3",
61
- "typedoc": "^0.23.15",
62
- "typedoc-json-parser": "^5.0.1",
63
- "typescript": "^4.8.4",
64
- "vitest": "^0.23.4"
58
+ "@favware/cliff-jumper": "^1.9.0",
59
+ "@vitest/coverage-c8": "^0.26.2",
60
+ "tsup": "^6.5.0",
61
+ "typedoc": "^0.23.23",
62
+ "typedoc-json-parser": "^7.0.2",
63
+ "typescript": "^4.9.4",
64
+ "vitest": "^0.26.2"
65
65
  }
66
66
  }