simple-folder-search 1.0.2 → 2.2.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/README.md +23 -36
- package/dist/index.cjs +51 -22
- package/dist/index.d.cts +12 -5
- package/dist/index.d.ts +12 -5
- package/dist/index.js +51 -22
- package/docs/caching.md +102 -0
- package/docs/index.md +11 -0
- package/docs/installation.md +13 -0
- package/docs/options.md +21 -0
- package/docs/types.md +32 -0
- package/docs/usage.md +55 -0
- package/package.json +1 -1
- package/src/simple-folder-search.ts +80 -37
package/README.md
CHANGED
|
@@ -1,14 +1,6 @@
|
|
|
1
1
|
# Simple Folder Search
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
This was mostly made for use with the `discord-player` package, but it can still be used almost everywhere.
|
|
6
|
-
|
|
7
|
-
## Features
|
|
8
|
-
|
|
9
|
-
- Search for files by name
|
|
10
|
-
- Filter files by extension
|
|
11
|
-
- Recursive search through subdirectories
|
|
3
|
+
Simple Folder Search is a package that provides a simple way to search for files within a folder. It is designed to be easy to use and integrate into your projects.
|
|
12
4
|
|
|
13
5
|
## Installation
|
|
14
6
|
|
|
@@ -18,40 +10,35 @@ To install the package, use the following command:
|
|
|
18
10
|
npm install simple-folder-search
|
|
19
11
|
```
|
|
20
12
|
|
|
13
|
+
You can also install it using yarn:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
yarn add simple-folder-search
|
|
17
|
+
```
|
|
18
|
+
|
|
21
19
|
## Usage
|
|
22
20
|
|
|
23
|
-
Here is
|
|
21
|
+
Here is a basic example of how to use the package:
|
|
24
22
|
|
|
25
23
|
```javascript
|
|
26
24
|
const { simpleFolderSearch } = require('simple-folder-search');
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const player = new Player(client);
|
|
31
|
-
|
|
32
|
-
// Search for music files in your music folder
|
|
33
|
-
const musicFiles = simpleFolderSearch('./music', ['.mp3', '.wav'], 'my song', 0.6);
|
|
34
|
-
|
|
35
|
-
if (musicFiles.length > 0) {
|
|
36
|
-
// Get the first matching file
|
|
37
|
-
const firstMatch = musicFiles[0];
|
|
38
|
-
|
|
39
|
-
// Search the track using the proper engine
|
|
40
|
-
const research = player.search(firstMatch, {
|
|
41
|
-
searchEngine: QueryType.FILE,
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
// Play the file using discord-player
|
|
45
|
-
await player.play(voiceChannel, research);
|
|
46
|
-
} else {
|
|
47
|
-
console.log('No matching files found');
|
|
48
|
-
}
|
|
25
|
+
|
|
26
|
+
const results = await simpleFolderSearch('./music', ['.mp3', '.wav'], 'my song', { minimumScore: 0.6 });
|
|
27
|
+
console.log(results);
|
|
49
28
|
```
|
|
50
29
|
|
|
51
|
-
|
|
30
|
+
For more detailed usage and advanced options, please refer to the [Usage Documentation](docs/usage.md).
|
|
52
31
|
|
|
53
|
-
|
|
32
|
+
## Documentation
|
|
54
33
|
|
|
55
|
-
|
|
34
|
+
For detailed documentation, please refer to the following files:
|
|
35
|
+
|
|
36
|
+
- [Installation](docs/installation.md)
|
|
37
|
+
- [Usage](docs/usage.md)
|
|
38
|
+
- [Options](docs/options.md)
|
|
39
|
+
- [Custom Caching](docs/caching.md)
|
|
40
|
+
- [Types](docs/types.md)
|
|
41
|
+
|
|
42
|
+
## License
|
|
56
43
|
|
|
57
|
-
|
|
44
|
+
This project is licensed under the MIT License.
|
package/dist/index.cjs
CHANGED
|
@@ -38,41 +38,70 @@ module.exports = __toCommonJS(index_exports);
|
|
|
38
38
|
var import_fs = __toESM(require("fs"), 1);
|
|
39
39
|
var import_path = __toESM(require("path"), 1);
|
|
40
40
|
var import_fuse = __toESM(require("fuse.js"), 1);
|
|
41
|
-
function simpleFolderSearch(filepath, fileExtensions, search,
|
|
41
|
+
async function simpleFolderSearch(filepath, fileExtensions, search, options = {}, cacheCallback) {
|
|
42
|
+
const {
|
|
43
|
+
minimumScore = 0.6,
|
|
44
|
+
batchSize = 100,
|
|
45
|
+
parallelSearches = 1
|
|
46
|
+
} = options;
|
|
42
47
|
const absolutePath = import_path.default.resolve(process.cwd(), filepath);
|
|
43
48
|
if (!import_fs.default.existsSync(absolutePath)) throw new Error("Filepath does not exist.");
|
|
44
49
|
if (!Array.isArray(fileExtensions)) throw new Error("fileExtensions must be an array.");
|
|
45
|
-
if (typeof search !== "string") throw new Error("Search query must be a string.");
|
|
50
|
+
if (typeof search !== "string" && !Array.isArray(search)) throw new Error("Search query must be a string or an array.");
|
|
46
51
|
if (typeof minimumScore !== "number") throw new Error("Minimum score must be a number.");
|
|
47
52
|
if (minimumScore < 0 || minimumScore > 1) throw new Error("Minimum score must be between 0 and 1.");
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
if (typeof batchSize !== "number" || batchSize <= 0) throw new Error("Batch size must be a positive number.");
|
|
54
|
+
if (typeof parallelSearches !== "number" || parallelSearches <= 0) throw new Error("Parallel searches must be a positive number.");
|
|
55
|
+
const filesForFuse = [];
|
|
56
|
+
const processFiles = async (files) => {
|
|
57
|
+
for (const file of files) {
|
|
58
|
+
const fileData = {
|
|
59
|
+
name: import_path.default.parse(file).name,
|
|
60
|
+
artist: import_path.default.basename(import_path.default.dirname(file)),
|
|
61
|
+
path: file
|
|
62
|
+
};
|
|
63
|
+
filesForFuse.push(fileData);
|
|
57
64
|
}
|
|
58
|
-
return files;
|
|
59
65
|
};
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
66
|
+
if (cacheCallback) {
|
|
67
|
+
const cachedFiles = await cacheCallback(absolutePath, fileExtensions, search);
|
|
68
|
+
await processFiles(cachedFiles);
|
|
69
|
+
} else {
|
|
70
|
+
function* collectFiles(dir) {
|
|
71
|
+
const entries = import_fs.default.readdirSync(dir, { withFileTypes: true });
|
|
72
|
+
let batch = [];
|
|
73
|
+
for (const entry of entries) {
|
|
74
|
+
const fullPath = import_path.default.join(dir, entry.name);
|
|
75
|
+
if (entry.isDirectory()) {
|
|
76
|
+
yield* collectFiles(fullPath);
|
|
77
|
+
} else if (fileExtensions.length == 0 || fileExtensions.some((ext) => entry.name.endsWith(ext))) {
|
|
78
|
+
batch.push(fullPath);
|
|
79
|
+
if (batch.length >= batchSize) {
|
|
80
|
+
yield batch;
|
|
81
|
+
batch = [];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (batch.length > 0)
|
|
86
|
+
yield batch;
|
|
87
|
+
}
|
|
88
|
+
for (const batch of collectFiles(absolutePath))
|
|
89
|
+
await processFiles(batch);
|
|
90
|
+
}
|
|
91
|
+
if (!search || Array.isArray(search) && search.length === 0)
|
|
92
|
+
return filesForFuse.map((file) => file.path);
|
|
63
93
|
const fuseOptions = {
|
|
64
94
|
includeScore: true,
|
|
65
95
|
threshold: 1 - minimumScore,
|
|
66
96
|
// Convert our score to Fuse threshold
|
|
67
|
-
keys: ["name"]
|
|
97
|
+
keys: ["name", "artist"]
|
|
68
98
|
};
|
|
69
|
-
const filesForFuse = allFiles.map((file) => ({
|
|
70
|
-
name: import_path.default.parse(file).name,
|
|
71
|
-
path: file
|
|
72
|
-
}));
|
|
73
99
|
const fuse = new import_fuse.default(filesForFuse, fuseOptions);
|
|
74
|
-
const
|
|
75
|
-
|
|
100
|
+
const searchQueries = Array.isArray(search) ? [{ name: search[0], artist: search[1] }] : [search];
|
|
101
|
+
const searchPromises = searchQueries.map((query) => fuse.search(query));
|
|
102
|
+
const results = await Promise.all(searchPromises);
|
|
103
|
+
const flattenedResults = results.flat();
|
|
104
|
+
return flattenedResults.filter((result) => result.score ? result.score <= 1 - minimumScore : false).map((result) => result.item.path);
|
|
76
105
|
}
|
|
77
106
|
// Annotate the CommonJS export names for ESM import in node:
|
|
78
107
|
0 && (module.exports = {
|
package/dist/index.d.cts
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
|
+
type CacheCallback = (filePath: string, fileExtensions: string[], search?: string[] | string) => Promise<string[]>;
|
|
2
|
+
interface SearchOptions {
|
|
3
|
+
minimumScore?: number;
|
|
4
|
+
batchSize?: number;
|
|
5
|
+
parallelSearches?: number;
|
|
6
|
+
}
|
|
1
7
|
/**
|
|
2
|
-
* Perform a simple folder search based on file names.
|
|
8
|
+
* Perform a simple folder search based on file names and optionally artist names.
|
|
3
9
|
*
|
|
4
10
|
* @param {string} filepath - The directory to search in.
|
|
5
11
|
* @param {string[]} fileExtensions - An array of file extensions to filter by.
|
|
6
|
-
* @param {string} search - The search query.
|
|
7
|
-
* @param {
|
|
8
|
-
* @
|
|
12
|
+
* @param {string | string[]} search - The search query, either a string or an array of [name, artist].
|
|
13
|
+
* @param {SearchOptions} [options] - Optional search options.
|
|
14
|
+
* @param {CacheCallback} [cacheCallback] - Optional callback for custom caching logic.
|
|
15
|
+
* @returns {Promise<string[]>} A promise that resolves to an array of file paths that match the search query.
|
|
9
16
|
*/
|
|
10
|
-
declare function simpleFolderSearch(filepath: string, fileExtensions: string[], search: string,
|
|
17
|
+
declare function simpleFolderSearch(filepath: string, fileExtensions: string[], search: string | string[], options?: SearchOptions, cacheCallback?: CacheCallback): Promise<string[]>;
|
|
11
18
|
|
|
12
19
|
export { simpleFolderSearch };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,19 @@
|
|
|
1
|
+
type CacheCallback = (filePath: string, fileExtensions: string[], search?: string[] | string) => Promise<string[]>;
|
|
2
|
+
interface SearchOptions {
|
|
3
|
+
minimumScore?: number;
|
|
4
|
+
batchSize?: number;
|
|
5
|
+
parallelSearches?: number;
|
|
6
|
+
}
|
|
1
7
|
/**
|
|
2
|
-
* Perform a simple folder search based on file names.
|
|
8
|
+
* Perform a simple folder search based on file names and optionally artist names.
|
|
3
9
|
*
|
|
4
10
|
* @param {string} filepath - The directory to search in.
|
|
5
11
|
* @param {string[]} fileExtensions - An array of file extensions to filter by.
|
|
6
|
-
* @param {string} search - The search query.
|
|
7
|
-
* @param {
|
|
8
|
-
* @
|
|
12
|
+
* @param {string | string[]} search - The search query, either a string or an array of [name, artist].
|
|
13
|
+
* @param {SearchOptions} [options] - Optional search options.
|
|
14
|
+
* @param {CacheCallback} [cacheCallback] - Optional callback for custom caching logic.
|
|
15
|
+
* @returns {Promise<string[]>} A promise that resolves to an array of file paths that match the search query.
|
|
9
16
|
*/
|
|
10
|
-
declare function simpleFolderSearch(filepath: string, fileExtensions: string[], search: string,
|
|
17
|
+
declare function simpleFolderSearch(filepath: string, fileExtensions: string[], search: string | string[], options?: SearchOptions, cacheCallback?: CacheCallback): Promise<string[]>;
|
|
11
18
|
|
|
12
19
|
export { simpleFolderSearch };
|
package/dist/index.js
CHANGED
|
@@ -2,41 +2,70 @@
|
|
|
2
2
|
import fs from "fs";
|
|
3
3
|
import path from "path";
|
|
4
4
|
import Fuse from "fuse.js";
|
|
5
|
-
function simpleFolderSearch(filepath, fileExtensions, search,
|
|
5
|
+
async function simpleFolderSearch(filepath, fileExtensions, search, options = {}, cacheCallback) {
|
|
6
|
+
const {
|
|
7
|
+
minimumScore = 0.6,
|
|
8
|
+
batchSize = 100,
|
|
9
|
+
parallelSearches = 1
|
|
10
|
+
} = options;
|
|
6
11
|
const absolutePath = path.resolve(process.cwd(), filepath);
|
|
7
12
|
if (!fs.existsSync(absolutePath)) throw new Error("Filepath does not exist.");
|
|
8
13
|
if (!Array.isArray(fileExtensions)) throw new Error("fileExtensions must be an array.");
|
|
9
|
-
if (typeof search !== "string") throw new Error("Search query must be a string.");
|
|
14
|
+
if (typeof search !== "string" && !Array.isArray(search)) throw new Error("Search query must be a string or an array.");
|
|
10
15
|
if (typeof minimumScore !== "number") throw new Error("Minimum score must be a number.");
|
|
11
16
|
if (minimumScore < 0 || minimumScore > 1) throw new Error("Minimum score must be between 0 and 1.");
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
if (typeof batchSize !== "number" || batchSize <= 0) throw new Error("Batch size must be a positive number.");
|
|
18
|
+
if (typeof parallelSearches !== "number" || parallelSearches <= 0) throw new Error("Parallel searches must be a positive number.");
|
|
19
|
+
const filesForFuse = [];
|
|
20
|
+
const processFiles = async (files) => {
|
|
21
|
+
for (const file of files) {
|
|
22
|
+
const fileData = {
|
|
23
|
+
name: path.parse(file).name,
|
|
24
|
+
artist: path.basename(path.dirname(file)),
|
|
25
|
+
path: file
|
|
26
|
+
};
|
|
27
|
+
filesForFuse.push(fileData);
|
|
21
28
|
}
|
|
22
|
-
return files;
|
|
23
29
|
};
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
30
|
+
if (cacheCallback) {
|
|
31
|
+
const cachedFiles = await cacheCallback(absolutePath, fileExtensions, search);
|
|
32
|
+
await processFiles(cachedFiles);
|
|
33
|
+
} else {
|
|
34
|
+
function* collectFiles(dir) {
|
|
35
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
36
|
+
let batch = [];
|
|
37
|
+
for (const entry of entries) {
|
|
38
|
+
const fullPath = path.join(dir, entry.name);
|
|
39
|
+
if (entry.isDirectory()) {
|
|
40
|
+
yield* collectFiles(fullPath);
|
|
41
|
+
} else if (fileExtensions.length == 0 || fileExtensions.some((ext) => entry.name.endsWith(ext))) {
|
|
42
|
+
batch.push(fullPath);
|
|
43
|
+
if (batch.length >= batchSize) {
|
|
44
|
+
yield batch;
|
|
45
|
+
batch = [];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (batch.length > 0)
|
|
50
|
+
yield batch;
|
|
51
|
+
}
|
|
52
|
+
for (const batch of collectFiles(absolutePath))
|
|
53
|
+
await processFiles(batch);
|
|
54
|
+
}
|
|
55
|
+
if (!search || Array.isArray(search) && search.length === 0)
|
|
56
|
+
return filesForFuse.map((file) => file.path);
|
|
27
57
|
const fuseOptions = {
|
|
28
58
|
includeScore: true,
|
|
29
59
|
threshold: 1 - minimumScore,
|
|
30
60
|
// Convert our score to Fuse threshold
|
|
31
|
-
keys: ["name"]
|
|
61
|
+
keys: ["name", "artist"]
|
|
32
62
|
};
|
|
33
|
-
const filesForFuse = allFiles.map((file) => ({
|
|
34
|
-
name: path.parse(file).name,
|
|
35
|
-
path: file
|
|
36
|
-
}));
|
|
37
63
|
const fuse = new Fuse(filesForFuse, fuseOptions);
|
|
38
|
-
const
|
|
39
|
-
|
|
64
|
+
const searchQueries = Array.isArray(search) ? [{ name: search[0], artist: search[1] }] : [search];
|
|
65
|
+
const searchPromises = searchQueries.map((query) => fuse.search(query));
|
|
66
|
+
const results = await Promise.all(searchPromises);
|
|
67
|
+
const flattenedResults = results.flat();
|
|
68
|
+
return flattenedResults.filter((result) => result.score ? result.score <= 1 - minimumScore : false).map((result) => result.item.path);
|
|
40
69
|
}
|
|
41
70
|
export {
|
|
42
71
|
simpleFolderSearch
|
package/docs/caching.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# Custom Caching
|
|
2
|
+
|
|
3
|
+
The Simple Folder Search package supports custom caching to improve search performance. You can provide a custom caching method to the `simpleFolderSearch` function using a callback. This allows you to use different caching mechanisms, such as an in-memory cache or a database.
|
|
4
|
+
|
|
5
|
+
## In-Memory Cache Example
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
const cache = {};
|
|
9
|
+
|
|
10
|
+
async function inMemoryCacheCallback(filePath, fileExtensions) {
|
|
11
|
+
if (cache[filePath]) {
|
|
12
|
+
return cache[filePath];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Default file collection logic
|
|
16
|
+
const files = [];
|
|
17
|
+
function collectFiles(dir) {
|
|
18
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
19
|
+
for (const entry of entries) {
|
|
20
|
+
const fullPath = path.join(dir, entry.name);
|
|
21
|
+
if (entry.isDirectory()) {
|
|
22
|
+
collectFiles(fullPath);
|
|
23
|
+
} else if (fileExtensions.length === 0 || fileExtensions.some(ext => entry.name.endsWith(ext))) {
|
|
24
|
+
files.push(fullPath);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
collectFiles(filePath);
|
|
30
|
+
cache[filePath] = files;
|
|
31
|
+
return files;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const results = await simpleFolderSearch("./music", [".mp3", ".wav"], "my song", { minimumScore: 0.6, batchSize: 100 }, inMemoryCacheCallback);
|
|
35
|
+
console.log(results);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Database Cache Example
|
|
39
|
+
|
|
40
|
+
You can use a lightweight database like SQLite for caching.
|
|
41
|
+
|
|
42
|
+
1. Install SQLite:
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
npm install sqlite3
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
2. Indexing Files: Create a script to index files and store them in the database.
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
const sqlite3 = require('sqlite3').verbose();
|
|
52
|
+
const db = new sqlite3.Database('files.db');
|
|
53
|
+
|
|
54
|
+
db.serialize(() => {
|
|
55
|
+
db.run("CREATE TABLE IF NOT EXISTS files (name TEXT, path TEXT, extension TEXT)");
|
|
56
|
+
|
|
57
|
+
const indexFiles = (dir) => {
|
|
58
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
59
|
+
for (const entry of entries) {
|
|
60
|
+
const fullPath = path.join(dir, entry.name);
|
|
61
|
+
if (entry.isDirectory()) {
|
|
62
|
+
indexFiles(fullPath);
|
|
63
|
+
} else {
|
|
64
|
+
const ext = path.extname(entry.name);
|
|
65
|
+
db.run("INSERT INTO files (name, path, extension) VALUES (?, ?, ?)", [entry.name, fullPath, ext]);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
indexFiles("./your-directory");
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
db.close();
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
3. Database Cache Callback: Implement the cache callback to fetch files from the database.
|
|
77
|
+
|
|
78
|
+
This method also pre-checks the search to filter words out of the database results, as the database searches much faster than fuzzy-matching
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
const sqlite3 = require('sqlite3').verbose();
|
|
82
|
+
const db = new sqlite3.Database('files.db');
|
|
83
|
+
|
|
84
|
+
async function dbCacheCallback(filePath, fileExtensions) {
|
|
85
|
+
return new Promise((resolve, reject) => {
|
|
86
|
+
const exts = fileExtensions.map(() => '?').join(',');
|
|
87
|
+
const tokens = (!search ? '1=1' : Array.isArray(search) ? search.join(' '): search).toLowerCase().split(' ').map(x => 'path LIKE "%'+x+'%"').join(' OR ');
|
|
88
|
+
db.all("SELECT path FROM files WHERE extension IN (" + exts + ") AND (" + tokens + ")", fileExtensions, (err, rows) => {
|
|
89
|
+
if (err) {
|
|
90
|
+
reject(err);
|
|
91
|
+
} else {
|
|
92
|
+
resolve(rows.map(row => row.path));
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const results = await simpleFolderSearch("./music", [".mp3", ".wav"], "my song", { minimumScore: 0.6, batchSize: 100 }, dbCacheCallback);
|
|
99
|
+
console.log(results);
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
This approach allows you to customize the caching mechanism according to your needs, whether it's an in-memory cache, a database, or any other storage solution.
|
package/docs/index.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# Simple Folder Search Documentation
|
|
2
|
+
|
|
3
|
+
Welcome to the Simple Folder Search documentation. This package provides a simple way to search for files within a folder. It is designed to be easy to use and integrate into your projects.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
- [Installation](installation.md)
|
|
8
|
+
- [Usage](usage.md)
|
|
9
|
+
- [Options](options.md)
|
|
10
|
+
- [Custom Caching](caching.md)
|
|
11
|
+
- [Types](types.md)
|
package/docs/options.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Options
|
|
2
|
+
|
|
3
|
+
The Simple Folder Search package provides several options to customize the search behavior. Here are the available options:
|
|
4
|
+
|
|
5
|
+
## minimumScore
|
|
6
|
+
|
|
7
|
+
- **Type:** `number`
|
|
8
|
+
- **Default:** `0.6`
|
|
9
|
+
- **Description:** The minimum score required for a file to be considered a match.
|
|
10
|
+
|
|
11
|
+
## batchSize
|
|
12
|
+
|
|
13
|
+
- **Type:** `number`
|
|
14
|
+
- **Default:** `100`
|
|
15
|
+
- **Description:** The number of files to process in each batch.
|
|
16
|
+
|
|
17
|
+
## parallelSearches
|
|
18
|
+
|
|
19
|
+
- **Type:** `number`
|
|
20
|
+
- **Default:** `1`
|
|
21
|
+
- **Description:** The number of parallel searches to perform.
|
package/docs/types.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Types
|
|
2
|
+
|
|
3
|
+
This document describes the types used in the Simple Folder Search package.
|
|
4
|
+
|
|
5
|
+
## SearchOptions
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
interface SearchOptions {
|
|
9
|
+
minimumScore?: number;
|
|
10
|
+
batchSize?: number;
|
|
11
|
+
parallelSearches?: number;
|
|
12
|
+
}
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## CacheCallback
|
|
16
|
+
|
|
17
|
+
A callback function type for custom caching logic.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
type CacheCallback = (filePath: string, fileExtensions: string[], search?: string[] | string) => Promise<string[]>;
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## SearchResult
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
interface SearchResult {
|
|
27
|
+
filePath: string;
|
|
28
|
+
score: number;
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
These types are used to define the options for the search and the structure of the search results.
|
package/docs/usage.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Usage
|
|
2
|
+
|
|
3
|
+
Here is an example of how to use the package to search for files and play a file using `discord-player`'s search method:
|
|
4
|
+
|
|
5
|
+
```javascript
|
|
6
|
+
const { simpleFolderSearch } = require('simple-folder-search');
|
|
7
|
+
const { Player, QueryType } = require('discord-player');
|
|
8
|
+
|
|
9
|
+
// Initialize the player
|
|
10
|
+
const player = new Player(client);
|
|
11
|
+
|
|
12
|
+
// Search for music files in your music folder
|
|
13
|
+
const musicFiles = await simpleFolderSearch('./music', ['.mp3', '.wav'], 'my song', { minimumScore: 0.6 });
|
|
14
|
+
|
|
15
|
+
if (musicFiles.length > 0) {
|
|
16
|
+
// Get the first matching file
|
|
17
|
+
const firstMatch = musicFiles[0];
|
|
18
|
+
|
|
19
|
+
// Search the track using the proper engine
|
|
20
|
+
const research = player.search(firstMatch, {
|
|
21
|
+
searchEngine: QueryType.FILE,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Play the file using discord-player
|
|
25
|
+
await player.play(voiceChannel, research);
|
|
26
|
+
} else {
|
|
27
|
+
console.log('No matching files found');
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Searching by Name and Artist
|
|
32
|
+
|
|
33
|
+
You can also search for files using both the file name and the artist (folder name) by providing the search query as an array. The first element of the array should be the file name, and the second element should be the artist (folder name).
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
const results = await simpleFolderSearch("./music", [".mp3", ".wav"], ["my song", "artist name"], { minimumScore: 0.6, batchSize: 100 });
|
|
37
|
+
console.log(results);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
In this example, the search will look for files named "my song" within folders named "artist name".
|
|
41
|
+
|
|
42
|
+
## Advanced Usage
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
const options = {
|
|
46
|
+
minimumScore: 0.7,
|
|
47
|
+
batchSize: 50,
|
|
48
|
+
parallelSearches: 5,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const advancedResults = await simpleFolderSearch("./music", [".mp3", ".wav"], "advanced search", options);
|
|
52
|
+
console.log(advancedResults);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
In this example, the search will look for files with a minimum score of 0.7, process 50 files at a time and perform 5 parallel searches.
|
package/package.json
CHANGED
|
@@ -2,69 +2,112 @@ import fs from "fs";
|
|
|
2
2
|
import path from "path";
|
|
3
3
|
import Fuse from "fuse.js";
|
|
4
4
|
|
|
5
|
+
type CacheCallback = (filePath: string, fileExtensions: string[], search?: string[] | string) => Promise<string[]>;
|
|
6
|
+
|
|
7
|
+
interface SearchOptions {
|
|
8
|
+
minimumScore?: number;
|
|
9
|
+
batchSize?: number;
|
|
10
|
+
parallelSearches?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
5
13
|
/**
|
|
6
|
-
* Perform a simple folder search based on file names.
|
|
14
|
+
* Perform a simple folder search based on file names and optionally artist names.
|
|
7
15
|
*
|
|
8
16
|
* @param {string} filepath - The directory to search in.
|
|
9
17
|
* @param {string[]} fileExtensions - An array of file extensions to filter by.
|
|
10
|
-
* @param {string} search - The search query.
|
|
11
|
-
* @param {
|
|
12
|
-
* @
|
|
18
|
+
* @param {string | string[]} search - The search query, either a string or an array of [name, artist].
|
|
19
|
+
* @param {SearchOptions} [options] - Optional search options.
|
|
20
|
+
* @param {CacheCallback} [cacheCallback] - Optional callback for custom caching logic.
|
|
21
|
+
* @returns {Promise<string[]>} A promise that resolves to an array of file paths that match the search query.
|
|
13
22
|
*/
|
|
14
|
-
export function simpleFolderSearch(
|
|
23
|
+
export async function simpleFolderSearch(
|
|
24
|
+
filepath: string,
|
|
25
|
+
fileExtensions: string[],
|
|
26
|
+
search: string | string[],
|
|
27
|
+
options: SearchOptions = {},
|
|
28
|
+
cacheCallback?: CacheCallback,
|
|
29
|
+
): Promise<string[]> {
|
|
30
|
+
const {
|
|
31
|
+
minimumScore = 0.6,
|
|
32
|
+
batchSize = 100,
|
|
33
|
+
parallelSearches = 1,
|
|
34
|
+
} = options;
|
|
35
|
+
|
|
15
36
|
const absolutePath = path.resolve(process.cwd(), filepath);
|
|
16
37
|
|
|
17
38
|
if (!fs.existsSync(absolutePath)) throw new Error("Filepath does not exist.");
|
|
18
39
|
if (!Array.isArray(fileExtensions)) throw new Error("fileExtensions must be an array.");
|
|
19
|
-
if (typeof search !== "string") throw new Error("Search query must be a string.");
|
|
40
|
+
if (typeof search !== "string" && !Array.isArray(search)) throw new Error("Search query must be a string or an array.");
|
|
20
41
|
if (typeof minimumScore !== "number") throw new Error("Minimum score must be a number.");
|
|
21
42
|
if (minimumScore < 0 || minimumScore > 1) throw new Error("Minimum score must be between 0 and 1.");
|
|
43
|
+
if (typeof batchSize !== "number" || batchSize <= 0) throw new Error("Batch size must be a positive number.");
|
|
44
|
+
if (typeof parallelSearches !== "number" || parallelSearches <= 0) throw new Error("Parallel searches must be a positive number.");
|
|
22
45
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const fullPath = path.join(dir, entry.name);
|
|
35
|
-
if (entry.isDirectory())
|
|
36
|
-
files = files.concat(collectFiles(fullPath));
|
|
37
|
-
else if (fileExtensions.some(ext => entry.name.endsWith(ext)))
|
|
38
|
-
files.push(fullPath);
|
|
39
|
-
|
|
46
|
+
const filesForFuse: { name: string; artist?: string; path: string}[] = [];
|
|
47
|
+
|
|
48
|
+
const processFiles = async (files: string[]) => {
|
|
49
|
+
for (const file of files) {
|
|
50
|
+
const fileData: { name: string; artist?: string; path: string} = {
|
|
51
|
+
name: path.parse(file).name,
|
|
52
|
+
artist: path.basename(path.dirname(file)),
|
|
53
|
+
path: file,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
filesForFuse.push(fileData);
|
|
40
57
|
}
|
|
41
|
-
return files;
|
|
42
58
|
};
|
|
43
59
|
|
|
44
|
-
|
|
60
|
+
if (cacheCallback) {
|
|
61
|
+
const cachedFiles = await cacheCallback(absolutePath, fileExtensions, search);
|
|
62
|
+
await processFiles(cachedFiles);
|
|
63
|
+
} else {
|
|
64
|
+
function* collectFiles(dir: string): Generator<string[]> {
|
|
65
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
66
|
+
let batch: string[] = [];
|
|
67
|
+
|
|
68
|
+
for (const entry of entries) {
|
|
69
|
+
const fullPath = path.join(dir, entry.name);
|
|
70
|
+
if (entry.isDirectory()) {
|
|
71
|
+
yield* collectFiles(fullPath);
|
|
72
|
+
} else if (fileExtensions.length == 0 || fileExtensions.some(ext => entry.name.endsWith(ext))) {
|
|
73
|
+
batch.push(fullPath);
|
|
74
|
+
if (batch.length >= batchSize) {
|
|
75
|
+
yield batch;
|
|
76
|
+
batch = [];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (batch.length > 0)
|
|
82
|
+
yield batch;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
for (const batch of collectFiles(absolutePath))
|
|
86
|
+
await processFiles(batch);
|
|
87
|
+
|
|
88
|
+
}
|
|
45
89
|
|
|
46
90
|
// If search is empty, return all files with matching extensions
|
|
47
|
-
if (!search.
|
|
48
|
-
return
|
|
49
|
-
|
|
91
|
+
if (!search || (Array.isArray(search) && search.length === 0))
|
|
92
|
+
return filesForFuse.map(file => file.path);
|
|
50
93
|
|
|
51
94
|
// Setup Fuse with options
|
|
52
95
|
const fuseOptions = {
|
|
53
96
|
includeScore: true,
|
|
54
97
|
threshold: 1 - minimumScore, // Convert our score to Fuse threshold
|
|
55
|
-
keys: ["name"],
|
|
98
|
+
keys: ["name", "artist"],
|
|
56
99
|
};
|
|
57
100
|
|
|
58
|
-
const filesForFuse = allFiles.map(file => ({
|
|
59
|
-
name: path.parse(file).name,
|
|
60
|
-
path: file,
|
|
61
|
-
}));
|
|
62
|
-
|
|
63
101
|
const fuse = new Fuse(filesForFuse, fuseOptions);
|
|
64
|
-
|
|
102
|
+
|
|
65
103
|
// Perform search and filter results
|
|
66
|
-
const
|
|
67
|
-
|
|
104
|
+
const searchQueries = Array.isArray(search) ? [{ name: search[0], artist: search[1] }] : [search];
|
|
105
|
+
const searchPromises = searchQueries.map(query => fuse.search(query));
|
|
106
|
+
|
|
107
|
+
const results = await Promise.all(searchPromises);
|
|
108
|
+
const flattenedResults = results.flat();
|
|
109
|
+
|
|
110
|
+
return flattenedResults
|
|
68
111
|
.filter(result => (result.score ? result.score <= (1 - minimumScore) : false))
|
|
69
112
|
.map(result => result.item.path);
|
|
70
113
|
}
|