simple-folder-search 1.0.2 → 2.0.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 +155 -0
- package/dist/index.cjs +43 -22
- package/dist/index.d.cts +7 -4
- package/dist/index.d.ts +7 -4
- package/dist/index.js +43 -22
- package/package.json +1 -1
- package/src/simple-folder-search.ts +59 -34
package/README.md
CHANGED
|
@@ -48,6 +48,161 @@ if (musicFiles.length > 0) {
|
|
|
48
48
|
}
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
+
### Searching by Name and Artist
|
|
52
|
+
|
|
53
|
+
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).
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
const results = await simpleFolderSearch("./music", [".mp3", ".wav"], ["my song", "artist name"], 0.6, 100);
|
|
57
|
+
console.log(results);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
In this example, the search will look for files named "my song" within folders named "artist name".
|
|
61
|
+
|
|
62
|
+
### Normal behaviour
|
|
63
|
+
|
|
64
|
+
Normally, simple-folder-search will go through files loading one batch at a time, which you can configure. A batch of 10, for example, consists of 10 fully loaded files, it will then push the file names to an array and then reset the batch and start again. This ensures that not all files are loaded at once, which could take a lot of memory.
|
|
65
|
+
|
|
66
|
+
The issue with that is that files will always be loaded again each time the function is called.
|
|
67
|
+
|
|
68
|
+
### Custom caching method
|
|
69
|
+
|
|
70
|
+
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.
|
|
71
|
+
|
|
72
|
+
#### In-Memory Cache Example
|
|
73
|
+
|
|
74
|
+
```javascript
|
|
75
|
+
const cache = {};
|
|
76
|
+
|
|
77
|
+
async function inMemoryCacheCallback(filePath, fileExtensions) {
|
|
78
|
+
if (cache[filePath]) {
|
|
79
|
+
return cache[filePath];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Default file collection logic
|
|
83
|
+
const files = [];
|
|
84
|
+
function collectFiles(dir) {
|
|
85
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
86
|
+
for (const entry of entries) {
|
|
87
|
+
const fullPath = path.join(dir, entry.name);
|
|
88
|
+
if (entry.isDirectory()) {
|
|
89
|
+
collectFiles(fullPath);
|
|
90
|
+
} else if (fileExtensions.length === 0 || fileExtensions.some(ext => entry.name.endsWith(ext))) {
|
|
91
|
+
files.push(fullPath);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
collectFiles(filePath);
|
|
97
|
+
cache[filePath] = files;
|
|
98
|
+
return files;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const results = await simpleFolderSearch("./music", [".mp3", ".wav"], "my song", 0.6, 100, inMemoryCacheCallback);
|
|
102
|
+
console.log(results);
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### Database Cache Example
|
|
106
|
+
|
|
107
|
+
You can use a lightweight database like SQLite for caching.
|
|
108
|
+
|
|
109
|
+
1. **Install SQLite**:
|
|
110
|
+
```bash
|
|
111
|
+
npm install sqlite3
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
2. **Indexing Files**:
|
|
115
|
+
Create a script to index files and store them in the database.
|
|
116
|
+
|
|
117
|
+
```javascript
|
|
118
|
+
const sqlite3 = require('sqlite3').verbose();
|
|
119
|
+
const db = new sqlite3.Database('files.db');
|
|
120
|
+
|
|
121
|
+
db.serialize(() => {
|
|
122
|
+
db.run("CREATE TABLE IF NOT EXISTS files (name TEXT, path TEXT, extension TEXT)");
|
|
123
|
+
|
|
124
|
+
const indexFiles = (dir) => {
|
|
125
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
126
|
+
for (const entry of entries) {
|
|
127
|
+
const fullPath = path.join(dir, entry.name);
|
|
128
|
+
if (entry.isDirectory()) {
|
|
129
|
+
indexFiles(fullPath);
|
|
130
|
+
} else {
|
|
131
|
+
const ext = path.extname(entry.name);
|
|
132
|
+
db.run("INSERT INTO files (name, path, extension) VALUES (?, ?, ?)", [entry.name, fullPath, ext]);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
indexFiles("./your-directory");
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
db.close();
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
3. **Database Cache Callback**:
|
|
144
|
+
Implement the cache callback to fetch files from the database.
|
|
145
|
+
|
|
146
|
+
```javascript
|
|
147
|
+
const sqlite3 = require('sqlite3').verbose();
|
|
148
|
+
const db = new sqlite3.Database('files.db');
|
|
149
|
+
|
|
150
|
+
async function dbCacheCallback(filePath, fileExtensions) {
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
db.all("SELECT path FROM files WHERE extension IN (" + fileExtensions.map(() => '?').join(',') + ")", fileExtensions, (err, rows) => {
|
|
153
|
+
if (err) {
|
|
154
|
+
reject(err);
|
|
155
|
+
} else {
|
|
156
|
+
resolve(rows.map(row => row.path));
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const results = await simpleFolderSearch("./music", [".mp3", ".wav"], "my song", 0.6, 100, dbCacheCallback);
|
|
163
|
+
console.log(results);
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
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.
|
|
167
|
+
|
|
168
|
+
## Types
|
|
169
|
+
|
|
170
|
+
The `simpleFolderSearch` function uses the following types:
|
|
171
|
+
|
|
172
|
+
### `CacheCallback`
|
|
173
|
+
|
|
174
|
+
A callback function type for custom caching logic.
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
type CacheCallback = (filePath: string, fileExtensions: string[]) => Promise<string[]>;
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### `simpleFolderSearch`
|
|
181
|
+
|
|
182
|
+
The main function to perform a simple folder search.
|
|
183
|
+
|
|
184
|
+
```typescript
|
|
185
|
+
/**
|
|
186
|
+
* Perform a simple folder search based on file names and optionally artist names.
|
|
187
|
+
*
|
|
188
|
+
* @param {string} filepath - The directory to search in.
|
|
189
|
+
* @param {string[]} fileExtensions - An array of file extensions to filter by.
|
|
190
|
+
* @param {string | string[]} search - The search query, either a string or an array of [name, artist].
|
|
191
|
+
* @param {number} [minimumScore=0.6] - The minimum score for a match to be considered valid.
|
|
192
|
+
* @param {number} [batchSize=10] - The number of files to process in each batch.
|
|
193
|
+
* @param {CacheCallback} [cacheCallback] - Optional callback for custom caching logic.
|
|
194
|
+
* @returns {Promise<string[]>} A promise that resolves to an array of file paths that match the search query.
|
|
195
|
+
*/
|
|
196
|
+
export async function simpleFolderSearch(
|
|
197
|
+
filepath: string,
|
|
198
|
+
fileExtensions: string[],
|
|
199
|
+
search: string | string[],
|
|
200
|
+
minimumScore: number = 0.6,
|
|
201
|
+
batchSize: number = 100,
|
|
202
|
+
cacheCallback?: CacheCallback
|
|
203
|
+
): Promise<string[]>;
|
|
204
|
+
```
|
|
205
|
+
|
|
51
206
|
## License
|
|
52
207
|
|
|
53
208
|
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
|
package/dist/index.cjs
CHANGED
|
@@ -38,40 +38,61 @@ 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, minimumScore = 0.6) {
|
|
41
|
+
async function simpleFolderSearch(filepath, fileExtensions, search, minimumScore = 0.6, batchSize = 100, cacheCallback) {
|
|
42
42
|
const absolutePath = import_path.default.resolve(process.cwd(), filepath);
|
|
43
43
|
if (!import_fs.default.existsSync(absolutePath)) throw new Error("Filepath does not exist.");
|
|
44
44
|
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.");
|
|
45
|
+
if (typeof search !== "string" && !Array.isArray(search)) throw new Error("Search query must be a string or an array.");
|
|
46
46
|
if (typeof minimumScore !== "number") throw new Error("Minimum score must be a number.");
|
|
47
47
|
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
|
-
|
|
48
|
+
if (typeof batchSize !== "number" || batchSize <= 0) throw new Error("Batch size must be a positive number.");
|
|
49
|
+
let filesForFuse = [];
|
|
50
|
+
if (cacheCallback) {
|
|
51
|
+
const cachedFiles = await cacheCallback(absolutePath, fileExtensions);
|
|
52
|
+
filesForFuse = cachedFiles.map((file) => ({
|
|
53
|
+
name: import_path.default.parse(file).name,
|
|
54
|
+
artist: import_path.default.basename(import_path.default.dirname(file)),
|
|
55
|
+
path: file
|
|
56
|
+
}));
|
|
57
|
+
} else {
|
|
58
|
+
function* collectFiles(dir) {
|
|
59
|
+
const entries = import_fs.default.readdirSync(dir, { withFileTypes: true });
|
|
60
|
+
let batch = [];
|
|
61
|
+
for (const entry of entries) {
|
|
62
|
+
const fullPath = import_path.default.join(dir, entry.name);
|
|
63
|
+
if (entry.isDirectory()) {
|
|
64
|
+
yield* collectFiles(fullPath);
|
|
65
|
+
} else if (fileExtensions.length == 0 || fileExtensions.some((ext) => entry.name.endsWith(ext))) {
|
|
66
|
+
batch.push(fullPath);
|
|
67
|
+
if (batch.length >= batchSize) {
|
|
68
|
+
yield batch;
|
|
69
|
+
batch = [];
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (batch.length > 0)
|
|
74
|
+
yield batch;
|
|
57
75
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
76
|
+
for (const batch of collectFiles(absolutePath)) {
|
|
77
|
+
for (const file of batch) {
|
|
78
|
+
filesForFuse.push({
|
|
79
|
+
name: import_path.default.parse(file).name,
|
|
80
|
+
artist: import_path.default.basename(import_path.default.dirname(file)),
|
|
81
|
+
path: file
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (!search || Array.isArray(search) && search.length === 0)
|
|
87
|
+
return filesForFuse.map((file) => file.path);
|
|
63
88
|
const fuseOptions = {
|
|
64
89
|
includeScore: true,
|
|
65
90
|
threshold: 1 - minimumScore,
|
|
66
91
|
// Convert our score to Fuse threshold
|
|
67
|
-
keys: ["name"]
|
|
92
|
+
keys: ["name", "artist"]
|
|
68
93
|
};
|
|
69
|
-
const filesForFuse = allFiles.map((file) => ({
|
|
70
|
-
name: import_path.default.parse(file).name,
|
|
71
|
-
path: file
|
|
72
|
-
}));
|
|
73
94
|
const fuse = new import_fuse.default(filesForFuse, fuseOptions);
|
|
74
|
-
const results = fuse.search(search);
|
|
95
|
+
const results = Array.isArray(search) ? fuse.search({ name: search[0], artist: search[1] }) : fuse.search(search);
|
|
75
96
|
return results.filter((result) => result.score ? result.score <= 1 - minimumScore : false).map((result) => result.item.path);
|
|
76
97
|
}
|
|
77
98
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/index.d.cts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
+
type CacheCallback = (filePath: string, fileExtensions: string[]) => Promise<string[]>;
|
|
1
2
|
/**
|
|
2
|
-
* Perform a simple folder search based on file names.
|
|
3
|
+
* Perform a simple folder search based on file names and optionally artist names.
|
|
3
4
|
*
|
|
4
5
|
* @param {string} filepath - The directory to search in.
|
|
5
6
|
* @param {string[]} fileExtensions - An array of file extensions to filter by.
|
|
6
|
-
* @param {string} search - The search query.
|
|
7
|
+
* @param {string | string[]} search - The search query, either a string or an array of [name, artist].
|
|
7
8
|
* @param {number} [minimumScore=0.6] - The minimum score for a match to be considered valid.
|
|
8
|
-
* @
|
|
9
|
+
* @param {number} [batchSize=10] - The number of files to process in each batch.
|
|
10
|
+
* @param {CacheCallback} [cacheCallback] - Optional callback for custom caching logic.
|
|
11
|
+
* @returns {Promise<string[]>} A promise that resolves to an array of file paths that match the search query.
|
|
9
12
|
*/
|
|
10
|
-
declare function simpleFolderSearch(filepath: string, fileExtensions: string[], search: string, minimumScore?: number): string[]
|
|
13
|
+
declare function simpleFolderSearch(filepath: string, fileExtensions: string[], search: string | string[], minimumScore?: number, batchSize?: number, cacheCallback?: CacheCallback): Promise<string[]>;
|
|
11
14
|
|
|
12
15
|
export { simpleFolderSearch };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
+
type CacheCallback = (filePath: string, fileExtensions: string[]) => Promise<string[]>;
|
|
1
2
|
/**
|
|
2
|
-
* Perform a simple folder search based on file names.
|
|
3
|
+
* Perform a simple folder search based on file names and optionally artist names.
|
|
3
4
|
*
|
|
4
5
|
* @param {string} filepath - The directory to search in.
|
|
5
6
|
* @param {string[]} fileExtensions - An array of file extensions to filter by.
|
|
6
|
-
* @param {string} search - The search query.
|
|
7
|
+
* @param {string | string[]} search - The search query, either a string or an array of [name, artist].
|
|
7
8
|
* @param {number} [minimumScore=0.6] - The minimum score for a match to be considered valid.
|
|
8
|
-
* @
|
|
9
|
+
* @param {number} [batchSize=10] - The number of files to process in each batch.
|
|
10
|
+
* @param {CacheCallback} [cacheCallback] - Optional callback for custom caching logic.
|
|
11
|
+
* @returns {Promise<string[]>} A promise that resolves to an array of file paths that match the search query.
|
|
9
12
|
*/
|
|
10
|
-
declare function simpleFolderSearch(filepath: string, fileExtensions: string[], search: string, minimumScore?: number): string[]
|
|
13
|
+
declare function simpleFolderSearch(filepath: string, fileExtensions: string[], search: string | string[], minimumScore?: number, batchSize?: number, cacheCallback?: CacheCallback): Promise<string[]>;
|
|
11
14
|
|
|
12
15
|
export { simpleFolderSearch };
|
package/dist/index.js
CHANGED
|
@@ -2,40 +2,61 @@
|
|
|
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, minimumScore = 0.6) {
|
|
5
|
+
async function simpleFolderSearch(filepath, fileExtensions, search, minimumScore = 0.6, batchSize = 100, cacheCallback) {
|
|
6
6
|
const absolutePath = path.resolve(process.cwd(), filepath);
|
|
7
7
|
if (!fs.existsSync(absolutePath)) throw new Error("Filepath does not exist.");
|
|
8
8
|
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.");
|
|
9
|
+
if (typeof search !== "string" && !Array.isArray(search)) throw new Error("Search query must be a string or an array.");
|
|
10
10
|
if (typeof minimumScore !== "number") throw new Error("Minimum score must be a number.");
|
|
11
11
|
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
|
-
|
|
12
|
+
if (typeof batchSize !== "number" || batchSize <= 0) throw new Error("Batch size must be a positive number.");
|
|
13
|
+
let filesForFuse = [];
|
|
14
|
+
if (cacheCallback) {
|
|
15
|
+
const cachedFiles = await cacheCallback(absolutePath, fileExtensions);
|
|
16
|
+
filesForFuse = cachedFiles.map((file) => ({
|
|
17
|
+
name: path.parse(file).name,
|
|
18
|
+
artist: path.basename(path.dirname(file)),
|
|
19
|
+
path: file
|
|
20
|
+
}));
|
|
21
|
+
} else {
|
|
22
|
+
function* collectFiles(dir) {
|
|
23
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
24
|
+
let batch = [];
|
|
25
|
+
for (const entry of entries) {
|
|
26
|
+
const fullPath = path.join(dir, entry.name);
|
|
27
|
+
if (entry.isDirectory()) {
|
|
28
|
+
yield* collectFiles(fullPath);
|
|
29
|
+
} else if (fileExtensions.length == 0 || fileExtensions.some((ext) => entry.name.endsWith(ext))) {
|
|
30
|
+
batch.push(fullPath);
|
|
31
|
+
if (batch.length >= batchSize) {
|
|
32
|
+
yield batch;
|
|
33
|
+
batch = [];
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (batch.length > 0)
|
|
38
|
+
yield batch;
|
|
21
39
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
40
|
+
for (const batch of collectFiles(absolutePath)) {
|
|
41
|
+
for (const file of batch) {
|
|
42
|
+
filesForFuse.push({
|
|
43
|
+
name: path.parse(file).name,
|
|
44
|
+
artist: path.basename(path.dirname(file)),
|
|
45
|
+
path: file
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (!search || Array.isArray(search) && search.length === 0)
|
|
51
|
+
return filesForFuse.map((file) => file.path);
|
|
27
52
|
const fuseOptions = {
|
|
28
53
|
includeScore: true,
|
|
29
54
|
threshold: 1 - minimumScore,
|
|
30
55
|
// Convert our score to Fuse threshold
|
|
31
|
-
keys: ["name"]
|
|
56
|
+
keys: ["name", "artist"]
|
|
32
57
|
};
|
|
33
|
-
const filesForFuse = allFiles.map((file) => ({
|
|
34
|
-
name: path.parse(file).name,
|
|
35
|
-
path: file
|
|
36
|
-
}));
|
|
37
58
|
const fuse = new Fuse(filesForFuse, fuseOptions);
|
|
38
|
-
const results = fuse.search(search);
|
|
59
|
+
const results = Array.isArray(search) ? fuse.search({ name: search[0], artist: search[1] }) : fuse.search(search);
|
|
39
60
|
return results.filter((result) => result.score ? result.score <= 1 - minimumScore : false).map((result) => result.item.path);
|
|
40
61
|
}
|
|
41
62
|
export {
|
package/package.json
CHANGED
|
@@ -2,68 +2,93 @@ 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[]) => Promise<string[]>;
|
|
6
|
+
|
|
5
7
|
/**
|
|
6
|
-
* Perform a simple folder search based on file names.
|
|
8
|
+
* Perform a simple folder search based on file names and optionally artist names.
|
|
7
9
|
*
|
|
8
10
|
* @param {string} filepath - The directory to search in.
|
|
9
11
|
* @param {string[]} fileExtensions - An array of file extensions to filter by.
|
|
10
|
-
* @param {string} search - The search query.
|
|
12
|
+
* @param {string | string[]} search - The search query, either a string or an array of [name, artist].
|
|
11
13
|
* @param {number} [minimumScore=0.6] - The minimum score for a match to be considered valid.
|
|
12
|
-
* @
|
|
14
|
+
* @param {number} [batchSize=10] - The number of files to process in each batch.
|
|
15
|
+
* @param {CacheCallback} [cacheCallback] - Optional callback for custom caching logic.
|
|
16
|
+
* @returns {Promise<string[]>} A promise that resolves to an array of file paths that match the search query.
|
|
13
17
|
*/
|
|
14
|
-
export function simpleFolderSearch(
|
|
18
|
+
export async function simpleFolderSearch(
|
|
19
|
+
filepath: string,
|
|
20
|
+
fileExtensions: string[],
|
|
21
|
+
search: string | string[],
|
|
22
|
+
minimumScore: number = 0.6,
|
|
23
|
+
batchSize: number = 100,
|
|
24
|
+
cacheCallback?: CacheCallback,
|
|
25
|
+
): Promise<string[]> {
|
|
15
26
|
const absolutePath = path.resolve(process.cwd(), filepath);
|
|
16
27
|
|
|
17
28
|
if (!fs.existsSync(absolutePath)) throw new Error("Filepath does not exist.");
|
|
18
29
|
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.");
|
|
30
|
+
if (typeof search !== "string" && !Array.isArray(search)) throw new Error("Search query must be a string or an array.");
|
|
20
31
|
if (typeof minimumScore !== "number") throw new Error("Minimum score must be a number.");
|
|
21
32
|
if (minimumScore < 0 || minimumScore > 1) throw new Error("Minimum score must be between 0 and 1.");
|
|
33
|
+
if (typeof batchSize !== "number" || batchSize <= 0) throw new Error("Batch size must be a positive number.");
|
|
34
|
+
|
|
35
|
+
let filesForFuse: { name: string; artist?: string; path: string }[] = [];
|
|
22
36
|
|
|
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);
|
|
37
|
+
if (cacheCallback) {
|
|
38
|
+
const cachedFiles = await cacheCallback(absolutePath, fileExtensions);
|
|
39
|
+
filesForFuse = cachedFiles.map(file => ({
|
|
40
|
+
name: path.parse(file).name,
|
|
41
|
+
artist: path.basename(path.dirname(file)),
|
|
42
|
+
path: file,
|
|
43
|
+
}));
|
|
44
|
+
} else {
|
|
45
|
+
function* collectFiles(dir: string): Generator<string[]> {
|
|
46
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
47
|
+
let batch: string[] = [];
|
|
39
48
|
|
|
49
|
+
for (const entry of entries) {
|
|
50
|
+
const fullPath = path.join(dir, entry.name);
|
|
51
|
+
if (entry.isDirectory()) {
|
|
52
|
+
yield* collectFiles(fullPath);
|
|
53
|
+
} else if (fileExtensions.length == 0 || fileExtensions.some(ext => entry.name.endsWith(ext))) {
|
|
54
|
+
batch.push(fullPath);
|
|
55
|
+
if (batch.length >= batchSize) {
|
|
56
|
+
yield batch;
|
|
57
|
+
batch = [];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (batch.length > 0)
|
|
63
|
+
yield batch;
|
|
40
64
|
}
|
|
41
|
-
return files;
|
|
42
|
-
};
|
|
43
65
|
|
|
44
|
-
|
|
66
|
+
for (const batch of collectFiles(absolutePath)) {
|
|
67
|
+
for (const file of batch) {
|
|
68
|
+
filesForFuse.push({
|
|
69
|
+
name: path.parse(file).name,
|
|
70
|
+
artist: path.basename(path.dirname(file)),
|
|
71
|
+
path: file,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
45
76
|
|
|
46
77
|
// If search is empty, return all files with matching extensions
|
|
47
|
-
if (!search.
|
|
48
|
-
return
|
|
49
|
-
|
|
78
|
+
if (!search || (Array.isArray(search) && search.length === 0))
|
|
79
|
+
return filesForFuse.map(file => file.path);
|
|
50
80
|
|
|
51
81
|
// Setup Fuse with options
|
|
52
82
|
const fuseOptions = {
|
|
53
83
|
includeScore: true,
|
|
54
84
|
threshold: 1 - minimumScore, // Convert our score to Fuse threshold
|
|
55
|
-
keys: ["name"],
|
|
85
|
+
keys: ["name", "artist"],
|
|
56
86
|
};
|
|
57
87
|
|
|
58
|
-
const filesForFuse = allFiles.map(file => ({
|
|
59
|
-
name: path.parse(file).name,
|
|
60
|
-
path: file,
|
|
61
|
-
}));
|
|
62
|
-
|
|
63
88
|
const fuse = new Fuse(filesForFuse, fuseOptions);
|
|
64
89
|
|
|
65
90
|
// Perform search and filter results
|
|
66
|
-
const results = fuse.search(search);
|
|
91
|
+
const results = Array.isArray(search) ? fuse.search({ name: search[0], artist: search[1] }) : fuse.search(search);
|
|
67
92
|
return results
|
|
68
93
|
.filter(result => (result.score ? result.score <= (1 - minimumScore) : false))
|
|
69
94
|
.map(result => result.item.path);
|