migrate-barrel-imports 1.1.0 → 1.3.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/dist/index.js +3 -3
- package/package.json +86 -86
package/dist/index.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
|
-
import{Command as e}from"commander";import{readFileSync as o,writeFileSync as t}from"node:fs";import r,{join as s}from"node:path";import i from"@babel/generator";import{parse as n}from"@babel/parser";import l from"@babel/traverse";import{isVariableDeclaration as a,isIdentifier as c,isExportSpecifier as p,isFunctionDeclaration as d,isImportSpecifier as u,importDeclaration as g,importSpecifier as f,stringLiteral as m}from"@babel/types";import h from"fast-glob";import x from"micromatch";let F=i.default||i,$=l.default||l;async function S(e){return JSON.parse(o(s(e,"package.json"),"utf-8"))}async function y({packagePath:e,ignoreSourceFiles:t=[],stats:r}){let i=[];console.log(`Scanning for TypeScript files in: ${e}`);let l=await h("**/*.{ts,tsx}",{cwd:e,ignore:["**/node_modules/**","**/dist/**","**/build/**"]});for(let u of(console.log(`Found ${l.length}
|
2
|
+
import{Command as e}from"commander";import{readFileSync as o,writeFileSync as t}from"node:fs";import r,{join as s}from"node:path";import i from"@babel/generator";import{parse as n}from"@babel/parser";import l from"@babel/traverse";import{isVariableDeclaration as a,isIdentifier as c,isExportSpecifier as p,isFunctionDeclaration as d,isImportSpecifier as u,importDeclaration as g,importSpecifier as f,stringLiteral as m}from"@babel/types";import h from"fast-glob";import x from"micromatch";let F=i.default||i,$=l.default||l;async function S(e){return JSON.parse(o(s(e,"package.json"),"utf-8"))}async function y({packagePath:e,ignoreSourceFiles:t=[],stats:r}){let i=[];console.log(`Scanning for TypeScript and JavaScript files in: ${e}`);let l=await h("**/*.{ts,tsx,js,jsx}",{cwd:e,ignore:["**/node_modules/**","**/dist/**","**/build/**"]});for(let u of(console.log(`Found ${l.length} files`),l)){let l=t.some(e=>x.isMatch(u,e));l&&(console.log(`File matches ignore pattern but will be preserved: ${u}`),r&&r.sourceFilesSkipped++);let g=s(e,u);console.log(`
|
3
3
|
Processing file: ${u}`);let f=o(g,"utf-8");try{let e=n(f,{sourceType:"module",plugins:["typescript","jsx","decorators-legacy","classProperties","classPrivateProperties","classPrivateMethods","exportDefaultFrom","exportNamespaceFrom","functionBind","functionSent","dynamicImport","nullishCoalescingOperator","optionalChaining","objectRestSpread","asyncGenerators","doExpressions","importMeta","logicalAssignment","moduleBlocks","moduleStringNames","numericSeparator","partialApplication","privateIn","throwExpressions","topLevelAwait"]});$(e,{ExportNamedDeclaration(e){if(console.log(`Found named export in ${u}`),e.node.source){console.log(`Skipping re-export from ${e.node.source.value}`);return}if(e.node.declaration){if(a(e.node.declaration)){let o=e.node.declaration.declarations.map(e=>c(e.id)?e.id.name:null).filter(e=>null!==e);o.length>0&&(console.log(`Named exports found: ${o.join(", ")}`),i.push({source:u,exports:o,isIgnored:l}))}return}let o=e.node.specifiers.map(e=>{if(p(e)){let o=e.exported;return c(o)?o.name:o.value}return null}).filter(e=>null!==e);o.length>0&&(console.log(`Named exports found: ${o.join(", ")}`),i.push({source:u,exports:o,isIgnored:l}))},ExportDefaultDeclaration(e){console.log(`Found default export in ${u}`);let o=e.node.declaration;c(o)?(console.log(`Default export name: ${o.name}`),i.push({source:u,exports:[o.name],isIgnored:l})):d(o)&&o.id?(console.log(`Default export name: ${o.id.name}`),i.push({source:u,exports:[o.id.name],isIgnored:l})):console.log("Default export is not an identifier or named function")}})}catch(e){console.error(`Error parsing ${u}:`,e)}}return console.log(`
|
4
|
-
Total exports found: ${i.length}`),i}async function w({packageName:e,monorepoRoot:t}){try{let r=new Set,s=await h(["**/*.{ts,tsx}"],{cwd:t,absolute:!0,ignore:["**/node_modules/**","**/dist/**","**/build/**"],followSymbolicLinks:!1});for(let t of(console.log(`Found ${s.length}
|
4
|
+
Total exports found: ${i.length}`),i}async function w({packageName:e,monorepoRoot:t}){try{let r=new Set,s=await h(["**/*.{ts,tsx,js,jsx}"],{cwd:t,absolute:!0,ignore:["**/node_modules/**","**/dist/**","**/build/**"],followSymbolicLinks:!1});for(let t of(console.log(`Found ${s.length} files to scan`),s))try{let s=o(t,"utf-8"),i=n(s,{sourceType:"module",plugins:["typescript","jsx"]});$(i,{ImportDeclaration(o){let s=o.node.source.value;(s===e||s.startsWith(`${e}/`))&&r.add(t)}})}catch(e){console.error(`Error processing file ${t}:`,e)}let i=Array.from(r);if(i.length>0)for(let o of(console.log(`Found total of ${i.length} files with imports from ${e}`),console.log("Files found:"),i))console.log(` ${o}`);else console.log(`No files found importing from ${e}`);return i}catch(e){return console.error("Error finding imports:",e),[]}}async function E({filePath:e,packageName:r,exports:s,includeExtension:i=!0}){console.log(`
|
5
5
|
Processing file: ${e}`);let l=o(e,"utf-8");try{let o=n(l,{sourceType:"module",plugins:["typescript","jsx","decorators-legacy","classProperties","classPrivateProperties","classPrivateMethods","exportDefaultFrom","exportNamespaceFrom","functionBind","functionSent","dynamicImport","nullishCoalescingOperator","optionalChaining","objectRestSpread","asyncGenerators","doExpressions","importMeta","logicalAssignment","moduleBlocks","moduleStringNames","numericSeparator","partialApplication","privateIn","throwExpressions","topLevelAwait"]}),a=!1,p=0;if($(o,{ImportDeclaration(e){if(e.node.source.value===r){p++,console.log(`Found import from ${r}`);let o=e.node.specifiers,t=[];for(let e of o)if(u(e)){let o=e.imported,n=c(o)?o.name:o.value,l=s.find(e=>e.exports.includes(n));if(l){if(console.log(` Found export ${n} in ${l.source}`),l.isIgnored){console.log(` Keeping original import for ignored file: ${l.source}`);continue}let o=i?`${r}/${l.source}`:`${r}/${l.source.replace(/\.(js|jsx|ts|tsx|mjs|cjs)$/,"")}`;t.push(g([f(e.local,e.imported)],m(o))),a=!0}else console.log(` Warning: Could not find export ${n}`)}if(t.length>0){let i=o.filter(e=>{if(!u(e))return!0;let o=c(e.imported)?e.imported.name:e.imported.value,t=s.find(e=>e.exports.includes(o));return!t||t.isIgnored});i.length>0&&t.unshift(g(i,m(r))),e.replaceWithMultiple(t),console.log(" Replaced import with direct imports from source files")}}}}),a){console.log(`Writing changes to ${e}`);let r=F(o,{retainLines:!1,retainFunctionParens:!0},l);t(e,r.code)}else p>0?console.log(`No changes needed for ${p} imports`):console.log("No imports found to update")}catch(o){console.error(`Error processing ${e}:`,o)}}async function P(e){let{sourcePath:t,targetPath:s,ignoreSourceFiles:i,ignoreTargetFiles:n}=e,l={totalFiles:0,filesProcessed:0,filesSkipped:0,importsUpdated:0,filesWithNoUpdates:0,errors:0,totalExports:0,sourceFilesScanned:0,sourceFilesWithExports:0,sourceFilesSkipped:0,targetFilesFound:[]},a=await S(t),c=await y({packagePath:t,ignoreSourceFiles:i,stats:l});l.totalExports=c.reduce((e,o)=>e+o.exports.length,0),l.sourceFilesWithExports=new Set(c.map(e=>e.source)).size,l.sourceFilesScanned=(await h("**/*.{ts,tsx}",{cwd:t,ignore:["**/node_modules/**","**/dist/**","**/build/**"]})).length;let p=await w({packageName:a.name,monorepoRoot:s});for(let t of(l.totalFiles=p.length,l.targetFilesFound=p,p)){let i=r.relative(s,t);if(n.some(e=>x.isMatch(i,e))){console.log(`Skipping ignored file: ${t} (matches pattern in ${n.join(", ")})`),l.filesSkipped++;continue}try{let r=o(t,"utf-8");await E({filePath:t,packageName:a.name,exports:c,includeExtension:e.includeExtension});let s=o(t,"utf-8");r!==s?l.importsUpdated++:l.filesWithNoUpdates++,l.filesProcessed++}catch(e){l.errors++,console.error(`Error processing ${t}:`,e)}}console.log("\nMigration Summary"),console.log(`Source files found: ${l.sourceFilesScanned}`),console.log(`Source files with exports: ${l.sourceFilesWithExports}`),console.log(`Source files skipped: ${l.sourceFilesSkipped}`),console.log(`Exports found: ${l.totalExports}`),console.log(`Target files found: ${l.totalFiles}`),console.log(`Target files processed: ${l.filesProcessed}`),console.log(`Target files with imports updated: ${l.importsUpdated}`),console.log(`Target files with no changes needed: ${l.filesWithNoUpdates}`),console.log(`Target files skipped: ${l.filesSkipped}`),l.errors>0&&console.log(`
|
6
|
-
Warning: ${l.errors} errors encountered during processing`)}let b={targetPath:".",ignoreSourceFiles:[],ignoreTargetFiles:[],includeExtension:!1};async function
|
6
|
+
Warning: ${l.errors} errors encountered during processing`)}let b={targetPath:".",ignoreSourceFiles:[],ignoreTargetFiles:[],includeExtension:!1};async function j(){let o=new e;o.name("migrate-barrel-imports").description("CLI tool to migrate barrel files imports to direct imports").argument("<source-path>","Path to the package containing barrel files").argument("[target-path]","Path to the directory where imports should be migrated (default: current directory)").option("--ignore-source-files <patterns>","Comma-separated list of file patterns to ignore in source directory").option("--ignore-target-files <patterns>","Comma-separated list of file patterns to ignore in target directory").option("--no-extension","Exclude js|jsx|ts|tsx|mjs|cjs file extensions from import statements").allowUnknownOption(!1).parse(process.argv);let t=o.args;t[0]||(console.error("Error: source-path is required"),process.exit(1));let r=t[0],s=t[1]||b.targetPath,i=o.opts();await P({sourcePath:r,targetPath:s,ignoreSourceFiles:i.ignoreSourceFiles?i.ignoreSourceFiles.split(","):b.ignoreSourceFiles,ignoreTargetFiles:i.ignoreTargetFiles?i.ignoreTargetFiles.split(","):b.ignoreTargetFiles,includeExtension:!1!==i.extension||b.includeExtension})}(async()=>{await j()})();
|
package/package.json
CHANGED
@@ -1,88 +1,88 @@
|
|
1
1
|
{
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
2
|
+
"name": "migrate-barrel-imports",
|
3
|
+
"version": "1.3.0",
|
4
|
+
"description": "A CLI tool to migrate barrel imports in JavaScript/TypeScript monorepos",
|
5
|
+
"files": [
|
6
|
+
"dist/"
|
7
|
+
],
|
8
|
+
"main": "./dist/index.js",
|
9
|
+
"targets": {
|
10
|
+
"main": {
|
11
|
+
"optimize": true
|
12
|
+
}
|
13
|
+
},
|
14
|
+
"keywords": [
|
15
|
+
"typescript",
|
16
|
+
"javascript",
|
17
|
+
"monorepo",
|
18
|
+
"cli",
|
19
|
+
"tool",
|
20
|
+
"migrate",
|
21
|
+
"barrel",
|
22
|
+
"import",
|
23
|
+
"imports"
|
24
|
+
],
|
25
|
+
"repository": {
|
26
|
+
"type": "git",
|
27
|
+
"url": "git+https://github.com/brandhaug/migrate-barrel-imports.git"
|
28
|
+
},
|
29
|
+
"bugs": {
|
30
|
+
"url": "https://github.com/brandhaug/migrate-barrel-imports/issues"
|
31
|
+
},
|
32
|
+
"homepage": "https://github.com/brandhaug/migrate-barrel-imports#readme",
|
33
|
+
"type": "module",
|
34
|
+
"bin": {
|
35
|
+
"migrate-barrel-imports": "dist/index.js"
|
36
|
+
},
|
37
|
+
"scripts": {
|
38
|
+
"build": "parcel build src/index.ts --no-source-maps",
|
39
|
+
"start": "node dist/index.js",
|
40
|
+
"lint": "biome lint",
|
41
|
+
"check-write": "biome check --write",
|
42
|
+
"typecheck": "tsc",
|
43
|
+
"prepare": "husky",
|
44
|
+
"test": "vitest",
|
45
|
+
"validate": "npm run lint && npm run typecheck && npm run test"
|
46
|
+
},
|
47
|
+
"dependencies": {
|
48
|
+
"@babel/generator": "7.26.10",
|
49
|
+
"@babel/parser": "7.26.10",
|
50
|
+
"@babel/traverse": "7.26.10",
|
51
|
+
"@babel/types": "7.26.10",
|
52
|
+
"commander": "13.1.0",
|
53
|
+
"fast-glob": "3.3.3",
|
54
|
+
"micromatch": "4.0.8",
|
55
|
+
"ts-morph": "25.0.1"
|
56
|
+
},
|
57
|
+
"devDependencies": {
|
58
|
+
"@biomejs/biome": "1.9.4",
|
59
|
+
"@commitlint/cli": "19.8.0",
|
60
|
+
"@commitlint/config-conventional": "19.8.0",
|
61
|
+
"@semantic-release/changelog": "6.0.3",
|
62
|
+
"@types/babel__generator": "7.6.8",
|
63
|
+
"@types/babel__traverse": "7.20.6",
|
64
|
+
"@types/micromatch": "4.0.9",
|
65
|
+
"execa": "9.5.2",
|
66
|
+
"husky": "9.1.7",
|
67
|
+
"lint-staged": "15.5.0",
|
68
|
+
"parcel": "2.14.1",
|
69
|
+
"semantic-release": "24.2.3",
|
70
|
+
"tsc-files": "1.1.4",
|
71
|
+
"tsx": "4.19.3",
|
72
|
+
"typescript": "5.8.2",
|
73
|
+
"vitest": "3.0.9"
|
74
|
+
},
|
75
|
+
"author": "Martin Brandhaug",
|
76
|
+
"license": "MIT",
|
77
|
+
"lint-staged": {
|
78
|
+
"*.{ts,json}": [
|
79
|
+
"biome check --write"
|
80
|
+
],
|
81
|
+
"*.ts": [
|
82
|
+
"tsc-files"
|
83
|
+
]
|
84
|
+
},
|
85
|
+
"engines": {
|
86
|
+
"node": ">=20"
|
87
|
+
}
|
88
88
|
}
|