rev-dep 1.0.0-alpha.5 → 1.0.0-alpha.6

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 CHANGED
@@ -1,5 +1,5 @@
1
1
  <h3 align="center">
2
- <code>revdep</code>
2
+ <code>revdep</code>
3
3
  </h3>
4
4
 
5
5
  <p align="center">
@@ -24,18 +24,18 @@ It's especially useful in JS world without TypeScript or tests coverage.
24
24
 
25
25
  It also helps to identify and eliminate dead files, understand the complexity of the file dependencies
26
26
 
27
- [Jump to CLI reference](#CLI-reference)
27
+ [Jump to CLI reference](#cli-reference)
28
28
 
29
- [`export * from` problem](#Export-from-problem)
29
+ [`export * from` problem](#export-from-problem)
30
30
 
31
31
  ### Use cases
32
32
 
33
- - You plan to refactor some file and you wonder which entry points are affected
34
- - You are wondering wether a given source file is used
35
- - You wonder if there are any dead files in your project
36
- - You want to identify all dead files at once
37
- - You want to verify if a given entry point imports only the required files
38
- - You want to optimize the amount of files imported by an entry point
33
+ - [You plan to refactor some file and you wonder which entry points are affected](#how-to-identify-where-a-file-is-used-in-the-project)
34
+
35
+ - [You are wondering wether a given source file is used](#how-to-check-if-a-file-is-used-in-the-project)
36
+ - [You wonder if there are any dead files in your project](#how-to-identify-dead-files-in-the-project)
37
+ - [You want to verify if a given entry point imports only the required files](#how-to-check-which-files-are-imported-by-a-given-file)
38
+ - [You want to optimize the amount of files imported by an entry point](#how-to-reduce-amount-of-files-imported-by-entry-point)
39
39
 
40
40
  ### How about dependency or bundle graphs?
41
41
 
@@ -67,48 +67,292 @@ or
67
67
 
68
68
  ### How to identify where a file is used in the project?
69
69
 
70
+ Just use `rev-dep resolve path/to/file.ts`
71
+
72
+ You will see all the entry points that implicitly require given file together with resolution path.
73
+
74
+ [`resolve` Command CLI reference](#command-resolve)
75
+ <details>
76
+ <summary>Example for the rev-dep repository</summary>
77
+
78
+ command:
79
+
80
+ `rev-dep resolve src/lib/utils.ts`
81
+
82
+ output:
83
+
84
+ ```s
85
+ src/babel/index.js :
86
+
87
+ ➞ src/babel/index.js
88
+ ➞ src/lib/utils.ts
89
+ _____________________
90
+
91
+ src/cli/index.ts :
92
+
93
+ ➞ src/cli/index.ts
94
+ ➞ src/cli/createCommands.ts
95
+ ➞ src/cli/resolve/index.ts
96
+ ➞ src/lib/find.ts
97
+ ➞ src/lib/getDepsTree.ts
98
+ ➞ src/lib/getDepsSetWebpack.ts
99
+ ➞ src/lib/utils.ts
100
+ _____________________
101
+
102
+ ```
103
+ </details>
104
+
105
+ #### Getting more details about file resolution in given entry point
106
+
107
+ To find out all paths combination use `rev-dep resolve` with `-a` flag
108
+
109
+ > You might be surprised how complex dependency tree can be!
110
+
111
+ <details>
112
+ <summary>Example for the rev-dep repository</summary>
113
+
114
+ command:
115
+
116
+ `rev-dep resolve src/lib/utils.ts src/cli/index.ts --all`
117
+
118
+ output:
119
+
120
+ ```s
121
+ src/cli/index.ts :
122
+
123
+ ➞ src/cli/index.ts
124
+ ➞ src/cli/createCommands.ts
125
+ ➞ src/cli/resolve/index.ts
126
+ ➞ src/lib/find.ts
127
+ ➞ src/lib/getDepsTree.ts
128
+ ➞ src/lib/getDepsSetWebpack.ts
129
+ ➞ src/lib/utils.ts
130
+
131
+ ➞ src/cli/index.ts
132
+ ➞ src/cli/createCommands.ts
133
+ ➞ src/cli/resolve/index.ts
134
+ ➞ src/lib/find.ts
135
+ ➞ src/lib/getEntryPoints.ts
136
+ ➞ src/lib/getDepsTree.ts
137
+ ➞ src/lib/getDepsSetWebpack.ts
138
+ ➞ src/lib/utils.ts
139
+
140
+ ➞ src/cli/index.ts
141
+ ➞ src/cli/createCommands.ts
142
+ ➞ src/cli/entryPoints/index.ts
143
+ ➞ src/lib/getEntryPoints.ts
144
+ ➞ src/lib/getDepsTree.ts
145
+ ➞ src/lib/getDepsSetWebpack.ts
146
+ ➞ src/lib/utils.ts
147
+
148
+ ➞ src/cli/index.ts
149
+ ➞ src/cli/createCommands.ts
150
+ ➞ src/cli/files/index.ts
151
+ ➞ src/lib/getDepsTree.ts
152
+ ➞ src/lib/getDepsSetWebpack.ts
153
+ ➞ src/lib/utils.ts
154
+
155
+ ➞ src/cli/index.ts
156
+ ➞ src/cli/createCommands.ts
157
+ ➞ src/cli/resolve/index.ts
158
+ ➞ src/lib/find.ts
159
+ ➞ src/lib/getEntryPoints.ts
160
+ ➞ src/lib/utils.ts
161
+
162
+ ➞ src/cli/index.ts
163
+ ➞ src/cli/createCommands.ts
164
+ ➞ src/cli/entryPoints/index.ts
165
+ ➞ src/lib/getEntryPoints.ts
166
+ ➞ src/lib/utils.ts
167
+
168
+ ➞ src/cli/index.ts
169
+ ➞ src/cli/createCommands.ts
170
+ ➞ src/cli/resolve/index.ts
171
+ ➞ src/lib/find.ts
172
+ ➞ src/lib/utils.ts
173
+
174
+ ➞ src/cli/index.ts
175
+ ➞ src/cli/createCommands.ts
176
+ ➞ src/cli/resolve/index.ts
177
+ ➞ src/lib/utils.ts
178
+
179
+ ➞ src/cli/index.ts
180
+ ➞ src/cli/createCommands.ts
181
+ ➞ src/cli/entryPoints/index.ts
182
+ ➞ src/lib/utils.ts
183
+
184
+ ➞ src/cli/index.ts
185
+ ➞ src/cli/createCommands.ts
186
+ ➞ src/cli/files/index.ts
187
+ ➞ src/lib/utils.ts
188
+
189
+ ```
190
+ </details>
191
+
70
192
  ### How to check if a file is used in the project?
71
193
 
194
+ Use `rev-dep resolve path/to/file.ts --compactSummary`
195
+
196
+ As a result you will see total amount of entry points requiring a given file.
197
+
198
+ > Note that among the entry points list there might be some dead files importing the searched file
199
+
200
+ [`resolve` Command CLI reference](#command-resolve)
201
+
202
+ <details>
203
+ <summary>Example for the rev-dep repository</summary>
204
+
205
+ command:
206
+
207
+ `rev-dep resolve src/lib/utils.ts --compactSummary`
208
+
209
+ output:
210
+
211
+ ```s
212
+ Results:
213
+
214
+ __tests__/find.test.js : 0
215
+ babel.js : 0
216
+ bin.js : 0
217
+ scripts/addDocsToReadme.js : 0
218
+ src/babel/index.js : 1
219
+ src/cli/index.ts : 1
220
+ src/lib/getMaxDepthInGraph.ts : 0
221
+ types.d.ts : 0
222
+
223
+ Total: 2
224
+ ```
225
+ </details>
226
+
72
227
  ### How to identify dead files in the project?
73
228
 
229
+ Use `rev-dep entry-points` to get list of all files that are not required by any other files in the project.
230
+
231
+ You might want to exclude some file paths that are meant to be actual entry point like `index.js` or `**/pages/**` in `next.js` projects using `--exclude` flag. The same for configuration files like `babel.config.js`
232
+
233
+ Review the list and look for suspicious files like `src/ui/components/SomeComponent/index.js`
234
+
235
+ [`entry-points` command CLI reference](#command-entry-points)
236
+
237
+ <details>
238
+ <summary>Example for the rev-dep repository</summary>
239
+
240
+ command:
241
+
242
+ `rev-dep entry-points --exclude '__tests__/**' 'types.d.ts'`
243
+
244
+ output:
245
+
246
+ ```s
247
+ babel.js
248
+ bin.js
249
+ scripts/addDocsToReadme.js
250
+ src/babel/index.js
251
+ src/cli/index.ts
252
+ src/lib/getMaxDepthInGraph.ts
253
+
254
+ ```
255
+
256
+ The last one `src/lib/getMaxDepthInGraph.ts` is the source file that is not used at the moment.
257
+
258
+ The rest of them looks legit!
259
+ </details>
260
+
74
261
  ### How to check which files are imported by a given file?
75
262
 
263
+ To get a full list of files imported by given entry point use `rev-dep files path/to/file.ts`.
264
+
265
+ You can use `--count` flag if you are interested in the amount.
266
+
267
+ This is a good indicator of how heavy a given entry point or component is
268
+
269
+ [`files` command CLI reference](#command-files)
270
+
271
+ <details>
272
+ <summary>Example for the rev-dep repository</summary>
273
+
274
+ command:
275
+
276
+ `rev-dep files files src/cli/index.ts`
277
+
278
+ output:
279
+
280
+ ```s
281
+ src/cli/index.ts
282
+ src/cli/createCommands.ts
283
+ package.json
284
+ src/cli/resolve/index.ts
285
+ src/cli/docs/index.ts
286
+ src/cli/entryPoints/index.ts
287
+ src/cli/files/index.ts
288
+ src/lib/find.ts
289
+ src/cli/resolve/types.ts
290
+ src/cli/resolve/formatResults.ts
291
+ src/lib/utils.ts
292
+ src/cli/commonOptions.ts
293
+ src/cli/docs/generate.ts
294
+ src/cli/entryPoints/types.ts
295
+ src/lib/getEntryPoints.ts
296
+ src/lib/buildDepsGraph.ts
297
+ src/cli/files/types.ts
298
+ src/lib/getDepsTree.ts
299
+ src/lib/types.ts
300
+ src/cli/docs/template.ts
301
+ src/lib/getDepsSetWebpack.ts
302
+ src/lib/cleanupDpdmDeps.ts
303
+
304
+ ```
305
+
306
+ As you can see cli even import `package.json`. This is to print version of the cli
307
+ </details>
308
+
309
+
76
310
  ### How to reduce amount of files imported by entry point?
77
311
 
312
+ There is no easy how to for this process, but you can do it iteratively using two of the `rev-dep` commands:
313
+ - `files`
314
+ - `resolve`
315
+
316
+ 1. Get the list of files imported by entry-point
317
+ - `rev-dep files path/to/entry-point`
318
+ 2. Identify some suspicious files on the list, components that should not be used on the given page or not related utility files
319
+ 3. Get all resolution paths for a suspicious file
320
+ - `rev-dep resolve path/to/suspicious-file path/to/entry-point --all`
321
+ 4. You would usually find out that there is some file, like directory `index` file that given entry point is using, which is mandatory, but as a side effect it imports a few files that are redundant for your entry point. In most cases you should be able to decouple the imports or reverse the dependency to cut off the resolution path for the unwanted file
322
+
78
323
  ## Usage
79
324
 
80
325
  Project can be used as a CLI tool or as a module
81
326
 
82
327
  ### CLI Tool
83
328
 
84
- For CLI usage see [CLI reference](#CLI-reference)
329
+ For CLI usage see [CLI reference](#cli-reference)
85
330
 
86
331
  ### Module
87
332
 
88
- #### `find` Function
333
+ #### `resolve` Function
89
334
 
90
335
  ```ts
91
- import { find } from "rev-dep";
336
+ import { resolve } from "rev-dep";
92
337
 
93
- const path = find({
338
+ const paths = resolve({
94
339
  entryPoints: ["index.js"],
95
340
  filePath: "utils.js",
96
341
  });
97
342
 
98
- console.log(path);
343
+ console.log(paths);
99
344
  ```
100
345
 
101
- #### `find` Function
346
+ #### `getEntryPoints` Function
102
347
 
103
348
  ```ts
104
- import { find } from "rev-dep";
349
+ import { getEntryPoints } from "rev-dep";
105
350
 
106
- const path = find({
107
- entryPoints: ["index.js"],
108
- filePath: "utils.js",
351
+ const entryPoints = getEntryPoints({
352
+ cwd: process.cwd(),
109
353
  });
110
354
 
111
- console.log(path);
355
+ console.log(entryPoints);
112
356
  ```
113
357
 
114
358
  ## CLI reference
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const find_1 = require("../../lib/find");
3
+ const resolve_1 = require("../../lib/resolve");
4
4
  const formatResults_1 = require("./formatResults");
5
5
  const utils_1 = require("../../lib/utils");
6
6
  const commonOptions_1 = require("../commonOptions");
@@ -20,7 +20,7 @@ function createResolve(program) {
20
20
  .option('-a, --all', 'finds all paths combination of a given dependency. Might work very slow or crash for some projects due to heavy usage of RAM', false)
21
21
  .action(async (filePath, entryPoints, data) => {
22
22
  const { compactSummary, webpackConfig, all, cwd, exclude, include } = data;
23
- const [results, resolveEntryPoints] = await (0, find_1.resolve)({
23
+ const [results, resolveEntryPoints] = await (0, resolve_1.resolve)({
24
24
  entryPoints,
25
25
  filePath,
26
26
  webpackConfig,
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolve = void 0;
4
+ const buildDepsGraph_1 = require("./buildDepsGraph");
5
+ const getDepsTree_1 = require("./getDepsTree");
6
+ const getEntryPoints_1 = require("./getEntryPoints");
7
+ const utils_1 = require("./utils");
8
+ const resolvePathsToRoot = (node, all = false, resolvedPaths = [[]]) => {
9
+ const newPaths = resolvedPaths.map((resolvedPath) => [
10
+ node.path,
11
+ ...resolvedPath
12
+ ]);
13
+ if (node.parents.length === 0) {
14
+ return newPaths;
15
+ }
16
+ if (all) {
17
+ return node.parents
18
+ .map((parentPath) => resolvePathsToRoot(parentPath, all, newPaths))
19
+ .flat(1);
20
+ }
21
+ return resolvePathsToRoot(node.parents[0], false, newPaths);
22
+ };
23
+ const resolve = async ({ entryPoints: _entryPoints, filePath, webpackConfig, cwd = process.cwd(), all, include, exclude }) => {
24
+ let deps, entryPoints;
25
+ if (_entryPoints.length > 0) {
26
+ entryPoints = _entryPoints;
27
+ const sanitizedEntryPoints = (0, utils_1.sanitizeUserEntryPoints)(entryPoints);
28
+ deps = await (0, getDepsTree_1.getDepsTree)(cwd, sanitizedEntryPoints, webpackConfig);
29
+ }
30
+ else {
31
+ ;
32
+ [entryPoints, deps] = await (0, getEntryPoints_1.getEntryPoints)({ cwd, exclude, include });
33
+ }
34
+ const cleanedEntryPoints = entryPoints.map(utils_1.removeInitialDot);
35
+ const cleanedFilePath = (0, utils_1.removeInitialDot)(filePath);
36
+ const forest = cleanedEntryPoints.map((0, buildDepsGraph_1.buildGraphDpdm)(deps, cleanedFilePath));
37
+ const resolvedPaths = forest.reduce((allPaths, [_, fileNode]) => {
38
+ if (!fileNode) {
39
+ return [...allPaths, []];
40
+ }
41
+ const pathsForTree = resolvePathsToRoot(fileNode, all);
42
+ return [...allPaths, pathsForTree];
43
+ }, []);
44
+ return [resolvedPaths, entryPoints];
45
+ };
46
+ exports.resolve = resolve;
package/dist/module.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEntryPoints = exports.resolve = void 0;
4
+ var resolve_1 = require("./lib/resolve");
5
+ Object.defineProperty(exports, "resolve", { enumerable: true, get: function () { return resolve_1.resolve; } });
6
+ var getEntryPoints_1 = require("./lib/getEntryPoints");
7
+ Object.defineProperty(exports, "getEntryPoints", { enumerable: true, get: function () { return getEntryPoints_1.getEntryPoints; } });
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "rev-dep",
3
- "version": "1.0.0-alpha.5",
4
- "description": "Reverse dependency resolution tool built with dependency-cruiser",
5
- "main": "lib/find.js",
3
+ "version": "1.0.0-alpha.6",
4
+ "description": "Dependency debugging tool for JavaScript and TypeScript projects",
5
+ "main": "dist/module.js",
6
6
  "bin": "bin.js",
7
7
  "files": [
8
8
  "dist/**",
@@ -59,5 +59,18 @@
59
59
  "prettier": "^2.1.2",
60
60
  "release-it": "^14.2.1",
61
61
  "typescript": "^4.6.2"
62
- }
62
+ },
63
+ "keywords": [
64
+ "dependencies",
65
+ "deps",
66
+ "dependency graph",
67
+ "dependency debugging",
68
+ "entry points",
69
+ "dependency resolution",
70
+ "reverse dependency resolution",
71
+ "dependency tree",
72
+ "import path",
73
+ "resolution path",
74
+ "dependency optimization"
75
+ ]
63
76
  }