imports-detector 0.1.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.
@@ -0,0 +1,221 @@
1
+ import path from 'path';
2
+ import { FileDiscovery } from './file-discovery.js';
3
+ import { CodeParser } from './parser.js';
4
+ import { ImportExtractor } from './extractor.js';
5
+ /**
6
+ * Main Analyzer Class
7
+ * Orchestrates file discovery, parsing, and import extraction
8
+ */
9
+ export class ImportAnalyzer {
10
+ fileDiscovery;
11
+ parser;
12
+ extractor;
13
+ options;
14
+ constructor(options = {}) {
15
+ this.options = {
16
+ detectStatic: options.detectStatic !== false,
17
+ detectDynamic: options.detectDynamic !== false,
18
+ detectLazy: options.detectLazy !== false,
19
+ detectRequire: options.detectRequire !== false,
20
+ verbose: options.verbose || false,
21
+ modulePath: options.modulePath,
22
+ };
23
+ this.fileDiscovery = new FileDiscovery({
24
+ includePatterns: options.includeExtensions
25
+ ? options.includeExtensions.map((ext) => `**/*${ext}`)
26
+ : undefined,
27
+ excludePatterns: options.excludePatterns,
28
+ });
29
+ this.parser = new CodeParser();
30
+ this.extractor = new ImportExtractor();
31
+ }
32
+ /**
33
+ * Find all files that import a specific module
34
+ * @param moduleName - Name of the module to search for (e.g., 'Test', 'react')
35
+ * @param searchPath - Root directory to search
36
+ */
37
+ async findFilesImporting(moduleName, searchPath) {
38
+ const results = [];
39
+ try {
40
+ const files = await this.fileDiscovery.findFiles(searchPath);
41
+ if (this.options.verbose) {
42
+ console.log(`Found ${files.length} files to analyze`);
43
+ }
44
+ for (const filePath of files) {
45
+ try {
46
+ const ast = this.parser.parseFile(filePath);
47
+ const allImports = this.extractor.getAllImports(ast);
48
+ // Filter imports that match the module name
49
+ const matchingImports = this.filterImportsByModule(allImports, moduleName);
50
+ if (matchingImports.length > 0) {
51
+ results.push({
52
+ file: filePath,
53
+ imports: matchingImports,
54
+ });
55
+ }
56
+ }
57
+ catch (error) {
58
+ if (this.options.verbose) {
59
+ console.error(`Error parsing ${filePath}:`, error);
60
+ }
61
+ }
62
+ }
63
+ return results;
64
+ }
65
+ catch (error) {
66
+ throw new Error(`Failed to find files importing "${moduleName}": ${error}`);
67
+ }
68
+ }
69
+ /**
70
+ * Analyze all imports in a project
71
+ * @param searchPath - Root directory to analyze
72
+ */
73
+ async analyzeProject(searchPath) {
74
+ try {
75
+ const files = await this.fileDiscovery.findFiles(searchPath);
76
+ const filesMap = {};
77
+ let totalStatic = 0;
78
+ let totalDynamic = 0;
79
+ let totalLazy = 0;
80
+ let totalRequire = 0;
81
+ for (const filePath of files) {
82
+ try {
83
+ const ast = this.parser.parseFile(filePath);
84
+ const fileImports = this.extractor.extractAll(ast, {
85
+ static: this.options.detectStatic,
86
+ dynamic: this.options.detectDynamic,
87
+ lazy: this.options.detectLazy,
88
+ require: this.options.detectRequire,
89
+ });
90
+ filesMap[filePath] = fileImports;
91
+ totalStatic += fileImports.static.length;
92
+ totalDynamic += fileImports.dynamic.length;
93
+ totalLazy += fileImports.lazy.length;
94
+ totalRequire += fileImports.require.length;
95
+ }
96
+ catch (error) {
97
+ if (this.options.verbose) {
98
+ console.error(`Error parsing ${filePath}:`, error);
99
+ }
100
+ }
101
+ }
102
+ const totalImports = totalStatic + totalDynamic + totalLazy + totalRequire;
103
+ return {
104
+ totalFiles: Object.keys(filesMap).length,
105
+ totalImports,
106
+ files: filesMap,
107
+ summary: {
108
+ static: totalStatic,
109
+ dynamic: totalDynamic,
110
+ lazy: totalLazy,
111
+ require: totalRequire,
112
+ },
113
+ };
114
+ }
115
+ catch (error) {
116
+ throw new Error(`Failed to analyze project: ${error}`);
117
+ }
118
+ }
119
+ /**
120
+ * Get all imports from a specific file
121
+ */
122
+ analyzeFile(filePath) {
123
+ try {
124
+ const ast = this.parser.parseFile(filePath);
125
+ return this.extractor.extractAll(ast, {
126
+ static: this.options.detectStatic,
127
+ dynamic: this.options.detectDynamic,
128
+ lazy: this.options.detectLazy,
129
+ require: this.options.detectRequire,
130
+ });
131
+ }
132
+ catch (error) {
133
+ throw new Error(`Failed to analyze file ${filePath}: ${error}`);
134
+ }
135
+ }
136
+ /**
137
+ * Filter imports by module name
138
+ * Handles both named imports and file paths
139
+ * If modulePath is specified in options, also filters by import path
140
+ */
141
+ filterImportsByModule(imports, moduleName) {
142
+ return imports.filter((imp) => {
143
+ // If modulePath is specified, check if the import module matches the path
144
+ if (this.options.modulePath) {
145
+ return this.doesImportMatchPath(imp.module, this.options.modulePath, moduleName);
146
+ }
147
+ // Check if module name matches exactly
148
+ if (imp.module === moduleName) {
149
+ return true;
150
+ }
151
+ // Check if the module is imported as a named specifier
152
+ if (imp.specifiers && imp.specifiers.includes(moduleName)) {
153
+ return true;
154
+ }
155
+ // Check if module path ends with the module name
156
+ // This handles cases like './components/Test'
157
+ const moduleBasename = path.basename(imp.module).replace(/\.(js|jsx|ts|tsx)$/, '');
158
+ if (moduleBasename === moduleName) {
159
+ return true;
160
+ }
161
+ return false;
162
+ });
163
+ }
164
+ /**
165
+ * Check if an import module matches a specific path
166
+ * Supports both relative and absolute path matching
167
+ */
168
+ doesImportMatchPath(importModule, targetPath, moduleName) {
169
+ // Normalize paths for comparison
170
+ const normalizedImport = this.normalizeModulePath(importModule);
171
+ const normalizedTarget = this.normalizeModulePath(targetPath);
172
+ // Check if paths match (handle various path formats)
173
+ const importPath = normalizedImport.toLowerCase();
174
+ const targetPathLower = normalizedTarget.toLowerCase();
175
+ // Direct match
176
+ if (importPath === targetPathLower) {
177
+ return true;
178
+ }
179
+ // Check if the target path is contained in the import path
180
+ // This handles cases like:
181
+ // - "admin/dashboard" matches "src/admin/dashboard"
182
+ if (importPath.includes(targetPathLower)) {
183
+ return true;
184
+ }
185
+ // Check if the import path is contained in the target path
186
+ // This handles cases like:
187
+ // - "../admin/dashboard" matches "admin/dashboard" (after normalization)
188
+ if (targetPathLower.includes(importPath)) {
189
+ return true;
190
+ }
191
+ // Check if path segments match (more strict comparison)
192
+ const importSegments = importPath.split('/').filter(s => s);
193
+ const targetSegments = targetPathLower.split('/').filter(s => s);
194
+ // Must match at least the target path segments
195
+ if (targetSegments.length > 0) {
196
+ // Check if all target segments appear in order in the import path
197
+ let targetIndex = 0;
198
+ for (const importSegment of importSegments) {
199
+ if (importSegment === targetSegments[targetIndex]) {
200
+ targetIndex++;
201
+ if (targetIndex === targetSegments.length) {
202
+ // All target segments matched
203
+ return true;
204
+ }
205
+ }
206
+ }
207
+ }
208
+ return false;
209
+ }
210
+ /**
211
+ * Normalize module path for comparison
212
+ * Removes file extensions, leading ./ or ../, and normalizes separators
213
+ */
214
+ normalizeModulePath(modulePath) {
215
+ return modulePath
216
+ .replace(/^\.+\//g, '') // Remove leading ./, ../, etc.
217
+ .replace(/\.(js|jsx|ts|tsx|json)$/, '') // Remove file extensions
218
+ .replace(/\\/g, '/'); // Normalize path separators
219
+ }
220
+ }
221
+ //# sourceMappingURL=analyzer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"analyzer.js","sourceRoot":"","sources":["../src/analyzer.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AASjD;;;GAGG;AACH,MAAM,OAAO,cAAc;IACjB,aAAa,CAAgB;IAC7B,MAAM,CAAa;IACnB,SAAS,CAAkB;IAC3B,OAAO,CAAkB;IAEjC,YAAY,UAA2B,EAAE;QACvC,IAAI,CAAC,OAAO,GAAG;YACb,YAAY,EAAE,OAAO,CAAC,YAAY,KAAK,KAAK;YAC5C,aAAa,EAAE,OAAO,CAAC,aAAa,KAAK,KAAK;YAC9C,UAAU,EAAE,OAAO,CAAC,UAAU,KAAK,KAAK;YACxC,aAAa,EAAE,OAAO,CAAC,aAAa,KAAK,KAAK;YAC9C,OAAO,EAAE,OAAO,CAAC,OAAO,IAAI,KA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;QACH,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;;OAGG;IACK,mBAAmB,CAAC,UAAkB;QAC5C,OAAO,UAAU;aACd,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,+BAA+B;aACtD,OAAO,CAAC,yBAAyB,EAAE,EAAE,CAAC,CAAC,yBAAyB;aAChE,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,4BAA4B;IACtD,CAAC;CACF"}
package/dist/cli.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
package/dist/cli.js ADDED
@@ -0,0 +1,180 @@
1
+ #!/usr/bin/env node
2
+ import { Command } from 'commander';
3
+ import { ImportAnalyzer } from './analyzer.js';
4
+ import { JSONGenerator } from './output/json-generator.js';
5
+ import { TextGenerator } from './output/text-generator.js';
6
+ import fs from 'fs';
7
+ const program = new Command();
8
+ program
9
+ .name('imports-detector')
10
+ .description('Detect and analyze imports in JavaScript/TypeScript applications')
11
+ .version('0.1.0');
12
+ /**
13
+ * Parse detector options from CLI flags
14
+ */
15
+ function parseDetectorOptions(options) {
16
+ return {
17
+ detectStatic: options.static !== false,
18
+ detectDynamic: options.dynamic !== false,
19
+ detectLazy: options.lazy !== false,
20
+ detectRequire: options.require !== false,
21
+ verbose: options.verbose || false,
22
+ includeExtensions: options.include ? options.include.split(',') : undefined,
23
+ excludePatterns: options.exclude ? options.exclude.split(',') : undefined,
24
+ modulePath: options.modulePath,
25
+ };
26
+ }
27
+ /**
28
+ * Find command - Find files importing a specific module
29
+ */
30
+ program
31
+ .command('find')
32
+ .description('Find all files that import a specific module')
33
+ .argument('<module>', 'Name of the module to search for')
34
+ .argument('[paths...]', 'Paths to search (default: current directory)')
35
+ .option('-p, --path <path>', 'Root directory to search')
36
+ .option('-o, --output <file>', 'Output file path')
37
+ .option('-f, --format <format>', 'Output format: json or text (default: text)')
38
+ .option('--module-path <path>', 'Specific module path to match (e.g., ./admin/Dashboard)')
39
+ .option('--include <patterns>', 'File extensions to include (comma-separated)')
40
+ .option('--exclude <patterns>', 'Patterns to exclude (comma-separated)')
41
+ .option('--no-static', 'Exclude static imports')
42
+ .option('--no-dynamic', 'Exclude dynamic imports')
43
+ .option('--no-lazy', 'Exclude lazy imports')
44
+ .option('--no-require', 'Exclude require calls')
45
+ .option('-v, --verbose', 'Verbose output')
46
+ .action(async (moduleName, paths, options) => {
47
+ try {
48
+ const searchPath = options.path || paths[0] || process.cwd();
49
+ const detectorOptions = parseDetectorOptions(options);
50
+ const format = options.format || 'text';
51
+ if (detectorOptions.verbose) {
52
+ console.error(`Searching for "${moduleName}" in ${searchPath}...`);
53
+ }
54
+ const analyzer = new ImportAnalyzer(detectorOptions);
55
+ const results = await analyzer.findFilesImporting(moduleName, searchPath);
56
+ let output;
57
+ if (format === 'json') {
58
+ const generator = new JSONGenerator();
59
+ output = generator.generateFromFindResults(results, moduleName);
60
+ }
61
+ else {
62
+ const generator = new TextGenerator();
63
+ output = generator.generateFromFindResults(results, moduleName);
64
+ }
65
+ if (options.output) {
66
+ fs.writeFileSync(options.output, output, 'utf-8');
67
+ console.log(`Output written to ${options.output}`);
68
+ }
69
+ else {
70
+ console.log(output);
71
+ }
72
+ process.exit(results.length > 0 ? 0 : 1);
73
+ }
74
+ catch (error) {
75
+ console.error(`Error: ${error}`);
76
+ process.exit(1);
77
+ }
78
+ });
79
+ /**
80
+ * List command - List all imports in each file
81
+ */
82
+ program
83
+ .command('list')
84
+ .description('List all imports in each file')
85
+ .argument('[paths...]', 'Paths to analyze (default: current directory)')
86
+ .option('-p, --path <path>', 'Root directory to analyze')
87
+ .option('-o, --output <file>', 'Output file path')
88
+ .option('-f, --format <format>', 'Output format: json or text (default: text)')
89
+ .option('--include <patterns>', 'File extensions to include (comma-separated)')
90
+ .option('--exclude <patterns>', 'Patterns to exclude (comma-separated)')
91
+ .option('--no-static', 'Exclude static imports')
92
+ .option('--no-dynamic', 'Exclude dynamic imports')
93
+ .option('--no-lazy', 'Exclude lazy imports')
94
+ .option('--no-require', 'Exclude require calls')
95
+ .option('-v, --verbose', 'Verbose output')
96
+ .action(async (paths, options) => {
97
+ try {
98
+ const searchPath = options.path || paths[0] || process.cwd();
99
+ const detectorOptions = parseDetectorOptions(options);
100
+ const format = options.format || 'text';
101
+ if (detectorOptions.verbose) {
102
+ console.error(`Analyzing imports in ${searchPath}...`);
103
+ }
104
+ const analyzer = new ImportAnalyzer(detectorOptions);
105
+ const analysis = await analyzer.analyzeProject(searchPath);
106
+ let output;
107
+ if (format === 'json') {
108
+ const generator = new JSONGenerator();
109
+ output = generator.generateFromAnalysis(analysis);
110
+ }
111
+ else {
112
+ const generator = new TextGenerator();
113
+ output = generator.generateFromAnalysis(analysis);
114
+ }
115
+ if (options.output) {
116
+ fs.writeFileSync(options.output, output, 'utf-8');
117
+ console.log(`Output written to ${options.output}`);
118
+ }
119
+ else {
120
+ console.log(output);
121
+ }
122
+ process.exit(0);
123
+ }
124
+ catch (error) {
125
+ console.error(`Error: ${error}`);
126
+ process.exit(1);
127
+ }
128
+ });
129
+ /**
130
+ * Report command - Generate a detailed report
131
+ */
132
+ program
133
+ .command('report')
134
+ .description('Generate a detailed import analysis report')
135
+ .argument('[paths...]', 'Paths to analyze (default: current directory)')
136
+ .option('-p, --path <path>', 'Root directory to analyze')
137
+ .option('-o, --output <file>', 'Output file path (required)')
138
+ .option('-f, --format <format>', 'Output format: json or text (default: text)')
139
+ .option('--include <patterns>', 'File extensions to include (comma-separated)')
140
+ .option('--exclude <patterns>', 'Patterns to exclude (comma-separated)')
141
+ .option('--no-static', 'Exclude static imports')
142
+ .option('--no-dynamic', 'Exclude dynamic imports')
143
+ .option('--no-lazy', 'Exclude lazy imports')
144
+ .option('--no-require', 'Exclude require calls')
145
+ .option('-v, --verbose', 'Verbose output')
146
+ .action(async (paths, options) => {
147
+ try {
148
+ const searchPath = options.path || paths[0] || process.cwd();
149
+ const detectorOptions = parseDetectorOptions(options);
150
+ const format = options.format || 'text';
151
+ if (!options.output) {
152
+ console.error('Error: --output is required for report command');
153
+ process.exit(1);
154
+ }
155
+ if (detectorOptions.verbose) {
156
+ console.error(`Generating report for ${searchPath}...`);
157
+ }
158
+ const analyzer = new ImportAnalyzer(detectorOptions);
159
+ const analysis = await analyzer.analyzeProject(searchPath);
160
+ let output;
161
+ if (format === 'json') {
162
+ const generator = new JSONGenerator();
163
+ output = generator.generateFromAnalysis(analysis);
164
+ }
165
+ else {
166
+ const generator = new TextGenerator();
167
+ output = generator.generateFromAnalysis(analysis);
168
+ }
169
+ fs.writeFileSync(options.output, output, 'utf-8');
170
+ console.log(`Report written to ${options.output}`);
171
+ process.exit(0);
172
+ }
173
+ catch (error) {
174
+ console.error(`Error: ${error}`);
175
+ process.exit(1);
176
+ }
177
+ });
178
+ // Parse and execute
179
+ program.parse();
180
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,OAAO,EAAE,MAAM,IAAI,CAAC;AAGpB,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE,CAAC;AAE9B,OAAO;KACJ,IAAI,CAAC,kBAAkB,CAAC;KACxB,WAAW,CAAC,kEAAkE,CAAC;KAC/E,OAAO,CAAC,OAAO,CAAC,CAAC;AAEpB;;GAEG;AACH,SAAS,oBAAoB,CAAC,OAAY;IACxC,OAAO;QACL,YAAY,EAAE,OAAO,CAAC,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AE,CAAC;YACtC,MAAM,GAAG,SAAS,CAAC,oBAAoB,CAAC,QAAQ,CAAC,CAAC;QACpD,CAAC;aAAM,CAAC;YACN,MAAM,SAAS,GAAG,IAAI,aAAa,EAAE,CAAC;YACtC,MAAM,GAAG,SAAS,CAAC,oBAAoB,CAAC,QAAQ,CAAC,CAAC;QACpD,CAAC;QAED,EAAE,CAAC,aAAa,CAAC,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QAClD,OAAO,CAAC,GAAG,CAAC,qBAAqB,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;QAEnD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,UAAU,KAAK,EAAE,CAAC,CAAC;QACjC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC,CAAC,CAAC;AAEL,oBAAoB;AACpB,OAAO,CAAC,KAAK,EAAE,CAAC"}
@@ -0,0 +1,26 @@
1
+ import type { File } from '@babel/types';
2
+ import { Import, FileImports } from './types.js';
3
+ /**
4
+ * Import Extractor Module
5
+ * Extracts all types of imports from AST
6
+ */
7
+ export declare class ImportExtractor {
8
+ private extractStaticImports;
9
+ private extractDynamicImports;
10
+ private extractRequireCalls;
11
+ private extractLazyImports;
12
+ /**
13
+ * Extract all imports from AST
14
+ */
15
+ extractAll(ast: File, options?: {
16
+ static?: boolean;
17
+ dynamic?: boolean;
18
+ lazy?: boolean;
19
+ require?: boolean;
20
+ }): FileImports;
21
+ /**
22
+ * Get all imports from a file (flattened)
23
+ */
24
+ getAllImports(ast: File): Import[];
25
+ }
26
+ //# sourceMappingURL=extractor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extractor.d.ts","sourceRoot":"","sources":["../src/extractor.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,MAAM,EAAc,WAAW,EAAE,MAAM,YAAY,CAAC;AAK7D;;;GAGG;AACH,qBAAa,eAAe;IAC1B,OAAO,CAAC,oBAAoB;IA2C5B,OAAO,CAAC,qBAAqB;IA8C7B,OAAO,CAAC,mBAAmB;IA2B3B,OAAO,CAAC,kBAAkB;IAyG1B;;OAEG;IACH,UAAU,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE;QAC9B,MAAM,CAAC,EAAE,OAAO,CAAC;QACjB,OAAO,CAAC,EAAE,OAAO,CAAC;QAClB,IAAI,CAAC,EAAE,OAAO,CAAC;QACf,OAAO,CAAC,EAAE,OAAO,CAAC;KACnB,GAAG,WAAW;IAmCf;;OAEG;IACH,aAAa,CAAC,GAAG,EAAE,IAAI,GAAG,MAAM,EAAE;CASnC"}