Skip to content

Commit 2d8e5ae

Browse files
feat: inital commit with complete code
0 parents  commit 2d8e5ae

File tree

98 files changed

+12488
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+12488
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.vscode/

ComponentGraphAnalyzer.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { parse } from "@typescript-eslint/typescript-estree";
2+
import * as fs from "node:fs";
3+
import { generateGraphDiagram } from "./src/utils/generateDiagram";
4+
import { analyzeAstBodyDeclaration } from "./src/declarations/AstBodyDeclaration";
5+
import { analyzeAstChildren } from "./src/declarations/AstChildren";
6+
import { GlobalConfig, RawConfig } from "./src/types/Config";
7+
import { FileNode } from "./src/types/FileNode";
8+
import { getFilesList } from "./src/utils/getFilesList";
9+
import * as nodePath from "node:path";
10+
11+
const getAbstractSyntaxTree = (filePath: string) => {
12+
try {
13+
const code = fs.readFileSync(filePath).toString();
14+
15+
return parse(code, { jsx: true, loc: true, loggerFn: false });
16+
} catch (err) {
17+
return null;
18+
}
19+
};
20+
21+
const analyze = (fileNode: FileNode) => {
22+
const config = GlobalConfig.getInstance();
23+
const { path: relativePath } = fileNode;
24+
25+
const path = nodePath.normalize(relativePath);
26+
27+
if (config.isFileAnalyzed(path)) {
28+
return;
29+
}
30+
31+
config.setFileAsAnalyzed(path);
32+
33+
const abstractSyntaxTree = getAbstractSyntaxTree(path);
34+
35+
if (!abstractSyntaxTree) {
36+
return;
37+
}
38+
39+
analyzeAstBodyDeclaration(fileNode, abstractSyntaxTree.body);
40+
analyzeAstChildren(fileNode);
41+
42+
for (const dependency of fileNode.dependencies) {
43+
if (dependency.isUsed) {
44+
analyze(dependency);
45+
}
46+
}
47+
};
48+
49+
export const fileBasedAnalyze = (filePath: string, config: RawConfig = {}) => {
50+
GlobalConfig.getInstance(config);
51+
52+
const rootNode = new FileNode(filePath);
53+
rootNode.setAsUsed();
54+
55+
analyze(rootNode);
56+
57+
return generateGraphDiagram(rootNode);
58+
};
59+
60+
export const directoryBasedAnalyze = (
61+
catalogPath: string,
62+
config: RawConfig = {}
63+
) => {
64+
GlobalConfig.getInstance(config);
65+
66+
const results: string[] = [];
67+
const files = getFilesList(catalogPath);
68+
69+
for (let filePath of files) {
70+
const rootNode = new FileNode(filePath);
71+
rootNode.setAsUsed();
72+
73+
analyze(rootNode);
74+
75+
results.push(...generateGraphDiagram(rootNode));
76+
}
77+
78+
return [...new Set(results)];
79+
};

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Components Dependency Graph
2+
3+
A script for creating a graph of relationships between components in React.js / React Native applications.
4+
5+
## Usage
6+
7+
File mode
8+
9+
`npx components-dependency-graph -f /path/to/component.tsx`
10+
11+
Directory mode
12+
13+
`npx components-dependency-graph -d /path/to/directory`
14+
15+
### Options
16+
17+
`--config <path>` – Allow to use config in different location (details about config below).
18+
19+
`--generate_diagram <path>` – Generates diagram in SVG format based on GraphViz.
20+
21+
## Configuration
22+
23+
Just create in base location file called `cdg.config.json`:
24+
25+
```
26+
{
27+
"excludeDirectoriesWithNameContains": [
28+
// ...
29+
],
30+
"excludeFilesContains": [
31+
/// ...
32+
],
33+
"rootPath": "",
34+
"generateGraphWithFilesPaths": true,
35+
"pathAliases": {
36+
"@assets": "./src/assets/"
37+
}
38+
}
39+
```
40+
41+
### Available parameters
42+
43+
| Property | Type | Description |
44+
| ---------------------------------- | ---------------------- | ---------------------------------------------------------------------- |
45+
| excludeDirectoriesWithNameContains | Array<string> | If catalog path contains one of string from this list will be ignored. |
46+
| excludeFilesContains | Array<string> | If filename contains one of string from this list will be ignored. |
47+
| rootPath | string | Specify the path of the main project root. |
48+
| generateGraphWithFilesPaths | boolean | Specify if graph nodes should have path added to name. |
49+
| pathAliases | Record<string, string> | Dictionary to replaces paths based on Babel configuration |
50+
51+
## License
52+
53+
MIT

babel.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
presets: [
3+
["@babel/preset-env", { targets: { node: "current" } }],
4+
"@babel/preset-typescript",
5+
],
6+
};

cli.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env node
2+
3+
import { program } from "commander";
4+
import pc from "picocolors";
5+
import * as fs from "node:fs";
6+
import {
7+
fileBasedAnalyze,
8+
directoryBasedAnalyze,
9+
} from "./ComponentGraphAnalyzer";
10+
import { generateGraphVizDiagram } from "./src/utils/generateDiagram";
11+
import { RawConfig } from "./src/types/Config";
12+
13+
program
14+
.option("-f, --file <path>")
15+
.option("-d, --directory <path>")
16+
.option("-c, --config <path>")
17+
.option("-g, --generate_diagram <path>");
18+
19+
program.parse();
20+
21+
const options = program.opts();
22+
const DEFAULT_CONFIG_PATH = "cdg.config.json";
23+
const configPath = options.config || DEFAULT_CONFIG_PATH;
24+
25+
let config: RawConfig = {};
26+
27+
try {
28+
config = JSON.parse(fs.readFileSync(configPath).toString());
29+
} catch (event) {
30+
console.log(pc.yellow("Config file not found. Continuing..."));
31+
}
32+
33+
let result: string[] = [];
34+
35+
if (options.file) {
36+
result = fileBasedAnalyze(options.file, config);
37+
} else if (options.directory) {
38+
result = directoryBasedAnalyze(options.directory, config);
39+
} else {
40+
console.log(
41+
pc.red("Specify file or directory. Check --help for more information.")
42+
);
43+
}
44+
45+
if (options.generate_diagram) {
46+
generateGraphVizDiagram(result, options.generate_diagram);
47+
} else {
48+
result.map(item => console.log(item));
49+
}

dist/ComponentGraphAnalyzer.js

Lines changed: 86 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ComponentGraphAnalyzer.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cli.js

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cli.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)