importy 0.0.1 → 0.0.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/.github/workflows/publish.yml +3 -0
- package/{bin → dist}/index.js +24 -36
- package/package.json +10 -3
- package/src/index.ts +105 -0
- package/tsconfig.json +14 -0
- package/tsup.config.ts +12 -0
package/{bin → dist}/index.js
RENAMED
|
@@ -1,27 +1,23 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
+
// src/index.ts
|
|
3
4
|
import fs from "fs";
|
|
4
5
|
import path from "path";
|
|
5
6
|
import { program } from "commander";
|
|
6
7
|
import * as parser from "@babel/parser";
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
program
|
|
10
|
-
|
|
11
|
-
.requiredOption("-l, --lib <library>", "Library name to match")
|
|
12
|
-
.parse(process.argv);
|
|
13
|
-
|
|
14
|
-
const { dir, lib } = program.opts();
|
|
15
|
-
|
|
8
|
+
import _traverse from "@babel/traverse";
|
|
9
|
+
var traverse = _traverse.default ?? _traverse;
|
|
10
|
+
program.requiredOption("-d, --dir <directory>", "Directory to scan").requiredOption("-l, --lib <library>", "Library name to match").parse(process.argv);
|
|
11
|
+
var { dir, lib } = program.opts();
|
|
16
12
|
function isJavaScriptFile(file) {
|
|
17
13
|
return /\.(js|ts|jsx|tsx)$/.test(file);
|
|
18
14
|
}
|
|
19
|
-
|
|
20
15
|
function getAllFiles(dirPath, arrayOfFiles = []) {
|
|
21
16
|
const files = fs.readdirSync(dirPath);
|
|
22
17
|
for (const file of files) {
|
|
23
18
|
const fullPath = path.join(dirPath, file);
|
|
24
|
-
|
|
19
|
+
const stat = fs.statSync(fullPath);
|
|
20
|
+
if (stat.isDirectory()) {
|
|
25
21
|
getAllFiles(fullPath, arrayOfFiles);
|
|
26
22
|
} else if (isJavaScriptFile(fullPath)) {
|
|
27
23
|
arrayOfFiles.push(fullPath);
|
|
@@ -29,58 +25,50 @@ function getAllFiles(dirPath, arrayOfFiles = []) {
|
|
|
29
25
|
}
|
|
30
26
|
return arrayOfFiles;
|
|
31
27
|
}
|
|
32
|
-
|
|
33
28
|
function extractImportsFromFile(filePath, targetLib) {
|
|
34
|
-
const code = fs.readFileSync(filePath, "
|
|
29
|
+
const code = fs.readFileSync(filePath, "utf-8");
|
|
35
30
|
let ast;
|
|
36
31
|
try {
|
|
37
32
|
ast = parser.parse(code, {
|
|
38
33
|
sourceType: "module",
|
|
39
|
-
plugins: ["typescript", "jsx"]
|
|
34
|
+
plugins: ["typescript", "jsx"]
|
|
40
35
|
});
|
|
41
36
|
} catch (err) {
|
|
42
|
-
console.warn(`Skipping ${filePath}:
|
|
37
|
+
console.warn(`Skipping ${filePath}: Failed to parse`);
|
|
43
38
|
return [];
|
|
44
39
|
}
|
|
45
|
-
|
|
46
40
|
const matches = [];
|
|
47
|
-
|
|
48
41
|
traverse(ast, {
|
|
49
|
-
ImportDeclaration(
|
|
42
|
+
ImportDeclaration(path2) {
|
|
43
|
+
const node = path2.node;
|
|
50
44
|
if (node.source.value === targetLib) {
|
|
51
45
|
for (const specifier of node.specifiers) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
46
|
+
const importedName = "imported" in specifier && specifier.imported ? specifier.imported.name : "default";
|
|
47
|
+
const localName = specifier.local.name;
|
|
55
48
|
matches.push({
|
|
56
49
|
importedName,
|
|
57
50
|
localName,
|
|
58
|
-
file: filePath
|
|
51
|
+
file: filePath
|
|
59
52
|
});
|
|
60
53
|
}
|
|
61
54
|
}
|
|
62
|
-
}
|
|
55
|
+
}
|
|
63
56
|
});
|
|
64
|
-
|
|
65
57
|
return matches;
|
|
66
58
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
for (const file of files) {
|
|
59
|
+
var allFiles = getAllFiles(dir);
|
|
60
|
+
var componentMap = {};
|
|
61
|
+
for (const file of allFiles) {
|
|
72
62
|
const imports = extractImportsFromFile(file, lib);
|
|
73
63
|
for (const { importedName, file: filePath } of imports) {
|
|
74
64
|
if (!componentMap[importedName]) {
|
|
75
|
-
componentMap[importedName] = new Set();
|
|
65
|
+
componentMap[importedName] = /* @__PURE__ */ new Set();
|
|
76
66
|
}
|
|
77
67
|
componentMap[importedName].add(filePath);
|
|
78
68
|
}
|
|
79
69
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
);
|
|
85
|
-
|
|
70
|
+
var output = {};
|
|
71
|
+
for (const [component, files] of Object.entries(componentMap)) {
|
|
72
|
+
output[component] = [...files];
|
|
73
|
+
}
|
|
86
74
|
console.log(JSON.stringify(output, null, 2));
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "importy",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
6
7
|
"bin": {
|
|
7
|
-
"inspect-imports": "./
|
|
8
|
+
"inspect-imports": "./dist/index.js"
|
|
8
9
|
},
|
|
9
10
|
"keywords": [],
|
|
10
11
|
"author": "Taras Shevchuk",
|
|
@@ -13,11 +14,17 @@
|
|
|
13
14
|
"registry": "https://registry.npmjs.org/"
|
|
14
15
|
},
|
|
15
16
|
"devDependencies": {
|
|
16
|
-
"@types
|
|
17
|
+
"@babel/types": "^7.27.3",
|
|
18
|
+
"@types/node": "^22.15.29",
|
|
19
|
+
"tsup": "^8.5.0",
|
|
20
|
+
"typescript": "^5.8.3"
|
|
17
21
|
},
|
|
18
22
|
"dependencies": {
|
|
19
23
|
"@babel/parser": "^7.27.5",
|
|
20
24
|
"@babel/traverse": "^7.27.4",
|
|
21
25
|
"commander": "^14.0.0"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsup"
|
|
22
29
|
}
|
|
23
30
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { program } from 'commander';
|
|
4
|
+
import * as parser from '@babel/parser';
|
|
5
|
+
|
|
6
|
+
import _traverse from '@babel/traverse';
|
|
7
|
+
const traverse = (_traverse as any).default ?? _traverse;
|
|
8
|
+
import type { ImportDeclaration } from '@babel/types';
|
|
9
|
+
|
|
10
|
+
// Define CLI options
|
|
11
|
+
program
|
|
12
|
+
.requiredOption('-d, --dir <directory>', 'Directory to scan')
|
|
13
|
+
.requiredOption('-l, --lib <library>', 'Library name to match')
|
|
14
|
+
.parse(process.argv);
|
|
15
|
+
|
|
16
|
+
const { dir, lib } = program.opts<{
|
|
17
|
+
dir: string;
|
|
18
|
+
lib: string;
|
|
19
|
+
}>();
|
|
20
|
+
|
|
21
|
+
// Helpers
|
|
22
|
+
function isJavaScriptFile(file: string): boolean {
|
|
23
|
+
return /\.(js|ts|jsx|tsx)$/.test(file);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function getAllFiles(dirPath: string, arrayOfFiles: string[] = []): string[] {
|
|
27
|
+
const files = fs.readdirSync(dirPath);
|
|
28
|
+
for (const file of files) {
|
|
29
|
+
const fullPath = path.join(dirPath, file);
|
|
30
|
+
const stat = fs.statSync(fullPath);
|
|
31
|
+
if (stat.isDirectory()) {
|
|
32
|
+
getAllFiles(fullPath, arrayOfFiles);
|
|
33
|
+
} else if (isJavaScriptFile(fullPath)) {
|
|
34
|
+
arrayOfFiles.push(fullPath);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return arrayOfFiles;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
type ImportMatch = {
|
|
41
|
+
importedName: string;
|
|
42
|
+
localName: string;
|
|
43
|
+
file: string;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
function extractImportsFromFile(filePath: string, targetLib: string): ImportMatch[] {
|
|
47
|
+
const code = fs.readFileSync(filePath, 'utf-8');
|
|
48
|
+
let ast;
|
|
49
|
+
try {
|
|
50
|
+
ast = parser.parse(code, {
|
|
51
|
+
sourceType: 'module',
|
|
52
|
+
plugins: ['typescript', 'jsx'],
|
|
53
|
+
});
|
|
54
|
+
} catch (err) {
|
|
55
|
+
console.warn(`Skipping ${filePath}: Failed to parse`);
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const matches: ImportMatch[] = [];
|
|
60
|
+
|
|
61
|
+
traverse(ast, {
|
|
62
|
+
ImportDeclaration(path: any) {
|
|
63
|
+
const node = path.node as ImportDeclaration;
|
|
64
|
+
if (node.source.value === targetLib) {
|
|
65
|
+
for (const specifier of node.specifiers) {
|
|
66
|
+
const importedName =
|
|
67
|
+
'imported' in specifier && specifier.imported
|
|
68
|
+
? specifier.imported.name as any
|
|
69
|
+
: 'default';
|
|
70
|
+
const localName = specifier.local.name;
|
|
71
|
+
|
|
72
|
+
matches.push({
|
|
73
|
+
importedName,
|
|
74
|
+
localName,
|
|
75
|
+
file: filePath,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
return matches;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Process all files and build result
|
|
86
|
+
const allFiles = getAllFiles(dir);
|
|
87
|
+
const componentMap: Record<string, Set<string>> = {};
|
|
88
|
+
|
|
89
|
+
for (const file of allFiles) {
|
|
90
|
+
const imports = extractImportsFromFile(file, lib);
|
|
91
|
+
for (const { importedName, file: filePath } of imports) {
|
|
92
|
+
if (!componentMap[importedName]) {
|
|
93
|
+
componentMap[importedName] = new Set();
|
|
94
|
+
}
|
|
95
|
+
componentMap[importedName].add(filePath);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Final output
|
|
100
|
+
const output: Record<string, string[]> = {};
|
|
101
|
+
for (const [component, files] of Object.entries(componentMap)) {
|
|
102
|
+
output[component] = [...files];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
console.log(JSON.stringify(output, null, 2));
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "Node",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"forceConsistentCasingInFileNames": true,
|
|
8
|
+
"verbatimModuleSyntax": true,
|
|
9
|
+
"strict": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"outDir": "dist"
|
|
12
|
+
},
|
|
13
|
+
"include": ["src"]
|
|
14
|
+
}
|
package/tsup.config.ts
ADDED