@john.klaumann/react-analyzer
Version:
Analyzes React components and automatically creates context providers to eliminate prop drilling
1 lines ⢠4.33 kB
JavaScript
import{createRequire as e}from"node:module";const n=e(import.meta.url)("child_process"),o=e(import.meta.url)("path"),t=(0,e(import.meta.url)("url").fileURLToPath)("file:///C:/PublicisRepos/john/react-component-analyzer/src/lib/analyzer.js"),s=o.dirname(t),a=process.argv.slice(2);let r=!1,l=!1,c=!1,i=!1,d=!1,u=!1,p="dot",g=null,m=null;for(let e=0;e<a.length;e++)"--html"===a[e]||"-h"===a[e]?r=!0:"--cruiser"===a[e]||"-c"===a[e]?(l=!0,e+1<a.length&&!a[e+1].startsWith("--")&&(p=a[e+1],e++)):"--state"===a[e]||"-s"===a[e]?c=!0:"--advanced-state"===a[e]||"-as"===a[e]?i=!0:"--create-context"===a[e]||"-cc"===a[e]?d=!0:"--full"===a[e]||"-f"===a[e]?u=!0:"--output"===a[e]||"-o"===a[e]?e+1<a.length&&(g=a[e+1],e++):m||(m=a[e]);if(m||(console.error("\nā Please specify a component name to analyze."),console.log("Usage: node analyzer.js ComponentName [options]"),console.log("Options:"),console.log(" --html, -h Generate HTML dependency report"),console.log(" --cruiser, -c [FORMAT] Generate dependency-cruiser report (FORMAT: dot/html/json/text/csv)"),console.log(" --state, -s Analyze state management (basic)"),console.log(" --advanced-state, -as Run advanced state analysis with metrics and refactoring suggestions"),console.log(" --create-context, -cc Create Context and Provider based on component analysis"),console.log(" --full, -f Run all analyses (HTML, state, cruiser, advanced state, and context)"),console.log(" --output, -o PATH Specify output path for reports"),process.exit(1)),d){console.log(`\nš Creating Context for component: ${m}...`);try{const e=g?o.dirname(g):"./generated-context";(0,n.execSync)(`node ${o.join(s,"context-generator.js")} ${m} --output ${e}`,{stdio:"inherit"}),console.log("\nā
Context creation completed!"),process.exit(0)}catch(e){console.error(`\nā Error creating context: ${e.message}`),process.exit(1)}}if(u){console.log(`\nš Running FULL analysis for ${m}...\n`);try{const e=g?`${o.dirname(g)}/${o.basename(g,o.extname(g))}-dependencies.html`:`${m.toLowerCase()}-dependencies.html`,t=g?`${o.dirname(g)}/${o.basename(g,o.extname(g))}-state.html`:`${m.toLowerCase()}-state-analysis.html`,a=g?`${o.dirname(g)}/${o.basename(g,o.extname(g))}-cruiser.${p}`:`${m.toLowerCase()}-cruiser.${p}`,r=g?o.dirname(g):"./generated-context";console.log("\nš Running dependency analysis with HTML output..."),(0,n.execSync)(`node ${o.join(s,"dependency-analyzer.js")} ${m} --html --output ${e}`,{stdio:"inherit"}),console.log("\nš Running advanced state management analysis..."),(0,n.execSync)(`node ${o.join(s,"state-analyzer-cli.js")} ${m} --full --output ${t}`,{stdio:"inherit"}),console.log("\nš Running dependency-cruiser analysis..."),(0,n.execSync)(`node ${o.join(s,"cruiser-reporter.js")} ${m} --format ${p} --output ${a}`,{stdio:"inherit"}),console.log("\nš Creating Context and Provider..."),(0,n.execSync)(`node ${o.join(s,"context-generator.js")} ${m} --output ${r}`,{stdio:"inherit"}),console.log("\nā
FULL analysis completed successfully!"),console.log("\nš Reports generated:"),console.log(` - HTML Dependency Report: ${e}`),console.log(" - Advanced State Analysis Reports: see output directory"),console.log(` - Dependency Cruiser Report: ${a}`),console.log(` - Context and Provider: ${r}`),process.exit(0)}catch(e){console.error(`\nā Error running full analysis: ${e.message}`),process.exit(1)}}let y="dependency-analyzer.js",h=[m];g&&h.push("--output",g),i?(y="state-analyzer-cli.js",h.push("--full")):c?y="state-analyzer-cli.js":r?(y="dependency-analyzer.js",h.push("--html")):l?(y="cruiser-reporter.js",h.push("--format",p)):d&&(y="context-generator.js",g&&(h=[m,"--output",g]));const $=o.join(s,y);i?console.log("š Running advanced state management analyzer..."):c?console.log("š Running state management analyzer..."):r?console.log("š Running component analyzer with HTML output..."):l?console.log(`š Running component analyzer with dependency-cruiser (${p} format)...`):d?console.log("š Creating Context and Provider for component..."):console.log("š Running component analyzer with console output...");const x=(0,n.spawn)("node",[$,...h],{stdio:"inherit",shell:!0});x.on("close",(e=>{process.exit(e)})),x.on("error",(e=>{console.error(`Error running analyzer: ${e}`),process.exit(1)}));