simple-folder-search 1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 iTsMaaT
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # Simple Folder Search
2
+
3
+ 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
+ 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
12
+
13
+ ## Installation
14
+
15
+ To install the package, use the following command:
16
+
17
+ ```bash
18
+ npm install simple-folder-search
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ Here is an example of how to use the package to search for files and play a file using `discord-player`'s search method:
24
+
25
+ ```javascript
26
+ const simpleFolderSearch = require('simple-folder-search');
27
+ const { Player, QueryType } = require('discord-player');
28
+
29
+ // Initialize the player
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
+ }
49
+ ```
50
+
51
+ ## License
52
+
53
+ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
54
+
55
+ ## Contributing
56
+
57
+ Contributions are welcome! Please open an issue or submit a pull request for any changes.
58
+
59
+ ## Contact
60
+
61
+ For any questions or inquiries, please contact [your-email@example.com].
package/dist/index.cjs ADDED
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ default: () => index_default
34
+ });
35
+ module.exports = __toCommonJS(index_exports);
36
+
37
+ // src/simple-folder-search.ts
38
+ var import_fs = __toESM(require("fs"), 1);
39
+ var import_path = __toESM(require("path"), 1);
40
+ var import_fuse = __toESM(require("fuse.js"), 1);
41
+ function simpleFolderSearch(filepath, fileExtensions, search, minimumScore = 0.6) {
42
+ const absolutePath = import_path.default.resolve(process.cwd(), filepath);
43
+ if (!import_fs.default.existsSync(absolutePath)) throw new Error("Filepath does not exist.");
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.");
46
+ if (typeof minimumScore !== "number") throw new Error("Minimum score must be a number.");
47
+ if (minimumScore < 0 || minimumScore > 1) throw new Error("Minimum score must be between 0 and 1.");
48
+ const collectFiles = (dir) => {
49
+ const entries = import_fs.default.readdirSync(dir, { withFileTypes: true });
50
+ let files = [];
51
+ for (const entry of entries) {
52
+ const fullPath = import_path.default.join(dir, entry.name);
53
+ if (entry.isDirectory())
54
+ files = files.concat(collectFiles(fullPath));
55
+ else if (fileExtensions.some((ext) => entry.name.endsWith(ext)))
56
+ files.push(fullPath);
57
+ }
58
+ return files;
59
+ };
60
+ const allFiles = collectFiles(absolutePath);
61
+ if (!search.trim())
62
+ return allFiles;
63
+ const fuseOptions = {
64
+ includeScore: true,
65
+ threshold: 1 - minimumScore,
66
+ // Convert our score to Fuse threshold
67
+ keys: ["name"]
68
+ };
69
+ const filesForFuse = allFiles.map((file) => ({
70
+ name: import_path.default.parse(file).name,
71
+ path: file
72
+ }));
73
+ const fuse = new import_fuse.default(filesForFuse, fuseOptions);
74
+ const results = fuse.search(search);
75
+ return results.filter((result) => result.score ? result.score <= 1 - minimumScore : false).map((result) => result.item.path);
76
+ }
77
+
78
+ // src/index.ts
79
+ var index_default = simpleFolderSearch;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Perform a simple folder search based on file names.
3
+ *
4
+ * @param {string} filepath - The directory to search in.
5
+ * @param {string[]} fileExtensions - An array of file extensions to filter by.
6
+ * @param {string} search - The search query.
7
+ * @param {number} [minimumScore=0.6] - The minimum score for a match to be considered valid.
8
+ * @returns {string[]} An array of file paths that match the search query.
9
+ */
10
+ declare function simpleFolderSearch(filepath: string, fileExtensions: string[], search: string, minimumScore?: number): string[];
11
+
12
+ export { simpleFolderSearch as default };
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Perform a simple folder search based on file names.
3
+ *
4
+ * @param {string} filepath - The directory to search in.
5
+ * @param {string[]} fileExtensions - An array of file extensions to filter by.
6
+ * @param {string} search - The search query.
7
+ * @param {number} [minimumScore=0.6] - The minimum score for a match to be considered valid.
8
+ * @returns {string[]} An array of file paths that match the search query.
9
+ */
10
+ declare function simpleFolderSearch(filepath: string, fileExtensions: string[], search: string, minimumScore?: number): string[];
11
+
12
+ export { simpleFolderSearch as default };
package/dist/index.js ADDED
@@ -0,0 +1,46 @@
1
+ // src/simple-folder-search.ts
2
+ import fs from "fs";
3
+ import path from "path";
4
+ import Fuse from "fuse.js";
5
+ function simpleFolderSearch(filepath, fileExtensions, search, minimumScore = 0.6) {
6
+ const absolutePath = path.resolve(process.cwd(), filepath);
7
+ if (!fs.existsSync(absolutePath)) throw new Error("Filepath does not exist.");
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.");
10
+ if (typeof minimumScore !== "number") throw new Error("Minimum score must be a number.");
11
+ if (minimumScore < 0 || minimumScore > 1) throw new Error("Minimum score must be between 0 and 1.");
12
+ const collectFiles = (dir) => {
13
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
14
+ let files = [];
15
+ for (const entry of entries) {
16
+ const fullPath = path.join(dir, entry.name);
17
+ if (entry.isDirectory())
18
+ files = files.concat(collectFiles(fullPath));
19
+ else if (fileExtensions.some((ext) => entry.name.endsWith(ext)))
20
+ files.push(fullPath);
21
+ }
22
+ return files;
23
+ };
24
+ const allFiles = collectFiles(absolutePath);
25
+ if (!search.trim())
26
+ return allFiles;
27
+ const fuseOptions = {
28
+ includeScore: true,
29
+ threshold: 1 - minimumScore,
30
+ // Convert our score to Fuse threshold
31
+ keys: ["name"]
32
+ };
33
+ const filesForFuse = allFiles.map((file) => ({
34
+ name: path.parse(file).name,
35
+ path: file
36
+ }));
37
+ const fuse = new Fuse(filesForFuse, fuseOptions);
38
+ const results = fuse.search(search);
39
+ return results.filter((result) => result.score ? result.score <= 1 - minimumScore : false).map((result) => result.item.path);
40
+ }
41
+
42
+ // src/index.ts
43
+ var index_default = simpleFolderSearch;
44
+ export {
45
+ index_default as default
46
+ };
@@ -0,0 +1,105 @@
1
+ import typescriptEslint from "@typescript-eslint/eslint-plugin";
2
+ import tsParser from "@typescript-eslint/parser";
3
+ import path from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ import js from "@eslint/js";
6
+ import { FlatCompat } from "@eslint/eslintrc";
7
+
8
+ const __filename = fileURLToPath(import.meta.url);
9
+ const __dirname = path.dirname(__filename);
10
+ const compat = new FlatCompat({
11
+ baseDirectory: __dirname,
12
+ recommendedConfig: js.configs.recommended,
13
+ allConfig: js.configs.all
14
+ });
15
+
16
+ export default [...compat.extends(
17
+ "eslint:recommended",
18
+ "plugin:@typescript-eslint/recommended",
19
+ //"plugin:@typescript-eslint/recommended-requiring-type-checking",
20
+ ), {
21
+ ignores: [
22
+ "dist/*",
23
+ "node_modules",
24
+ "eslint.config.mjs",
25
+ "tsup.config.ts",
26
+ "tinker.ts"
27
+ ],
28
+ plugins: {
29
+ "@typescript-eslint": typescriptEslint,
30
+ },
31
+ languageOptions: {
32
+ parser: tsParser,
33
+ ecmaVersion: 2023,
34
+ sourceType: "module",
35
+
36
+ parserOptions: {
37
+ project: "./tsconfig.json",
38
+ },
39
+ },
40
+
41
+ rules: {
42
+ "@typescript-eslint/no-unused-vars": "off",
43
+ "@typescript-eslint/no-require-imports": "off",
44
+ indent: ["error", 4, {
45
+ SwitchCase: 1,
46
+ }],
47
+
48
+ semi: "error",
49
+ "prefer-const": "error",
50
+ "no-undef": "off",
51
+ "no-unused-vars": "off",
52
+
53
+ "arrow-spacing": ["warn", {
54
+ before: true,
55
+ after: true,
56
+ }],
57
+
58
+ "comma-dangle": ["error", "always-multiline"],
59
+ "comma-spacing": "error",
60
+ "comma-style": "error",
61
+ curly: ["error", "multi-or-nest", "consistent"],
62
+ "dot-location": ["error", "property"],
63
+ "handle-callback-err": "off",
64
+ "keyword-spacing": "error",
65
+
66
+ "max-nested-callbacks": ["error", {
67
+ max: 3,
68
+ }],
69
+
70
+ "max-statements-per-line": ["error", {
71
+ max: 3,
72
+ }],
73
+
74
+ "no-console": "off",
75
+ "no-empty-function": "error",
76
+ "no-floating-decimal": "error",
77
+ "no-lonely-if": "error",
78
+
79
+ "no-multiple-empty-lines": ["error", {
80
+ max: 2,
81
+ maxEOF: 1,
82
+ maxBOF: 0,
83
+ }],
84
+
85
+ "no-shadow": ["error", {
86
+ allow: ["err", "error", "e", "resolve", "reject", "buffer"],
87
+ }],
88
+
89
+ "no-var": "error",
90
+ "object-curly-spacing": ["error", "always"],
91
+ quotes: ["error", "double"],
92
+ "space-before-blocks": "error",
93
+
94
+ "space-before-function-paren": ["error", {
95
+ anonymous: "never",
96
+ named: "never",
97
+ asyncArrow: "always",
98
+ }],
99
+
100
+ "space-in-parens": "error",
101
+ "space-infix-ops": "error",
102
+ "spaced-comment": "error",
103
+ yoda: "error",
104
+ },
105
+ }];
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "simple-folder-search",
3
+ "version": "1.0.0",
4
+ "description": "A simple folder search package",
5
+ "main": "dist/index.cjs",
6
+ "exports": {
7
+ "import": "./dist/index.mjs",
8
+ "require": "./dist/index.cjs"
9
+ },
10
+ "type": "module",
11
+ "scripts": {
12
+ "test": "echo \"Error: no test specified\" && exit 1",
13
+ "build": "tsup src/index.ts --format esm,cjs --dts --out-dir dist",
14
+ "code": "git pull && npm i && code . && eslint src",
15
+ "c": "npm run code"
16
+ },
17
+ "author": "iTsMaaT",
18
+ "license": "MIT",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/iTsMaaT/simple-folder-search.git"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/iTsMaaT/simple-folder-search.git"
25
+ },
26
+ "homepage": "https://github.com/iTsMaaT/simple-folder-search.git",
27
+ "keywords": [
28
+ "folder",
29
+ "file",
30
+ "search",
31
+ "find"
32
+ ],
33
+ "devDependencies": {
34
+ "@types/node": "^22.10.2",
35
+ "@typescript-eslint/eslint-plugin": "^8.18.1",
36
+ "@typescript-eslint/parser": "^8.18.1",
37
+ "eslint": "^9.17.0",
38
+ "globals": "^15.13.0",
39
+ "tsup": "^8.3.5",
40
+ "tsx": "^4.19.2",
41
+ "typescript": "^5.7.2"
42
+ },
43
+ "dependencies": {
44
+ "fuse.js": "^7.0.0"
45
+ }
46
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ import simpleFolderSearch from "./simple-folder-search";
2
+
3
+ export default simpleFolderSearch;
@@ -0,0 +1,70 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import Fuse from "fuse.js";
4
+
5
+ /**
6
+ * Perform a simple folder search based on file names.
7
+ *
8
+ * @param {string} filepath - The directory to search in.
9
+ * @param {string[]} fileExtensions - An array of file extensions to filter by.
10
+ * @param {string} search - The search query.
11
+ * @param {number} [minimumScore=0.6] - The minimum score for a match to be considered valid.
12
+ * @returns {string[]} An array of file paths that match the search query.
13
+ */
14
+ export default function simpleFolderSearch(filepath: string, fileExtensions: string[], search: string, minimumScore: number = 0.6): string[] {
15
+ const absolutePath = path.resolve(process.cwd(), filepath);
16
+
17
+ if (!fs.existsSync(absolutePath)) throw new Error("Filepath does not exist.");
18
+ 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.");
20
+ if (typeof minimumScore !== "number") throw new Error("Minimum score must be a number.");
21
+ if (minimumScore < 0 || minimumScore > 1) throw new Error("Minimum score must be between 0 and 1.");
22
+
23
+ /**
24
+ * Recursively collects all files in a directory and its subdirectories.
25
+ *
26
+ * @param {string} dir - The directory to collect files from.
27
+ * @returns {string[]} An array of file paths.
28
+ */
29
+ const collectFiles = (dir: string): string[] => {
30
+ const entries = fs.readdirSync(dir, { withFileTypes: true });
31
+ let files: string[] = [];
32
+
33
+ for (const entry of entries) {
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
+
40
+ }
41
+ return files;
42
+ };
43
+
44
+ const allFiles = collectFiles(absolutePath);
45
+
46
+ // If search is empty, return all files with matching extensions
47
+ if (!search.trim())
48
+ return allFiles;
49
+
50
+
51
+ // Setup Fuse with options
52
+ const fuseOptions = {
53
+ includeScore: true,
54
+ threshold: 1 - minimumScore, // Convert our score to Fuse threshold
55
+ keys: ["name"],
56
+ };
57
+
58
+ const filesForFuse = allFiles.map(file => ({
59
+ name: path.parse(file).name,
60
+ path: file,
61
+ }));
62
+
63
+ const fuse = new Fuse(filesForFuse, fuseOptions);
64
+
65
+ // Perform search and filter results
66
+ const results = fuse.search(search);
67
+ return results
68
+ .filter(result => (result.score ? result.score <= (1 - minimumScore) : false))
69
+ .map(result => result.item.path);
70
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from "tsup"
2
+
3
+ export default defineConfig({
4
+ format: ["esm","cjs"],
5
+ entry: ['./lib/index.ts'],
6
+ outDir: "./dist",
7
+ skipNodeModulesBundle: true,
8
+ dts: true
9
+ });