rev-dep 0.2.0 → 1.0.0-alpha.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/README.md CHANGED
@@ -81,14 +81,14 @@ Available options are
81
81
  #### `find` Function
82
82
 
83
83
  ```js
84
- import { find } from 'rev-dep'
84
+ import { find } from "rev-dep";
85
85
 
86
86
  const path = find({
87
- entryPoints: ['index.js'],
88
- filePath: 'utils.js'
89
- })
87
+ entryPoints: ["index.js"],
88
+ filePath: "utils.js",
89
+ });
90
90
 
91
- console.log(path)
91
+ console.log(path);
92
92
  ```
93
93
 
94
94
  #### `find` Options
@@ -111,6 +111,54 @@ If you installed `rev-dep` **globally**, you will have appropriate compiler inst
111
111
 
112
112
  For example, to support `*.ts` and `*.tsx` implicit extensions in globally installed `rev-dep`, you have to also install globally `typescript` package (see [source](https://github.com/sverweij/dependency-cruiser/blob/96e34d0cf158034f2b7c8cafe9cec72dd74d8c45/src/extract/transpile/typescript-wrap.js))
113
113
 
114
+ ## CLI reference
115
+
116
+ <!-- cli-docs-start -->
117
+
118
+ ### Command `resolve`
119
+
120
+ Description not available
121
+
122
+ #### Usage
123
+
124
+ ```sh
125
+ rev-dep resolve <filePath> [entryPoints...] [options]
126
+ ```
127
+
128
+ #### Arguments
129
+
130
+ - `filePath` - undefined (**required**),\* `entryPoints...` - undefined (_optional_)
131
+
132
+ #### Options
133
+
134
+ - `-cs, --compactSummary` - print a compact summary of reverse resolution with a count of found paths (_optional_)
135
+ - `--verbose` - print current action information (_optional_)
136
+ - `-wc, --webpackConfig <path>` - path to webpack config to enable webpack aliases support (_optional_)
137
+ - `-tc, --typescriptConfig <path>` - path to TypeScript config to enable TS aliases support (_optional_)
138
+ - `-md, --maxDepth <maxDepth>` - max depth of the dependency tree (_optional_)
139
+ - `-pmd, --printMaxDepth` - print max depth in the tree (_optional_)
140
+ - `-pdc, --printDependentCount` - print count of entry point dependencies (_optional_)
141
+ - `-co, --checkOnly` - finds only one path to entry point instead of all (_optional_)
142
+
143
+ ### Command `docs`
144
+
145
+ Generate documentation of available commands into md file.
146
+
147
+ #### Usage
148
+
149
+ ```sh
150
+ rev-dep docs <outputPath> [options]
151
+ ```
152
+
153
+ #### Arguments
154
+
155
+ - `outputPath` - path to output \*.md file (**required**)
156
+
157
+ #### Options
158
+
159
+ - `-hl, --headerLevel <value>` - Initial header level (_optional_)
160
+ <!-- cli-docs-end -->
161
+
114
162
  ## Contributing
115
163
 
116
164
  Project is open to contributions, just rise an issue if you have some ideas about features or you noticed a bug. After discussion we can approach implementation :)
package/bin.js ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ require('./dist/cli');
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.reexportRewireOption = exports.cwdOption = exports.webpackConfigOption = void 0;
4
+ exports.webpackConfigOption = [
5
+ '-wc, --webpackConfig <path>',
6
+ 'path to webpack config to enable webpack aliases support'
7
+ ];
8
+ exports.cwdOption = [
9
+ '--cwd <path>',
10
+ 'path to a directory that should be used as a resolution root',
11
+ process.cwd()
12
+ ];
13
+ exports.reexportRewireOption = [
14
+ '--rr reexportRewire <value>',
15
+ 'resolve actual dependencies for "export * from" statements'
16
+ ];
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createCommands = void 0;
7
+ const resolve_1 = __importDefault(require("./resolve"));
8
+ const docs_1 = __importDefault(require("./docs"));
9
+ const entryPoints_1 = __importDefault(require("./entryPoints"));
10
+ const files_1 = __importDefault(require("./files"));
11
+ function createCommands(program) {
12
+ (0, resolve_1.default)(program);
13
+ (0, entryPoints_1.default)(program);
14
+ (0, files_1.default)(program);
15
+ (0, docs_1.default)(program);
16
+ }
17
+ exports.createCommands = createCommands;
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const fs_1 = __importDefault(require("fs"));
7
+ const createCommands_1 = require("../createCommands");
8
+ const template_1 = __importDefault(require("./template"));
9
+ function createCommandsInspector() {
10
+ let currentCommand = null;
11
+ const commands = [];
12
+ const parseOption = (data, description, defaultValue, required) => {
13
+ const argRegex = /(<|\[).+?(>|\])/g;
14
+ const argument = data.match(argRegex);
15
+ const [shortName, longName] = data
16
+ .replace(argRegex, '')
17
+ .trim()
18
+ .split(/,\s+/);
19
+ return {
20
+ shortName,
21
+ longName,
22
+ argument: argument !== null && argument.length > 0 ? argument[0] : undefined,
23
+ description,
24
+ defaultValue,
25
+ required
26
+ };
27
+ };
28
+ return {
29
+ command(cmd) {
30
+ if (currentCommand !== null) {
31
+ commands.push(currentCommand);
32
+ }
33
+ const [name, ...args] = cmd.split(/\s+/);
34
+ currentCommand = {
35
+ name,
36
+ arguments: args.map((arg) => ({
37
+ nameRaw: arg,
38
+ name: arg.substring(1, arg.length - 1),
39
+ required: arg.charAt(0) === '<'
40
+ })),
41
+ options: []
42
+ };
43
+ return this;
44
+ },
45
+ description(description, argDescription) {
46
+ if (currentCommand !== null) {
47
+ currentCommand.description = description;
48
+ if (argDescription !== undefined) {
49
+ currentCommand.arguments.forEach((arg) => {
50
+ //eslint-disable-next-line
51
+ if (argDescription.hasOwnProperty(arg.name)) {
52
+ arg.description = argDescription[arg.name];
53
+ }
54
+ });
55
+ }
56
+ }
57
+ return this;
58
+ },
59
+ option(data, description, defaultValue) {
60
+ if (currentCommand !== null) {
61
+ currentCommand.options.push(parseOption(data, description, defaultValue, false));
62
+ }
63
+ return this;
64
+ },
65
+ requiredOption(data, description, defaultValue) {
66
+ if (currentCommand !== null) {
67
+ currentCommand.options.push(parseOption(data, description, defaultValue, true));
68
+ }
69
+ return this;
70
+ },
71
+ action() {
72
+ return this;
73
+ },
74
+ getCommands() {
75
+ if (currentCommand !== null) {
76
+ commands.push(currentCommand);
77
+ }
78
+ return commands;
79
+ }
80
+ };
81
+ }
82
+ function generate(output, initialHeaderLevel) {
83
+ const commandInspector = createCommandsInspector();
84
+ //@ts-ignore
85
+ (0, createCommands_1.createCommands)(commandInspector);
86
+ const commands = commandInspector.getCommands();
87
+ const document = (0, template_1.default)(commands, initialHeaderLevel);
88
+ fs_1.default.writeFileSync(output, document);
89
+ }
90
+ exports.default = generate;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const generate_1 = __importDefault(require("./generate"));
7
+ function create(program) {
8
+ program
9
+ .command('docs <outputPath>')
10
+ .description('Generate documentation of available commands into md file.', {
11
+ outputPath: 'path to output *.md file'
12
+ })
13
+ .option('-hl, --headerLevel <value>', 'Initial header level', '3')
14
+ .action((outputPath, options) => {
15
+ (0, generate_1.default)(outputPath, parseInt(options.headerLevel));
16
+ });
17
+ }
18
+ exports.default = create;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const dedent_1 = __importDefault(require("dedent"));
7
+ const programName = 'rev-dep';
8
+ const header = (level, ...text) => '#'.repeat(level) + ' ' + text.join(' ');
9
+ const code = (...text) => '`' + text.join(' ') + '`';
10
+ const codeBlock = (...text) => '```sh\n' + text.join(' ') + '\n```';
11
+ const requiredStr = '**required**';
12
+ const optionalStr = '_optional_';
13
+ const filterFalsy = (array) => array.filter((val) => val);
14
+ function template(commands, headerLevel) {
15
+ return (0, dedent_1.default)(commands
16
+ .map((cmd) => {
17
+ return `
18
+ ${header(headerLevel, 'Command', code(cmd.name))}
19
+
20
+ ${cmd.description || 'Description not available'}
21
+
22
+ ${header(headerLevel + 1, 'Usage')}
23
+
24
+ ${codeBlock(...filterFalsy([
25
+ programName,
26
+ cmd.name,
27
+ ...cmd.arguments.map((arg) => arg.nameRaw),
28
+ cmd.options.length > 0 ? '[options]' : undefined
29
+ ]))}
30
+ ${cmd.arguments.length > 0 ? header(headerLevel + 1, 'Arguments') : ''}
31
+
32
+ ${cmd.arguments.map(({ name, required, description }) => (0, dedent_1.default) `
33
+ * ${code(name)} - ${description} (${required ? requiredStr : optionalStr})
34
+ `)}
35
+
36
+ ${cmd.options.length > 0 ? header(headerLevel + 1, 'Options') : ''}
37
+
38
+ ${cmd.options
39
+ .map(({ shortName, longName, argument, required, description }) => (0, dedent_1.default) `
40
+ * ${code(filterFalsy([shortName, longName]).join(', ') +
41
+ (argument ? ` ${argument}` : ''))} - ${description} (${required ? requiredStr : optionalStr})
42
+ `)
43
+ .join('\n')}
44
+
45
+ `;
46
+ })
47
+ .join('\n'));
48
+ }
49
+ exports.default = template;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const commonOptions_1 = require("../commonOptions");
4
+ const getEntryPoints_1 = require("../../lib/getEntryPoints");
5
+ const buildDepsGraph_1 = require("../../lib/buildDepsGraph");
6
+ function createEntryPoints(program) {
7
+ program
8
+ .command('entry-points')
9
+ .description('Print list of entry points in current directory')
10
+ .option(...commonOptions_1.webpackConfigOption)
11
+ .option(...commonOptions_1.cwdOption)
12
+ .option(...commonOptions_1.reexportRewireOption)
13
+ .option('-pdc, --printDependentCount', 'print count of entry point dependencies', false)
14
+ .action(async (data) => {
15
+ const { webpackConfig: webpackConfigPath, cwd, printDependentCount } = data;
16
+ const [entryPoints, depsTree] = await (0, getEntryPoints_1.getEntryPoints)({
17
+ cwd,
18
+ webpackConfigPath
19
+ });
20
+ let depsCount = null;
21
+ if (printDependentCount) {
22
+ depsCount = entryPoints
23
+ .map((0, buildDepsGraph_1.buildGraphDpdm)(depsTree))
24
+ .map(([_, __, vertices]) => vertices.size);
25
+ }
26
+ entryPoints.forEach((pathName, idx) => {
27
+ if (depsCount !== null) {
28
+ console.log(pathName, depsCount[idx]);
29
+ }
30
+ else {
31
+ console.log(pathName);
32
+ }
33
+ });
34
+ });
35
+ }
36
+ exports.default = createEntryPoints;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const commonOptions_1 = require("../commonOptions");
4
+ const utils_1 = require("../../lib/utils");
5
+ const getDepsTree_1 = require("../../lib/getDepsTree");
6
+ function createFiles(program) {
7
+ program
8
+ .command('files <entryPoint>')
9
+ .description('Get list of files required by entry point')
10
+ .option(...commonOptions_1.webpackConfigOption)
11
+ .option(...commonOptions_1.cwdOption)
12
+ .option(...commonOptions_1.reexportRewireOption)
13
+ .option('-c, --count', 'print only count of entry point dependencies', false)
14
+ .action(async (entryPoint, data) => {
15
+ const { webpackConfig: webpackConfigPath, cwd, count } = data;
16
+ const sanitizedEntryPoints = (0, utils_1.sanitizeUserEntryPoints)([entryPoint]);
17
+ const depsTree = await (0, getDepsTree_1.getDepsTree)(cwd, sanitizedEntryPoints, webpackConfigPath);
18
+ const filePaths = Object.keys(depsTree);
19
+ if (count) {
20
+ console.log(filePaths.length);
21
+ }
22
+ else {
23
+ filePaths.forEach((filePath) => console.log(filePath));
24
+ }
25
+ });
26
+ }
27
+ exports.default = createFiles;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const commander_1 = require("commander");
4
+ //eslint-disable-next-line
5
+ const pkg = require('../../package.json');
6
+ const createCommands_1 = require("./createCommands");
7
+ const program = new commander_1.Command('rev-dep');
8
+ program.version(pkg.version, '-v, --version');
9
+ (0, createCommands_1.createCommands)(program);
10
+ program.parse(process.argv);
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatResults = void 0;
4
+ const pathToString = (str, filePath, indentation) => {
5
+ return `${str ? `${str}\n` : ''}${' '.repeat(indentation)} ➞ ${filePath}`;
6
+ };
7
+ const join = (...args) => args.join(' ') + '\n';
8
+ function formatResults({ results, filePath, entryPoints, compactSummary }) {
9
+ let formatted = '';
10
+ const hasAnyResults = results.some((paths) => paths.length > 0);
11
+ if (!hasAnyResults) {
12
+ formatted = join('No results found for', filePath, 'in', entryPoints);
13
+ return;
14
+ }
15
+ formatted += join('Results:\n');
16
+ if (compactSummary) {
17
+ const maxEntryLength = entryPoints.reduce((maxLength, entryPoint) => {
18
+ return entryPoint.length > maxLength ? entryPoint.length : maxLength;
19
+ }, 0);
20
+ let total = 0;
21
+ entryPoints.forEach((entry, index) => {
22
+ formatted += join(`${entry.padEnd(maxEntryLength)} :`, results[index].length);
23
+ total += results[index].length;
24
+ });
25
+ formatted += join('\nTotal:', total);
26
+ }
27
+ else {
28
+ results.forEach((entryPointResults, index) => {
29
+ entryPointResults.forEach((path) => {
30
+ formatted += join(path.reduce(pathToString, ''), '\n');
31
+ });
32
+ if (index < results.length - 1 && entryPointResults.length > 0) {
33
+ formatted += join('_'.repeat(process.stdout.columns));
34
+ }
35
+ });
36
+ }
37
+ return formatted;
38
+ }
39
+ exports.formatResults = formatResults;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const find_1 = require("../../lib/find");
4
+ const formatResults_1 = require("./formatResults");
5
+ const utils_1 = require("../../lib/utils");
6
+ function createResolve(program) {
7
+ program
8
+ .command('resolve <filePath> [entryPoints...]')
9
+ .description('Checks if a filePath is required from entryPoint(s) and prints the resolution path')
10
+ .option('-cs, --compactSummary', 'print a compact summary of reverse resolution with a count of found paths')
11
+ .option('-wc, --webpackConfig <path>', 'path to webpack config to enable webpack aliases support')
12
+ .option('-pmd, --printMaxDepth', 'print max depth in the tree', false)
13
+ .option('-a, --all', 'finds all paths combination of a given dependency. Might work very slow and tend to crash for some projects', false)
14
+ .action(async (filePath, entryPoints, data) => {
15
+ const { compactSummary, webpackConfig, printMaxDepth, all } = data;
16
+ const sanitizedEntryPoints = (0, utils_1.sanitizeUserEntryPoints)(entryPoints);
17
+ const results = await (0, find_1.resolve)({
18
+ entryPoints: sanitizedEntryPoints,
19
+ filePath,
20
+ webpackConfig,
21
+ printMaxDepth,
22
+ all
23
+ });
24
+ const formatted = (0, formatResults_1.formatResults)({
25
+ results,
26
+ entryPoints,
27
+ compactSummary,
28
+ filePath
29
+ });
30
+ console.log(formatted);
31
+ });
32
+ }
33
+ exports.default = createResolve;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildGraphDpdm = void 0;
4
+ const buildGraphDpdm = (deps, filePath) => (entryPoint) => {
5
+ const vertices = new Map();
6
+ let fileNode = null;
7
+ const inner = (path, visited = new Set(), depth = 1, parent = null) => {
8
+ const vertex = vertices.get(path);
9
+ if (vertex) {
10
+ vertex.parents.push(parent);
11
+ return vertex;
12
+ }
13
+ const localVisited = new Set(visited);
14
+ if (localVisited.has(path)) {
15
+ // console.error('CIRCULAR DEP', ...localVisited.values(), path)
16
+ return {
17
+ path: 'CIRCULAR',
18
+ parents: parent ? [parent] : [],
19
+ children: []
20
+ };
21
+ }
22
+ localVisited.add(path);
23
+ const dep = deps[path];
24
+ if (dep === undefined) {
25
+ throw new Error(`Dependency '${path}' not found!`);
26
+ }
27
+ const node = {
28
+ parents: parent ? [parent] : [],
29
+ path
30
+ };
31
+ node.children = (dep || [])
32
+ .map((d) => d.id)
33
+ .filter((path) => path !== null && !path.includes('node_modules'))
34
+ .map((path) => inner(path, localVisited, depth + 1, node));
35
+ vertices.set(path, node);
36
+ if (path === filePath) {
37
+ fileNode = node;
38
+ }
39
+ return node;
40
+ };
41
+ return [inner(entryPoint), fileNode, vertices];
42
+ };
43
+ exports.buildGraphDpdm = buildGraphDpdm;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cleanupDpdmDeps = void 0;
4
+ const cleanupDpdmDeps = (deps) => {
5
+ const newDeps = {};
6
+ Object.entries(deps).forEach(([id, dependencies]) => {
7
+ if (!id.includes('node_modules') && dependencies !== null) {
8
+ newDeps[id] = dependencies
9
+ .filter(({ id }) => id && !id.includes('node_modules'))
10
+ .map(({ id, request }) => ({
11
+ id,
12
+ request
13
+ }));
14
+ }
15
+ });
16
+ return newDeps;
17
+ };
18
+ exports.cleanupDpdmDeps = cleanupDpdmDeps;
@@ -0,0 +1,51 @@
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 getMaxDepthInGrapth_1 = require("./getMaxDepthInGrapth");
8
+ const utils_1 = require("./utils");
9
+ const resolvePathsToRoot = (node, all = false, resolvedPaths = [[]]) => {
10
+ const newPaths = resolvedPaths.map((resolvedPath) => [
11
+ node.path,
12
+ ...resolvedPath
13
+ ]);
14
+ if (node.parents.length === 0) {
15
+ return newPaths;
16
+ }
17
+ if (all) {
18
+ return node.parents
19
+ .map((parentPath) => resolvePathsToRoot(parentPath, all, newPaths))
20
+ .flat(1);
21
+ }
22
+ return resolvePathsToRoot(node.parents[0], false, newPaths);
23
+ };
24
+ const resolve = async ({ entryPoints: _entryPoints, filePath, webpackConfig, cwd = process.cwd(), printMaxDepth, all }) => {
25
+ let deps, entryPoints;
26
+ if (_entryPoints.length > 0) {
27
+ entryPoints = _entryPoints;
28
+ deps = await (0, getDepsTree_1.getDepsTree)(cwd, entryPoints, webpackConfig);
29
+ }
30
+ else {
31
+ ;
32
+ [entryPoints, deps] = await (0, getEntryPoints_1.getEntryPoints)({ cwd });
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
+ if (printMaxDepth) {
38
+ forest.forEach(([tree]) => {
39
+ console.log('Max depth', ...(0, getMaxDepthInGrapth_1.getMaxDepth)()(tree));
40
+ });
41
+ }
42
+ const resolvedPaths = forest.reduce((allPaths, [_, fileNode]) => {
43
+ if (!fileNode) {
44
+ return [...allPaths, []];
45
+ }
46
+ const pathsForTree = resolvePathsToRoot(fileNode, all);
47
+ return [...allPaths, pathsForTree];
48
+ }, []);
49
+ return resolvedPaths;
50
+ };
51
+ exports.resolve = resolve;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDepsSetWebpack = void 0;
4
+ const dependency_cruiser_1 = require("dependency-cruiser");
5
+ const utils_1 = require("./utils");
6
+ // eslint-disable-next-line
7
+ const resolveWebpackConfig = require('dependency-cruiser/config-utl/extract-webpack-resolve-config');
8
+ const normalizeDepsTree = (modules) => {
9
+ const normalized = {};
10
+ const nonResolvableDeps = [];
11
+ modules.forEach((mod) => {
12
+ const { source, dependencies } = mod;
13
+ if (!nonResolvableDeps.includes(source)) {
14
+ normalized[source] = dependencies
15
+ .filter(({ couldNotResolve, resolved: id }) => {
16
+ if (couldNotResolve) {
17
+ nonResolvableDeps.push(id);
18
+ }
19
+ return !couldNotResolve;
20
+ })
21
+ .map(({ resolved, module }) => ({
22
+ id: resolved,
23
+ request: module
24
+ }));
25
+ }
26
+ });
27
+ return normalized;
28
+ };
29
+ const getDepsSetWebpack = (entryPoints, webpackConfigPath, cwd, skipRegex) => {
30
+ const skip = skipRegex || '(node_modules|/__tests__|/__test__|/__mockContent__|.scss)';
31
+ const webpackResolveOptions = webpackConfigPath
32
+ ? resolveWebpackConfig((0, utils_1.createResolveAbsolutePath)(cwd)(webpackConfigPath))
33
+ : null;
34
+ const result = (0, dependency_cruiser_1.cruise)(entryPoints, {
35
+ //@ts-ignore
36
+ exclude: skip,
37
+ //@ts-ignore
38
+ doNotFollow: { path: skip },
39
+ tsPreCompilationDeps: true,
40
+ baseDir: cwd
41
+ }, webpackResolveOptions);
42
+ return normalizeDepsTree(result.output.modules);
43
+ };
44
+ exports.getDepsSetWebpack = getDepsSetWebpack;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDepsTree = void 0;
4
+ const getDepsSetWebpack_1 = require("./getDepsSetWebpack");
5
+ const dpdm_1 = require("dpdm");
6
+ const cleanupDpdmDeps_1 = require("./cleanupDpdmDeps");
7
+ async function getDepsTree(cwd, entryPoints, webpackConfigPath) {
8
+ const deps = webpackConfigPath
9
+ ? (0, getDepsSetWebpack_1.getDepsSetWebpack)(entryPoints, webpackConfigPath, cwd)
10
+ : (0, cleanupDpdmDeps_1.cleanupDpdmDeps)(await (0, dpdm_1.parseDependencyTree)(entryPoints, {
11
+ context: cwd
12
+ }));
13
+ return deps;
14
+ }
15
+ exports.getDepsTree = getDepsTree;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getEntryPoints = exports.findEntryPointsInDepsTree = exports.getDirectoriesForEntryPointsSearch = void 0;
7
+ const minimatch_1 = __importDefault(require("minimatch"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const promises_1 = __importDefault(require("fs/promises"));
10
+ const utils_1 = require("./utils");
11
+ const getDepsTree_1 = require("./getDepsTree");
12
+ const getDirectoriesForEntryPointsSearch = async (dir) => {
13
+ const entries = await promises_1.default.readdir(dir);
14
+ const directories = await (0, utils_1.asyncFilter)(entries, async (pathName) => {
15
+ if (pathName === 'node_modules' || pathName.startsWith('.')) {
16
+ return false;
17
+ }
18
+ const stat = await promises_1.default.lstat(path_1.default.resolve(dir, pathName));
19
+ return stat.isDirectory();
20
+ });
21
+ const joinedWithDir = directories.map((pathName) => path_1.default.join(dir, pathName));
22
+ return [
23
+ ...joinedWithDir,
24
+ ...(await Promise.all(joinedWithDir.map(exports.getDirectoriesForEntryPointsSearch))).flat(1)
25
+ ];
26
+ };
27
+ exports.getDirectoriesForEntryPointsSearch = getDirectoriesForEntryPointsSearch;
28
+ const findEntryPointsInDepsTree = (deps, exclude = []) => {
29
+ const referencedIds = new Set();
30
+ Object.values(deps).forEach((entry) => {
31
+ if (entry !== null) {
32
+ entry.forEach(({ id }) => referencedIds.add(id));
33
+ }
34
+ });
35
+ return Object.keys(deps)
36
+ .filter((id) => /\.(ts|tsx|mjs|js|jsx)$/.test(id) &&
37
+ !/node_modules/.test(id) &&
38
+ !referencedIds.has(id))
39
+ .filter((id) => exclude.reduce((result, pattern) => result && !(0, minimatch_1.default)(id, pattern), true));
40
+ };
41
+ exports.findEntryPointsInDepsTree = findEntryPointsInDepsTree;
42
+ const getEntryPoints = async ({ cwd, exclude, webpackConfigPath }) => {
43
+ const dirs = await (0, exports.getDirectoriesForEntryPointsSearch)(cwd);
44
+ const globs = dirs
45
+ .map((dirName) => path_1.default.relative(cwd, dirName))
46
+ .map((dirName) => `${dirName}/*`);
47
+ const globsWithRoot = ['*', ...globs];
48
+ const depsTree = await (0, getDepsTree_1.getDepsTree)(cwd, globsWithRoot, webpackConfigPath);
49
+ const possibleEntryPoints = (0, exports.findEntryPointsInDepsTree)(depsTree, exclude);
50
+ return [possibleEntryPoints, depsTree];
51
+ };
52
+ exports.getEntryPoints = getEntryPoints;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getMaxDepth = void 0;
4
+ const getMaxDepth = (depth = 1, path = [], vertices = new Map()) => {
5
+ return (tree) => {
6
+ const depthFromCache = vertices.get(tree.path);
7
+ if (depthFromCache) {
8
+ return depthFromCache;
9
+ }
10
+ const newPath = [...path, tree.path];
11
+ if (tree.children.length === 0) {
12
+ return [depth, newPath];
13
+ }
14
+ const results = tree.children.map((0, exports.getMaxDepth)(depth + 1, newPath, vertices));
15
+ const maxChildDepth = Math.max(...results.map(([depth]) => depth));
16
+ const itemWithMaxDepth = results.find(([depth]) => depth === maxChildDepth);
17
+ vertices.set(tree.path, itemWithMaxDepth);
18
+ return itemWithMaxDepth;
19
+ };
20
+ };
21
+ exports.getMaxDepth = getMaxDepth;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.sanitizeUserEntryPoints = exports.asyncFilter = exports.createResolveAbsolutePath = exports.removeInitialDot = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const glob_escape_1 = __importDefault(require("glob-escape"));
9
+ const removeInitialDot = (path) => path.replace(/^\.\//, '');
10
+ exports.removeInitialDot = removeInitialDot;
11
+ const createResolveAbsolutePath = (cwd) => (p) => (typeof p === 'string' ? path_1.default.resolve(cwd, p) : p);
12
+ exports.createResolveAbsolutePath = createResolveAbsolutePath;
13
+ const asyncFilter = async (arr, predicate) => {
14
+ const results = await Promise.all(arr.map(predicate));
15
+ return arr.filter((_v, index) => results[index]);
16
+ };
17
+ exports.asyncFilter = asyncFilter;
18
+ const sanitizeUserEntryPoints = (entryPoints) => {
19
+ const globEscapedEntryPoints = entryPoints.map(glob_escape_1.default);
20
+ return globEscapedEntryPoints;
21
+ };
22
+ exports.sanitizeUserEntryPoints = sanitizeUserEntryPoints;
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "rev-dep",
3
- "version": "0.2.0",
3
+ "version": "1.0.0-alpha.0",
4
4
  "description": "Reverse dependency resolution tool built with dependency-cruiser",
5
- "main": "find.js",
6
- "bin": "cli.js",
5
+ "main": "lib/find.js",
6
+ "bin": "bin.js",
7
7
  "files": [
8
- "find.js",
9
- "cli.js",
10
- "getDepsSet.js"
8
+ "dist/**",
9
+ "lib/**",
10
+ "bin.js"
11
11
  ],
12
12
  "author": "Jakub Mazurek @jayu",
13
13
  "license": "MIT",
@@ -20,26 +20,37 @@
20
20
  "node": ">=10"
21
21
  },
22
22
  "scripts": {
23
- "lint": "eslint *.js",
24
- "lint:fix": "eslint --fix *.js",
25
- "format": "prettier --check *.js",
26
- "format:fix": "prettier --write *.js",
27
- "cli": "node cli.js",
23
+ "lint": "eslint --ext .js,.ts src",
24
+ "lint:fix": "yarn lint --fix",
25
+ "docs-gen": "node ./scripts/addDocsToReadme.js",
26
+ "dev": "node bin",
28
27
  "test": "jest",
29
- "release": "release-it"
28
+ "release": "release-it",
29
+ "build": "tsc",
30
+ "build:watch": "tsc --watch",
31
+ "typecheck": "tsc --noEmit"
30
32
  },
31
33
  "dependencies": {
34
+ "@types/dedent": "^0.7.0",
32
35
  "commander": "^6.1.0",
33
- "dependency-cruiser": "9.23.0"
36
+ "dedent": "^0.7.0",
37
+ "dependency-cruiser": "9.23.0",
38
+ "dpdm": "^3.8.0",
39
+ "glob-escape": "^0.0.2",
40
+ "minimatch": "^5.0.1"
34
41
  },
35
42
  "devDependencies": {
43
+ "@typescript-eslint/eslint-plugin": "^5.16.0",
44
+ "@typescript-eslint/parser": "^5.16.0",
36
45
  "eslint": "^7.11.0",
37
- "eslint-config-prettier": "^6.13.0",
46
+ "eslint-config-prettier": "^8.5.0",
38
47
  "eslint-plugin-jest": "^24.1.0",
39
48
  "eslint-plugin-node": "^11.1.0",
49
+ "eslint-plugin-prettier": "^4.0.0",
40
50
  "jest": "^26.5.3",
41
51
  "mock-fs": "^4.13.0",
42
52
  "prettier": "^2.1.2",
43
- "release-it": "^14.2.1"
53
+ "release-it": "^14.2.1",
54
+ "typescript": "^4.6.2"
44
55
  }
45
56
  }
package/cli.js DELETED
@@ -1,61 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- const package = require('./package.json')
4
- const { Command } = require('commander')
5
-
6
- const { find } = require('./find')
7
- const program = new Command('rev-dep')
8
- program.version(package.version, '-v, --version')
9
-
10
- const pathToString = (str, f, i) => {
11
- return `${str ? `${str}\n` : ''}${' '.repeat(i)} ➞ ${f}`
12
- }
13
-
14
- program
15
- .command('resolve <filePath> <entryPoints...>')
16
- .option(
17
- '-cs, --compactSummary',
18
- 'print a compact summary of reverse resolution with a count of found paths'
19
- )
20
- .option('--verbose', 'print current action information')
21
- .option(
22
- '-wc, --webpackConfig <path>',
23
- 'path to webpack config to enable webpack aliases support'
24
- )
25
- .action((filePath, entryPoints, data) => {
26
- const { compactSummary, verbose, webpackConfig } = data
27
- const results = find({
28
- entryPoints,
29
- filePath,
30
- verbose,
31
- webpackConfig
32
- })
33
- const hasAnyResults = results.some((paths) => paths.length > 0)
34
- if (!hasAnyResults) {
35
- console.log('No results found for', filePath, 'in', entryPoints)
36
- return
37
- }
38
- console.log('Results:\n')
39
- if (compactSummary) {
40
- const maxEntryLength = entryPoints.reduce((maxLength, entryPoint) => {
41
- return entryPoint.length > maxLength ? entryPoint.length : maxLength
42
- }, 0)
43
- let total = 0
44
- entryPoints.forEach((entry, index) => {
45
- console.log(`${entry.padEnd(maxEntryLength)} :`, results[index].length)
46
- total += results[index].length
47
- })
48
- console.log('\nTotal:', total)
49
- } else {
50
- results.forEach((entryPointResults, index) => {
51
- entryPointResults.forEach((path) => {
52
- console.log(path.reduce(pathToString, ''))
53
- })
54
- if (index < results.length - 1) {
55
- console.log('_'.repeat(process.stdout.columns))
56
- }
57
- })
58
- }
59
- })
60
-
61
- program.parse(process.argv)
package/find.js DELETED
@@ -1,80 +0,0 @@
1
- const path = require('path')
2
- const getDepsSet = require('./getDepsSet')
3
-
4
- const buildTree = (deps) => (entryPoint) => {
5
- const inner = (path) => {
6
- const dep = deps.find((d) => d.source === path)
7
- if (dep === undefined) {
8
- throw new Error(`Dependency '${path}' not found!`)
9
- }
10
-
11
- return {
12
- path,
13
- children: dep.dependencies.map((d) => {
14
- if (d.circular) {
15
- return { path: 'CIRCULAR', children: [] }
16
- }
17
- return inner(d.resolved)
18
- })
19
- }
20
- }
21
- return inner(entryPoint)
22
- }
23
-
24
- const traverse = (file) => (tree) => {
25
- if (tree.path === file) {
26
- return [[file]]
27
- } else {
28
- return tree.children
29
- .map(traverse(file)) // [ [[]],[[]],[[]] ]
30
- .filter((p) => p.length > 0)
31
- .map((pathsArr) => pathsArr.filter((p) => p.length > 0))
32
- .reduce((flat, subPath) => {
33
- return [...flat, ...subPath]
34
- }, [])
35
- .map((p) => [tree.path, ...p])
36
- }
37
- }
38
-
39
- const removeInitialDot = (path) => path.replace(/^\.\//, '')
40
-
41
- const _resolveAbsolutePath = (cwd) => (p) => typeof p === 'string' ? path.resolve(cwd, p) : p
42
-
43
- const find = ({
44
- entryPoints,
45
- filePath,
46
- skipRegex,
47
- verbose,
48
- webpackConfig,
49
- cwd = process.cwd()
50
- }) => {
51
- const resolveAbsolutePath = _resolveAbsolutePath(cwd)
52
- const absoluteEntryPoints = entryPoints.map(resolveAbsolutePath)
53
-
54
- if (verbose) {
55
- console.log('Entry points:')
56
- console.log(absoluteEntryPoints)
57
- console.log('Getting dependency set for entry points...')
58
- }
59
- const deps = getDepsSet(
60
- absoluteEntryPoints,
61
- skipRegex,
62
- resolveAbsolutePath(webpackConfig)
63
- )
64
- const cleanedEntryPoints = entryPoints.map(removeInitialDot)
65
- const cleanedFilePath = removeInitialDot(filePath)
66
- if (verbose) {
67
- console.log('Building dependency trees for entry points...')
68
- }
69
- const forest = cleanedEntryPoints.map(buildTree(deps))
70
- if (verbose) {
71
- console.log('Finding paths in dependency trees...')
72
- }
73
- const resolvedPaths = forest.reduce((allPaths, tree) => {
74
- const paths = traverse(cleanedFilePath)(tree)
75
- return [...allPaths, paths]
76
- }, [])
77
- return resolvedPaths
78
- }
79
-
80
- module.exports = { find }
package/getDepsSet.js DELETED
@@ -1,14 +0,0 @@
1
- const depcruise = require('dependency-cruiser').cruise
2
- const resolveWebpackConfig = require('dependency-cruiser/src/config-utl/extract-webpack-resolve-config')
3
- const getDepsSet = (entryPoints, skipRegex, webpackConfigPath) => {
4
- const skip =
5
- skipRegex || '(node_modules|/__tests__|/__test__|/__mockContent__|.scss)'
6
- const webpackResolveOptions = webpackConfigPath ? resolveWebpackConfig(webpackConfigPath) : null
7
- const result = depcruise(entryPoints, {
8
- exclude: skip,
9
- doNotFollow: { path: skip },
10
- }, webpackResolveOptions)
11
- return result.output.modules
12
- }
13
-
14
- module.exports = getDepsSet