importy 0.0.1 → 0.0.4

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.
@@ -27,6 +27,9 @@ jobs:
27
27
  - name: Install dependencies
28
28
  run: pnpm install
29
29
 
30
+ - name: Build
31
+ run: pnpm build
32
+
30
33
  - name: Publish to npm
31
34
  run: pnpm publish --no-git-checks
32
35
  env:
@@ -1,27 +1,22 @@
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
8
  import traverse from "@babel/traverse";
8
-
9
- program
10
- .requiredOption("-d, --dir <directory>", "Directory to scan")
11
- .requiredOption("-l, --lib <library>", "Library name to match")
12
- .parse(process.argv);
13
-
14
- const { dir, lib } = program.opts();
15
-
9
+ program.requiredOption("-d, --dir <directory>", "Directory to scan").requiredOption("-l, --lib <library>", "Library name to match").parse(process.argv);
10
+ var { dir, lib } = program.opts();
16
11
  function isJavaScriptFile(file) {
17
12
  return /\.(js|ts|jsx|tsx)$/.test(file);
18
13
  }
19
-
20
14
  function getAllFiles(dirPath, arrayOfFiles = []) {
21
15
  const files = fs.readdirSync(dirPath);
22
16
  for (const file of files) {
23
17
  const fullPath = path.join(dirPath, file);
24
- if (fs.statSync(fullPath).isDirectory()) {
18
+ const stat = fs.statSync(fullPath);
19
+ if (stat.isDirectory()) {
25
20
  getAllFiles(fullPath, arrayOfFiles);
26
21
  } else if (isJavaScriptFile(fullPath)) {
27
22
  arrayOfFiles.push(fullPath);
@@ -29,58 +24,50 @@ function getAllFiles(dirPath, arrayOfFiles = []) {
29
24
  }
30
25
  return arrayOfFiles;
31
26
  }
32
-
33
27
  function extractImportsFromFile(filePath, targetLib) {
34
- const code = fs.readFileSync(filePath, "utf8");
28
+ const code = fs.readFileSync(filePath, "utf-8");
35
29
  let ast;
36
30
  try {
37
31
  ast = parser.parse(code, {
38
32
  sourceType: "module",
39
- plugins: ["typescript", "jsx"],
33
+ plugins: ["typescript", "jsx"]
40
34
  });
41
35
  } catch (err) {
42
- console.warn(`Skipping ${filePath}: Parse error`);
36
+ console.warn(`Skipping ${filePath}: Failed to parse`);
43
37
  return [];
44
38
  }
45
-
46
39
  const matches = [];
47
-
48
40
  traverse(ast, {
49
- ImportDeclaration({ node }) {
41
+ ImportDeclaration(path2) {
42
+ const node = path2.node;
50
43
  if (node.source.value === targetLib) {
51
44
  for (const specifier of node.specifiers) {
52
- let importedName = specifier.imported?.name || "default";
53
- let localName = specifier.local.name;
54
-
45
+ const importedName = "imported" in specifier && specifier.imported ? specifier.imported.name : "default";
46
+ const localName = specifier.local.name;
55
47
  matches.push({
56
48
  importedName,
57
49
  localName,
58
- file: filePath,
50
+ file: filePath
59
51
  });
60
52
  }
61
53
  }
62
- },
54
+ }
63
55
  });
64
-
65
56
  return matches;
66
57
  }
67
-
68
- const files = getAllFiles(dir);
69
- const componentMap = {};
70
-
71
- for (const file of files) {
58
+ var allFiles = getAllFiles(dir);
59
+ var componentMap = {};
60
+ for (const file of allFiles) {
72
61
  const imports = extractImportsFromFile(file, lib);
73
62
  for (const { importedName, file: filePath } of imports) {
74
63
  if (!componentMap[importedName]) {
75
- componentMap[importedName] = new Set();
64
+ componentMap[importedName] = /* @__PURE__ */ new Set();
76
65
  }
77
66
  componentMap[importedName].add(filePath);
78
67
  }
79
68
  }
80
-
81
- // Convert Set to array for output
82
- const output = Object.fromEntries(
83
- Object.entries(componentMap).map(([name, paths]) => [name, [...paths]]),
84
- );
85
-
69
+ var output = {};
70
+ for (const [component, files] of Object.entries(componentMap)) {
71
+ output[component] = [...files];
72
+ }
86
73
  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.1",
3
+ "version": "0.0.4",
4
4
  "description": "",
5
5
  "type": "module",
6
+ "main": "./dist/index.js",
6
7
  "bin": {
7
- "inspect-imports": "./bin/index.js"
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/node": "^22.15.29"
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,103 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { program } from 'commander';
4
+ import * as parser from '@babel/parser';
5
+ import traverse from '@babel/traverse';
6
+ import type { ImportDeclaration } from '@babel/types';
7
+
8
+ // Define CLI options
9
+ program
10
+ .requiredOption('-d, --dir <directory>', 'Directory to scan')
11
+ .requiredOption('-l, --lib <library>', 'Library name to match')
12
+ .parse(process.argv);
13
+
14
+ const { dir, lib } = program.opts<{
15
+ dir: string;
16
+ lib: string;
17
+ }>();
18
+
19
+ // Helpers
20
+ function isJavaScriptFile(file: string): boolean {
21
+ return /\.(js|ts|jsx|tsx)$/.test(file);
22
+ }
23
+
24
+ function getAllFiles(dirPath: string, arrayOfFiles: string[] = []): string[] {
25
+ const files = fs.readdirSync(dirPath);
26
+ for (const file of files) {
27
+ const fullPath = path.join(dirPath, file);
28
+ const stat = fs.statSync(fullPath);
29
+ if (stat.isDirectory()) {
30
+ getAllFiles(fullPath, arrayOfFiles);
31
+ } else if (isJavaScriptFile(fullPath)) {
32
+ arrayOfFiles.push(fullPath);
33
+ }
34
+ }
35
+ return arrayOfFiles;
36
+ }
37
+
38
+ type ImportMatch = {
39
+ importedName: string;
40
+ localName: string;
41
+ file: string;
42
+ };
43
+
44
+ function extractImportsFromFile(filePath: string, targetLib: string): ImportMatch[] {
45
+ const code = fs.readFileSync(filePath, 'utf-8');
46
+ let ast;
47
+ try {
48
+ ast = parser.parse(code, {
49
+ sourceType: 'module',
50
+ plugins: ['typescript', 'jsx'],
51
+ });
52
+ } catch (err) {
53
+ console.warn(`Skipping ${filePath}: Failed to parse`);
54
+ return [];
55
+ }
56
+
57
+ const matches: ImportMatch[] = [];
58
+
59
+ traverse(ast, {
60
+ ImportDeclaration(path: any) {
61
+ const node = path.node as ImportDeclaration;
62
+ if (node.source.value === targetLib) {
63
+ for (const specifier of node.specifiers) {
64
+ const importedName =
65
+ 'imported' in specifier && specifier.imported
66
+ ? specifier.imported.name as any
67
+ : 'default';
68
+ const localName = specifier.local.name;
69
+
70
+ matches.push({
71
+ importedName,
72
+ localName,
73
+ file: filePath,
74
+ });
75
+ }
76
+ }
77
+ },
78
+ });
79
+
80
+ return matches;
81
+ }
82
+
83
+ // Process all files and build result
84
+ const allFiles = getAllFiles(dir);
85
+ const componentMap: Record<string, Set<string>> = {};
86
+
87
+ for (const file of allFiles) {
88
+ const imports = extractImportsFromFile(file, lib);
89
+ for (const { importedName, file: filePath } of imports) {
90
+ if (!componentMap[importedName]) {
91
+ componentMap[importedName] = new Set();
92
+ }
93
+ componentMap[importedName].add(filePath);
94
+ }
95
+ }
96
+
97
+ // Final output
98
+ const output: Record<string, string[]> = {};
99
+ for (const [component, files] of Object.entries(componentMap)) {
100
+ output[component] = [...files];
101
+ }
102
+
103
+ 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
@@ -0,0 +1,12 @@
1
+ // tsup.config.ts
2
+ import { defineConfig } from "tsup";
3
+
4
+ export default defineConfig({
5
+ entry: ["src/index.ts"],
6
+ format: ["esm"],
7
+ outDir: "dist",
8
+ banner: {
9
+ js: "#!/usr/bin/env node",
10
+ },
11
+ target: "node18",
12
+ });