@gld5000-cli/dependency-finder 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) 2026 Gareth L Devlin
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,66 @@
1
+ # [@gld5000-cli/dependency-finder](https://www.npmjs.com/package/@gld5000-cli/dependency-finder)
2
+
3
+ Finds how many dependents your components have.
4
+
5
+ ## Install
6
+
7
+ ```
8
+ npm i -D @gld5000-cli/dependency-finder
9
+ ```
10
+
11
+ ## Example Usage
12
+
13
+ ### Import (.mjs)
14
+
15
+ ```
16
+ import * as dependencyFinder from '@gld5000-cli/dependency-finder'
17
+ ```
18
+
19
+ ### Example Input
20
+
21
+ ```
22
+ Add your code here...
23
+ ```
24
+
25
+ ### Example Output
26
+
27
+ ```
28
+ Add your code here...
29
+ ```
30
+
31
+ ## Update
32
+
33
+ ```
34
+ npm update @gld5000-cli/dependency-finder
35
+ ```
36
+
37
+ ## Uninstall
38
+
39
+ ```
40
+ npm uninstall @gld5000-cli/dependency-finder
41
+ ```
42
+
43
+
44
+ ## License
45
+
46
+ MIT License
47
+
48
+ Copyright (c) 2026 Gareth L Devlin
49
+
50
+ Permission is hereby granted, free of charge, to any person obtaining a copy
51
+ of this software and associated documentation files (the "Software"), to deal
52
+ in the Software without restriction, including without limitation the rights
53
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
54
+ copies of the Software, and to permit persons to whom the Software is
55
+ furnished to do so, subject to the following conditions:
56
+
57
+ The above copyright notice and this permission notice shall be included in all
58
+ copies or substantial portions of the Software.
59
+
60
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
61
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
62
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
63
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
64
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
65
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
66
+ SOFTWARE.
package/bin/index.mjs ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { runDependencyFinder } from "../src/index.mjs";
4
+ import { answerStringQuestion } from "@gld5000-cli/readline";
5
+
6
+ const args = process.argv.slice(2);
7
+ const defaultSearchPattern = "./components/**/*.tsx";
8
+ const searchPattern =
9
+ args[0] ||
10
+ (await answerStringQuestion(
11
+ "Where are your components?",
12
+ defaultSearchPattern,
13
+ ));
14
+
15
+ const defaultTargetPaths = "./components/**/*.tsx|./pages/**/*.tsx";
16
+ const targetPaths = `${
17
+ args[1] ||
18
+ (await answerStringQuestion(
19
+ "Which folders should we check for imports (pipe-separated)?",
20
+ defaultTargetPaths,
21
+ ))
22
+ }`.split("|");
23
+
24
+ const defaultIgnorePatterns = ".test|.stories";
25
+ const ignorePatterns = `${
26
+ args[2] ||
27
+ (await answerStringQuestion(
28
+ "Which filename patterns should be ignored (pipe-separated)?",
29
+ defaultIgnorePatterns,
30
+ ))
31
+ }`.split("|");
32
+ runDependencyFinder(searchPattern, targetPaths, ignorePatterns);
@@ -0,0 +1 @@
1
+ {"noDependents":{"count":2,"results":[{"matches":[],"filePath":"test\\test-components\\index.tsx","dependents":[]},{"matches":["ComponentC"],"filePath":"test\\test-components\\folder-c\\test3.tsx","dependents":[]}]},"someDependents":{"count":2,"results":[{"matches":["ComponentB"],"filePath":"test\\test-components\\folder-b\\test2.tsx","dependents":[{"filePath":"test\\test-components\\index.tsx","matches":["import { ComponentB } from \"./folder-b/test2\""]}]},{"matches":["ComponentA"],"filePath":"test\\test-components\\folder-a\\test1.tsx","dependents":[{"filePath":"test\\test-components\\index.tsx","matches":["import ComponentA from \"./folder-a/test1\"","import { ComponentA } from \"./folder-a/test1\""]}]}]}}
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@gld5000-cli/dependency-finder",
3
+ "version": "1.0.0",
4
+ "description": "Finds how many dependents your components have.",
5
+ "keywords": [
6
+ "CLI",
7
+ "NODE",
8
+ "Search",
9
+ "Local",
10
+ "Repo",
11
+ "Deprecation",
12
+ "Dependents"
13
+ ],
14
+ "homepage": "https://github.com/GLD5000/dependency-finder#readme",
15
+ "bugs": {
16
+ "url": "https://github.com/GLD5000/dependency-finder/issues"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/GLD5000/dependency-finder.git"
21
+ },
22
+ "license": "MIT",
23
+ "author": "GLD5000",
24
+ "type": "commonjs",
25
+ "main": "src/index.mjs",
26
+ "scripts": {
27
+ "test": "node test/test.mjs"
28
+ },
29
+ "dependencies": {
30
+ "@gld5000-cli/readline": "^1.0.6"
31
+ }
32
+ }
@@ -0,0 +1,11 @@
1
+ export function filterNoDependents(componentObjectArray) {
2
+ return componentObjectArray.filter(
3
+ (componentObject) => componentObject.dependents.length === 0
4
+ )
5
+ }
6
+
7
+ export function filterSomeDependents(componentObjectArray) {
8
+ return componentObjectArray.filter(
9
+ (componentObject) => componentObject.dependents.length > 0
10
+ )
11
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ *
3
+ * @param {string[]} array
4
+ * @param {string[]} conditions
5
+ * @returns
6
+ */
7
+ export function filterFilesNot(array, conditions) {
8
+ return array.filter(
9
+ (item) => !conditions.some((condition) => item.indexOf(condition) > -1)
10
+ )
11
+ }
@@ -0,0 +1,16 @@
1
+ import { filterFilesNot } from "./filterFiles.mjs";
2
+ import { findFiles } from "./findFiles.mjs";
3
+ import { searchFiles } from "./searchFiles.mjs";
4
+
5
+ /**
6
+ * Finds component exports within a folder's subfolders and returns an nested arrays of paths and component exports
7
+ * @param {string} inputPath
8
+ * @returns {[path: string, matches: string[]]}
9
+ */
10
+ export function findComponents(inputPath, ignorePatterns) {
11
+ return searchFiles(
12
+ filterFilesNot(findFiles(inputPath), ignorePatterns),
13
+ /(export default function|export const|export default) ([a-zA-Z]+)/g,
14
+ 2,
15
+ );
16
+ }
@@ -0,0 +1,73 @@
1
+ import { readFileContent } from "./readFileContent.mjs";
2
+ import { searchFileContent } from "./searchFileContent.mjs";
3
+ import { findFiles } from "./findFiles.mjs";
4
+ import { filterFilesNot } from "./filterFiles.mjs";
5
+
6
+ export function findDependentsInTargetPaths(
7
+ componentObjectArray,
8
+ targetPaths = ["./components/**/*.tsx", "./pages/**/*.tsx"],
9
+ ) {
10
+ return targetPaths.reduce(targetPathReducer, componentObjectArray);
11
+ }
12
+ /**
13
+ * Finds dependents in each target path and creates a single object to represent them
14
+ * @param {*} accumulator
15
+ * @param {*} currentTargetPath
16
+ * @returns
17
+ */
18
+ function targetPathReducer(componentObjectArray, currentTargetPath) {
19
+ return componentObjectArray.reduce(componentObjectArrayReducer, {
20
+ currentTargetPath,
21
+ componentObjectArray: [],
22
+ }).componentObjectArray;
23
+ }
24
+ /**
25
+ * Traverses componentObjectArray and checks for its exports in all relevant files in the current target path
26
+ * @param {*} componentObject
27
+ * @returns
28
+ */
29
+ function componentObjectArrayReducer(acc, componentObject) {
30
+ const { matches, filePath, dependents } = componentObject;
31
+ const { currentTargetPath } = acc;
32
+ const targetFilelist = filterFilesNot(findFiles(currentTargetPath), [
33
+ ".test",
34
+ ".stories",
35
+ ]);
36
+ const { dependentsArray } = targetFilelist.reduce(targetFileListReducer, {
37
+ matches,
38
+ dependentsArray: [],
39
+ });
40
+ acc.componentObjectArray.push({
41
+ matches,
42
+ filePath,
43
+ dependents:
44
+ dependents !== undefined
45
+ ? [...dependents, ...dependentsArray]
46
+ : dependentsArray,
47
+ });
48
+ return acc;
49
+ }
50
+ /**
51
+ * For each target file, check against all match items and add details to dependents array if matches are found
52
+ * @param {*} acc
53
+ * @param {*} curr
54
+ * @returns
55
+ */
56
+ function targetFileListReducer(acc, currentFilePath) {
57
+ const content = readFileContent(currentFilePath);
58
+ const dependentsCandidates = acc.matches
59
+ .flatMap((searchTerm) =>
60
+ searchFileContent(
61
+ content,
62
+ new RegExp(`import [^']*${searchTerm}[^a-zA-Z]([^']+'){2}`, "g"),
63
+ ),
64
+ )
65
+ .filter((currentArray) => currentArray.length > 0);
66
+ if (dependentsCandidates && dependentsCandidates.length > 0) {
67
+ acc.dependentsArray.push({
68
+ filePath: currentFilePath,
69
+ matches: dependentsCandidates,
70
+ });
71
+ }
72
+ return acc;
73
+ }
@@ -0,0 +1,74 @@
1
+ import { readFileContent } from "./readFileContent.mjs";
2
+ import { searchFileContent } from "./searchFileContent.mjs";
3
+ import { findFiles } from "./findFiles.mjs";
4
+ import { filterFilesNot } from "./filterFiles.mjs";
5
+
6
+ export function findDependentsInTargetPaths(
7
+ componentObjectArray,
8
+ targetPaths,
9
+ ignorePatterns,
10
+ ) {
11
+ return targetPaths.reduce(targetPathReducer, componentObjectArray);
12
+ /**
13
+ * Finds dependents in each target path and creates a single object to represent them
14
+ * @param {*} accumulator
15
+ * @param {*} currentTargetPath
16
+ * @returns
17
+ */
18
+ function targetPathReducer(componentObjectArray, currentTargetPath) {
19
+ return componentObjectArray.reduce(componentObjectArrayReducer, {
20
+ currentTargetPath,
21
+ componentObjectArray: [],
22
+ }).componentObjectArray;
23
+ }
24
+ /**
25
+ * Traverses componentObjectArray and checks for its exports in all relevant files in the current target path
26
+ * @param {*} componentObject
27
+ * @returns
28
+ */
29
+ function componentObjectArrayReducer(acc, componentObject) {
30
+ const { matches, filePath, dependents } = componentObject;
31
+ const { currentTargetPath } = acc;
32
+ const targetFilelist = filterFilesNot(
33
+ findFiles(currentTargetPath),
34
+ ignorePatterns,
35
+ );
36
+ const { dependentsArray } = targetFilelist.reduce(targetFileListReducer, {
37
+ matches,
38
+ dependentsArray: [],
39
+ });
40
+ acc.componentObjectArray.push({
41
+ matches,
42
+ filePath,
43
+ dependents:
44
+ dependents !== undefined
45
+ ? [...dependents, ...dependentsArray]
46
+ : dependentsArray,
47
+ });
48
+ return acc;
49
+ }
50
+ /**
51
+ * For each target file, check against all match items and add details to dependents array if matches are found
52
+ * @param {*} acc
53
+ * @param {*} curr
54
+ * @returns
55
+ */
56
+ function targetFileListReducer(acc, currentFilePath) {
57
+ const content = readFileContent(currentFilePath);
58
+ const dependentsCandidates = acc.matches
59
+ .flatMap((searchTerm) =>
60
+ searchFileContent(
61
+ content,
62
+ new RegExp(`import [^'"]*${searchTerm}[^a-zA-Z]([^'"]+["']){2}`, "g"),
63
+ ),
64
+ )
65
+ .filter((currentArray) => currentArray.length > 0);
66
+ if (dependentsCandidates && dependentsCandidates.length > 0) {
67
+ acc.dependentsArray.push({
68
+ filePath: currentFilePath,
69
+ matches: dependentsCandidates,
70
+ });
71
+ }
72
+ return acc;
73
+ }
74
+ }
@@ -0,0 +1,10 @@
1
+ import * as fs from 'fs'
2
+
3
+ /**
4
+ *
5
+ * @param {string} pattern
6
+ * @returns {string[]}
7
+ */
8
+ export function findFiles(pattern) {
9
+ return fs.globSync(pattern)
10
+ }
package/src/index.mjs ADDED
@@ -0,0 +1,33 @@
1
+ import { findComponents } from "./findComponents.mjs";
2
+ import { writeFile } from "./writeFile.mjs";
3
+ import {
4
+ filterNoDependents,
5
+ filterSomeDependents,
6
+ } from "./filterDependents.mjs";
7
+ import { findDependentsInTargetPaths } from "./findDependents.mjs";
8
+
9
+ /**
10
+ *
11
+ * @param {string} searchPattern
12
+ * @param {string[]} targetPaths
13
+ * @param {string[]} ignorePatterns
14
+ */
15
+ export function runDependencyFinder(
16
+ searchPattern,
17
+ targetPaths,
18
+ ignorePatterns,
19
+ ) {
20
+ // Get files with TSX exports
21
+ const allDependents = findDependentsInTargetPaths(
22
+ findComponents(searchPattern, ignorePatterns),
23
+ targetPaths,
24
+ ignorePatterns,
25
+ );
26
+ const noDependents = filterNoDependents(allDependents);
27
+ const someDependents = filterSomeDependents(allDependents);
28
+ const returnObject = {
29
+ noDependents: { count: noDependents.length, results: noDependents },
30
+ someDependents: { count: someDependents.length, results: someDependents },
31
+ };
32
+ writeFile(JSON.stringify(returnObject), "dependents-report.json");
33
+ }
@@ -0,0 +1,10 @@
1
+ import * as fs from 'fs'
2
+
3
+ /**
4
+ *
5
+ * @param {string} filePath
6
+ * @returns {string}
7
+ */
8
+ export function readFileContent(filePath) {
9
+ return fs.readFileSync(filePath, { encoding: 'utf8' })
10
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ *
3
+ * @param {string} content
4
+ * @param {RegExp} searchPattern
5
+ * @param {*} captureGroup
6
+ * @returns
7
+ */
8
+ export function searchFileContent(content, searchPattern, captureGroup) {
9
+ const matches = content.match(searchPattern)
10
+ if (!matches) return []
11
+ if (captureGroup) {
12
+ return matches.map((matchString) =>
13
+ matchString.replace(searchPattern, `$${captureGroup}`)
14
+ )
15
+ }
16
+ return matches
17
+ }
@@ -0,0 +1,30 @@
1
+ import { readFileContent } from "./readFileContent.mjs";
2
+ import { searchFileContent } from "./searchFileContent.mjs";
3
+
4
+ /**
5
+ *
6
+ * @param {string[]} filePathArray
7
+ * @param {RegExp} searchPattern
8
+ * @param {number} captureGroup
9
+ * @returns {[path:string, matches: string[]]}
10
+ */
11
+ export function searchFiles(filePathArray, searchPattern, captureGroup) {
12
+ return filePathArray.reduce(searchReducer, []);
13
+
14
+ function searchReducer(acc, curr) {
15
+ const matches = searchFile(curr, searchPattern, captureGroup);
16
+ acc.push({ filePath: curr, matches });
17
+ return acc;
18
+ }
19
+ }
20
+ /**
21
+ *
22
+ * @param {string} filePath
23
+ * @param {RegExp} searchPattern
24
+ * @param {number} captureGroup
25
+ * @returns {string[]}
26
+ */
27
+ function searchFile(filePath, searchPattern, captureGroup) {
28
+ const content = readFileContent(filePath);
29
+ return searchFileContent(content, searchPattern, captureGroup);
30
+ }
@@ -0,0 +1,9 @@
1
+ import * as fs from 'fs'
2
+ /**
3
+ *
4
+ * @param {string} content
5
+ * @param {string} path
6
+ */
7
+ export function writeFile(content, path) {
8
+ fs.writeFileSync(path, content)
9
+ }
@@ -0,0 +1,3 @@
1
+ export default function ComponentA() {
2
+ return <div>Test</div>;
3
+ }
@@ -0,0 +1,3 @@
1
+ export const ComponentB = () => {
2
+ return <div>Test</div>;
3
+ };
@@ -0,0 +1,5 @@
1
+ const ComponentC = () => {
2
+ return <div>Test</div>;
3
+ };
4
+
5
+ export default ComponentC;
@@ -0,0 +1,3 @@
1
+ import ComponentA from "./folder-a/test1";
2
+ import { ComponentA } from "./folder-a/test1";
3
+ import { ComponentB } from "./folder-b/test2";
package/test/test.mjs ADDED
@@ -0,0 +1,39 @@
1
+ import { runDependencyFinder } from "../src/index.mjs";
2
+ import { answerStringQuestion } from "@gld5000-cli/readline";
3
+
4
+ const args = process.argv.slice(2);
5
+ const defaultSearchPattern = "./components/**/*.tsx";
6
+ const searchPattern =
7
+ args[0] ||
8
+ (await answerStringQuestion(
9
+ "Where are your components?",
10
+ defaultSearchPattern,
11
+ ));
12
+
13
+ const defaultTargetPaths = "./components/**/*.tsx|./pages/**/*.tsx";
14
+ const targetPaths = `${
15
+ args[1] ||
16
+ (await answerStringQuestion(
17
+ "Which folders should we check for imports (pipe-separated)?",
18
+ defaultTargetPaths,
19
+ ))
20
+ }`.split("|");
21
+
22
+ const defaultIgnorePatterns = ".test|.stories";
23
+ const ignorePatterns = `${
24
+ args[2] ||
25
+ (await answerStringQuestion(
26
+ "Which filename patterns should be ignored (pipe-separated)?",
27
+ defaultIgnorePatterns,
28
+ ))
29
+ }`.split("|");
30
+ // node test/test.mjs "test/test-components/**/*.tsx" "test/test-components/**/*.tsx" ".test|.stories"
31
+ console.log(
32
+ "searchPattern:",
33
+ searchPattern,
34
+ "targetPaths:",
35
+ targetPaths,
36
+ "ignorePatterns:",
37
+ ignorePatterns,
38
+ );
39
+ runDependencyFinder(searchPattern, targetPaths, ignorePatterns);