Skip to content

Commit 7549b79

Browse files
committed
flat tree for folder
1 parent 7f3ea9a commit 7549b79

File tree

7 files changed

+313
-78
lines changed

7 files changed

+313
-78
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,24 @@ npm i @andriyorehov/ts-graph
1212

1313
## Usage
1414

15+
Per file
16+
1517
```ts
1618
import { getTreeByFile } from '@andriyorehov/ts-graph';
1719

1820
const tree = getTreeByFile('filePath.ts');
1921
console.dir(tree, { depth: null });
2022
```
2123

24+
Per folder
25+
```ts
26+
import { getTreeByFolder } from '@andriyorehov/ts-graph';
27+
28+
const tree = getTreeByFolder('filePath.ts');
29+
console.dir(tree, { depth: null });
30+
```
31+
32+
2233
## Comparison table
2334

2435
| Feature/Name | ts-tree | [module-graph](https://github.com/thepassle/module-graph) | [node-dependency-tree](https://github.com/dependents/node-dependency-tree) |
@@ -60,6 +71,9 @@ console.dir(tree, { depth: null });
6071
- [ ] check barrel files handling for default import
6172
- [ ] circular dependencies handling?
6273
- [ ] how to get file deps with skipAddingFilesFromTsConfig: false, skipFileDependencyResolution: false,
74+
- [x] depth number
75+
- [x] flat tree for folder
76+
- [ ] parent in flat tree
6377

6478
## Tech Debt
6579

@@ -72,3 +86,17 @@ console.dir(tree, { depth: null });
7286
- [ ] add test for folder tree id uniqueness
7387
- [ ] upgrade `find-up` to latest version with ESM
7488
- [ ] try [find-up-simple](https://github.com/sindresorhus/find-up-simple) (ESM)
89+
90+
## Development
91+
92+
### Run tests
93+
94+
```shell
95+
npm run test
96+
```
97+
98+
### Prepare mocks
99+
100+
```shell
101+
tsx test/prepare-mocks.ts
102+
```

src/fileTree.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type FileTree = {
1414
meta?: unknown;
1515
usedExports: string[];
1616
children: FileTree[];
17+
depth: number;
1718
};
1819

1920
const buildFileTree = (
@@ -23,7 +24,9 @@ const buildFileTree = (
2324
usedExports: string[] = [],
2425
flatTree: Record<string, FileTree> = {},
2526
additionalInfo: Record<string, unknown> = {},
27+
depth = 0,
2628
) => {
29+
console.log('filePath', depth, filePath);
2730
const sourceFile = project.addSourceFileAtPath(filePath);
2831

2932
const currentFilePath = sourceFile.getFilePath();
@@ -40,16 +43,19 @@ const buildFileTree = (
4043
meta: additionalFileInfo,
4144
usedExports: usedExports,
4245
children: [],
46+
depth: depth,
4347
};
4448
flatTree[fileTree.path] = { ...fileTree, children: [] };
4549

4650
sourceFile.getChildrenOfKind(SyntaxKind.ImportDeclaration).forEach((importDeclaration) => {
51+
console.log('importDeclaration', importDeclaration.getText());
4752
const importClause = importDeclaration.getImportClause();
4853
const namedBindings = importClause?.getNamedBindings();
4954

5055
// handle default import
5156
// https://github.com/dsherret/ts-morph/issues/1507
5257
if (importClause && namedBindings === undefined) {
58+
console.log('andle default import');
5359
const importName = importClause.getText();
5460
const importPath = importDeclaration.getModuleSpecifier().getText();
5561
const unquotedPath = trimQuotes(importPath);
@@ -62,6 +68,7 @@ const buildFileTree = (
6268
[importName],
6369
flatTree,
6470
additionalInfo,
71+
depth + 1,
6572
).fileTree;
6673
fileTree.children.push(childFileTree);
6774
}
@@ -72,10 +79,13 @@ const buildFileTree = (
7279
namedBindings.getElements().forEach((named) => {
7380
const nameNode = named.getNameNode();
7481
const importedName = nameNode.getText();
82+
console.log('importedName', importedName);
7583

7684
const definitionNodes = nameNode.getDefinitionNodes();
85+
// console.log('definitionNodes', definitionNodes);
7786
definitionNodes.forEach((node) => {
7887
const path = node.getSourceFile().getFilePath();
88+
console.log('path', path);
7989
if (!path.includes('node_modules') && isValidNode(node)) {
8090
if (paths[path]) {
8191
paths[path].push(importedName);
@@ -94,6 +104,7 @@ const buildFileTree = (
94104
importedNames,
95105
flatTree,
96106
additionalInfo,
107+
depth + 1,
97108
).fileTree;
98109
// TODO: optimize this?
99110
const sameIndex = fileTree.children.findIndex(child => child.path === childFileTree.path);
@@ -111,7 +122,7 @@ const buildFileTree = (
111122
export const getTreeByFile = (filePath: string, additionalInfo: Record<string, unknown> = {}) => {
112123
const tsConfigFilePath = findUp.sync('tsconfig.json', { cwd: filePath });
113124
const project = new Project({
114-
skipAddingFilesFromTsConfig: true,
125+
skipAddingFilesFromTsConfig: false, // true can cause recursion
115126
skipFileDependencyResolution: true,
116127
tsConfigFilePath,
117128
});

src/folderTree.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,26 @@ export const getTreeByFolder = (folderPath: string) => {
155155
const info = getFilesInfo(folderPath);
156156
const tree = buildTree(info);
157157

158-
const fileTree: FileTree = {
158+
const rootTree: FileTree = {
159159
id: crypto.randomUUID(),
160160
path: '',
161161
name: 'Root',
162162
parentId: undefined,
163163
meta: undefined,
164164
usedExports: [],
165165
children: [],
166+
depth: 0
166167
};
167168

169+
let flatTree = {};
170+
168171
for (const rootChildren of tree.children) {
169172
const tree = getTreeByFile(rootChildren.path);
170-
fileTree.children.push(tree.fileTree);
173+
flatTree = {...flatTree, ...tree.flatTree}
174+
rootTree.children.push(tree.fileTree);
171175
}
172-
return fileTree;
176+
return {
177+
...rootTree,
178+
flatTree
179+
};
173180
};

test/mock/file-additional-tree.json

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@
2020
"usedExports": [
2121
"nested"
2222
],
23-
"children": []
23+
"children": [],
24+
"depth": 2
2425
},
2526
{
2627
"path": "test/test-project/nested2.ts",
2728
"name": "nested2.ts",
2829
"usedExports": [
2930
"nested2"
3031
],
31-
"children": []
32+
"children": [],
33+
"depth": 2
3234
}
33-
]
35+
],
36+
"depth": 1
3437
},
3538
{
3639
"path": "test/test-project/deep2.ts",
@@ -48,7 +51,8 @@
4851
"usedExports": [
4952
"nested"
5053
],
51-
"children": []
54+
"children": [],
55+
"depth": 2
5256
},
5357
{
5458
"path": "test/test-project/nested2.ts",
@@ -57,9 +61,11 @@
5761
"nested2",
5862
"nested3"
5963
],
60-
"children": []
64+
"children": [],
65+
"depth": 2
6166
}
62-
]
67+
],
68+
"depth": 1
6369
},
6470
{
6571
"path": "test/test-project/helper/helper.ts",
@@ -84,27 +90,32 @@
8490
"usedExports": [
8591
"nested"
8692
],
87-
"children": []
93+
"children": [],
94+
"depth": 3
8895
},
8996
{
9097
"path": "test/test-project/nested2.ts",
9198
"name": "nested2.ts",
9299
"usedExports": [
93100
"nested2"
94101
],
95-
"children": []
102+
"children": [],
103+
"depth": 3
96104
}
97-
]
105+
],
106+
"depth": 2
98107
}
99-
]
108+
],
109+
"depth": 1
100110
},
101111
{
102112
"path": "test/test-project/default.ts",
103113
"name": "default.ts",
104114
"usedExports": [
105115
"defaultSum"
106116
],
107-
"children": []
117+
"children": [],
118+
"depth": 1
108119
},
109120
{
110121
"path": "test/test-project/nested/use-module-outside-dir.ts",
@@ -122,17 +133,20 @@
122133
"usedExports": [
123134
"nested"
124135
],
125-
"children": []
136+
"children": [],
137+
"depth": 2
126138
}
127-
]
139+
],
140+
"depth": 1
128141
},
129142
{
130143
"path": "test/test-project/usedInTypeAndCode.ts",
131144
"name": "usedInTypeAndCode.ts",
132145
"usedExports": [
133146
"usedInTypeAndCode"
134147
],
135-
"children": []
148+
"children": [],
149+
"depth": 1
136150
},
137151
{
138152
"path": "test/test-project/multiExport.ts",
@@ -141,24 +155,28 @@
141155
"something",
142156
"something2"
143157
],
144-
"children": []
158+
"children": [],
159+
"depth": 1
145160
}
146-
]
161+
],
162+
"depth": 0
147163
},
148164
"flatTree": {
149165
"test/test-project/index.ts": {
150166
"path": "test/test-project/index.ts",
151167
"name": "index.ts",
152168
"usedExports": [],
153-
"children": []
169+
"children": [],
170+
"depth": 0
154171
},
155172
"test/test-project/deep.ts": {
156173
"path": "test/test-project/deep.ts",
157174
"name": "deep.ts",
158175
"usedExports": [
159176
"deep"
160177
],
161-
"children": []
178+
"children": [],
179+
"depth": 2
162180
},
163181
"test/test-project/nested.ts": {
164182
"path": "test/test-project/nested.ts",
@@ -169,63 +187,71 @@
169187
"usedExports": [
170188
"nested"
171189
],
172-
"children": []
190+
"children": [],
191+
"depth": 2
173192
},
174193
"test/test-project/nested2.ts": {
175194
"path": "test/test-project/nested2.ts",
176195
"name": "nested2.ts",
177196
"usedExports": [
178197
"nested2"
179198
],
180-
"children": []
199+
"children": [],
200+
"depth": 3
181201
},
182202
"test/test-project/deep2.ts": {
183203
"path": "test/test-project/deep2.ts",
184204
"name": "deep2.ts",
185205
"usedExports": [
186206
"deep2"
187207
],
188-
"children": []
208+
"children": [],
209+
"depth": 1
189210
},
190211
"test/test-project/helper/helper.ts": {
191212
"path": "test/test-project/helper/helper.ts",
192213
"name": "helper.ts",
193214
"usedExports": [
194215
"helper"
195216
],
196-
"children": []
217+
"children": [],
218+
"depth": 1
197219
},
198220
"test/test-project/default.ts": {
199221
"path": "test/test-project/default.ts",
200222
"name": "default.ts",
201223
"usedExports": [
202224
"defaultSum"
203225
],
204-
"children": []
226+
"children": [],
227+
"depth": 1
205228
},
206229
"test/test-project/nested/use-module-outside-dir.ts": {
207230
"path": "test/test-project/nested/use-module-outside-dir.ts",
208231
"name": "use-module-outside-dir.ts",
209232
"usedExports": [
210233
"useModuleOutsideDir"
211234
],
212-
"children": []
235+
"children": [],
236+
"depth": 1
213237
},
214238
"test/test-project/usedInTypeAndCode.ts": {
215239
"path": "test/test-project/usedInTypeAndCode.ts",
216240
"name": "usedInTypeAndCode.ts",
217241
"usedExports": [
218242
"usedInTypeAndCode"
219243
],
220-
"children": []
244+
"children": [],
245+
"depth": 1
221246
},
222247
"test/test-project/multiExport.ts": {
223248
"path": "test/test-project/multiExport.ts",
224249
"name": "multiExport.ts",
225250
"usedExports": [
226251
"something2"
227252
],
228-
"children": []
253+
"children": [],
254+
"depth": 1
229255
}
230256
}
231257
}

0 commit comments

Comments
 (0)