UNPKG

@john.klaumann/react-analyzer

Version:

Analyzes React components and automatically creates context providers to eliminate prop drilling

2 lines • 8.83 kB
#!/usr/bin/env node import{createRequire as e}from"node:module";const o=e(import.meta.url)("child_process"),n=e(import.meta.url)("path"),t=e(import.meta.url)("url"),s=e(import.meta.url)("fs"),l=(0,t.fileURLToPath)("file:///C:/PublicisRepos/john/react-component-analyzer/src/bin/react-analyzer.js"),a=n.dirname(l),r=n.join(a,"../lib"),c=process.argv.slice(2);(0===c.length||c.includes("--help")||c.includes("-h"))&&(console.log("\nšŸš€ React Component Analyzer - Enhanced CLI Tool"),console.log("\nUsage: react-analyzer <ComponentName|FolderPath> [options]"),console.log("\nšŸ“‹ Target Types:"),console.log(" ComponentName Analyze a specific component"),console.log(" FolderPath Analyze all components in a folder"),console.log(""),console.log("šŸ“Š Analysis 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(" --folder-state, -fs Run folder-wide state analysis (auto-detected for folder paths)"),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(""),console.log("šŸŽ›ļø Configuration Options:"),console.log(" --type <type> Force analysis type: component, folder, auto (default: auto)"),console.log(" --output, -o PATH Specify output path for reports"),console.log(" --output-dir DIR Specify output directory for all reports"),console.log(" --console-only Output to console only, no HTML reports"),console.log(" --no-recursive Don't analyze folders recursively"),console.log(" --include-tests Include test files in folder analysis"),console.log(" --metrics Calculate advanced state metrics"),console.log(" --refactor Generate refactoring suggestions"),console.log(" --runtime-capture Generate runtime state capture instrumentation"),console.log(" --help Show this help message"),console.log(""),console.log("šŸ“ Folder Analysis Examples:"),console.log(" react-analyzer ./src/components --folder-state"),console.log(" react-analyzer ./src/features/dashboard --full"),console.log(" react-analyzer ./src/pages --advanced-state --metrics"),console.log(" react-analyzer ./src/components --console-only --no-recursive"),console.log(""),console.log("šŸ” Component Analysis Examples:"),console.log(" react-analyzer MyComponent --create-context"),console.log(" react-analyzer Header --html --output ./reports"),console.log(" react-analyzer Dashboard --full"),console.log(" react-analyzer UserProfile --advanced-state --refactor"),console.log(""),console.log("šŸŽÆ Quick Commands:"),console.log(" react-analyzer ./src/components # Auto-detect folder analysis"),console.log(" react-analyzer MyComponent # Auto-detect component analysis"),console.log(" react-analyzer ./src --full --output-dir ./reports # Full analysis with custom output"),process.exit(0));let i=!1,u=!1,d=!1,p=!1,y=!1,g=!1,m=!1,f="dot",h=null,z=null,x=null,$="auto",v=!1,j=!0,R=!1,C=!1,A=!1,T=!1;for(let e=0;e<c.length;e++){const o=c[e];"--html"===o||"-h"===o?i=!0:"--cruiser"===o||"-c"===o?(u=!0,e+1<c.length&&!c[e+1].startsWith("--")&&(f=c[e+1],e++)):"--state"===o||"-s"===o?d=!0:"--advanced-state"===o||"-as"===o?p=!0:"--folder-state"===o||"-fs"===o?y=!0:"--create-context"===o||"-cc"===o?g=!0:"--full"===o||"-f"===o?m=!0:"--output"===o||"-o"===o?e+1<c.length&&(h=c[e+1],e++):"--output-dir"===o?e+1<c.length&&(z=c[e+1],e++):"--type"===o?e+1<c.length&&($=c[e+1],e++):"--console-only"===o?v=!0:"--no-recursive"===o?j=!1:"--include-tests"===o?R=!0:"--metrics"===o?C=!0:"--refactor"===o?A=!0:"--runtime-capture"===o?T=!0:x||o.startsWith("-")||(x=o)}x||(console.error("\nāŒ Please specify a component name or folder path to analyze."),console.log("Usage: react-analyzer <ComponentName|FolderPath> [options]"),console.log('Run "react-analyzer --help" for more information.'),process.exit(1));const L=((e,o="auto")=>{if("auto"!==o)return o;try{if(s.existsSync(e)){return s.statSync(e).isDirectory()?"folder":"component"}}catch(e){}return e.includes("/")||e.includes("\\")||e.startsWith(".")?"folder":"component"})(x,$),F="folder"===L;if(!F||p||d||i||u||g||(y=!0),console.log(`\nšŸš€ React Analyzer - ${F?"Folder":"Component"} Analysis`),console.log(`šŸ“‹ Target: ${x}`),console.log(`šŸ” Type: ${L} analysis\n`),g){F&&(console.error("\nāŒ Context creation is only available for individual components, not folders."),console.log("šŸ’” Try: react-analyzer ComponentName --create-context"),process.exit(1)),console.log(`šŸ”„ Creating Context for component: ${x}...`);try{const e=h?n.dirname(h):z||"./generated-context";(0,o.execSync)(`node ${n.join(r,"context-generator.js")} ${x} --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(m){console.log(`šŸ” Running FULL analysis for ${x}...\n`);try{const e=z||"./analysis-output",t=F?n.basename(n.resolve(x)):x.toLowerCase(),l=h||n.join(e,`${t}-dependencies.html`),a=h||n.join(e,`${t}-state-analysis.html`),c=h||n.join(e,`${t}-cruiser.${f}`),i=z||n.join(e,"generated-context");if(s.existsSync(e)||(s.mkdirSync(e,{recursive:!0}),console.log(`šŸ“ Created output directory: ${e}`)),F){console.log("šŸ“Š Running advanced folder state analysis...");const t=[x,"--full","--output-dir",e];j||t.push("--no-recursive"),R&&t.push("--include-tests"),v&&t.push("--console-only"),(0,o.execSync)(`node ${n.join(r,"state-analyzer-cli.js")} ${t.join(" ")}`,{stdio:"inherit"}),console.log("\nāœ… FULL folder analysis completed!"),console.log(`šŸ“ Reports generated in: ${e}`)}else console.log("šŸ“Š Running dependency analysis with HTML output..."),(0,o.execSync)(`node ${n.join(r,"dependency-analyzer.js")} ${x} --html --output ${l}`,{stdio:"inherit"}),console.log("šŸ“Š Running advanced state management analysis..."),(0,o.execSync)(`node ${n.join(r,"state-analyzer-cli.js")} ${x} --full --output ${a}`,{stdio:"inherit"}),console.log("šŸ“Š Running dependency-cruiser analysis..."),(0,o.execSync)(`node ${n.join(r,"cruiser-reporter.js")} ${x} --format ${f} --output ${c}`,{stdio:"inherit"}),console.log("šŸ“Š Creating Context and Provider..."),(0,o.execSync)(`node ${n.join(r,"context-generator.js")} ${x} --output ${i}`,{stdio:"inherit"}),console.log("\nāœ… FULL component analysis completed!"),console.log("šŸ“ Reports generated:"),console.log(` - HTML Dependency Report: ${l}`),console.log(` - Advanced State Analysis: ${a}`),console.log(` - Dependency Cruiser Report: ${c}`),console.log(` - Context and Provider: ${i}`);process.exit(0)}catch(e){console.error(`\nāŒ Error running full analysis: ${e.message}`),process.exit(1)}}let S="dependency-analyzer.js",b=[x];h&&b.push("--output",h),z&&b.push("--output-dir",z),v&&b.push("--console-only"),!j&&F&&b.push("--no-recursive"),R&&F&&b.push("--include-tests"),C&&b.push("--metrics"),A&&b.push("--refactor"),T&&b.push("--runtime-capture"),p||y?(S="state-analyzer-cli.js",p&&b.push("--full"),F&&b.push("--type","folder")):d?(S="state-analyzer-cli.js",F&&b.push("--type","folder")):i?(F&&(console.error("\nāŒ HTML dependency analysis is only available for individual components."),console.log("šŸ’” For folder analysis, try: react-analyzer ./src/components --folder-state"),process.exit(1)),S="dependency-analyzer.js",b.push("--html")):u?(F&&(console.error("\nāŒ Dependency cruiser analysis is only available for individual components."),console.log("šŸ’” For folder analysis, try: react-analyzer ./src/components --folder-state"),process.exit(1)),S="cruiser-reporter.js",b.push("--format",f)):F?(S="state-analyzer-cli.js",b.push("--type","folder")):S="dependency-analyzer.js";const M=n.join(r,S);p?console.log("šŸ” Running advanced state management analyzer..."):y||F&&(d||!i&&!u)?console.log("šŸ” Running folder-wide state management analysis..."):d?console.log("šŸ” Running state management analyzer..."):i?console.log("šŸ” Running component analyzer with HTML output..."):u?console.log(`šŸ” Running component analyzer with dependency-cruiser (${f} format)...`):console.log(`šŸ” Running ${F?"folder":"component"} analyzer...`);const P=(0,o.spawn)("node",[M,...b],{stdio:"inherit",shell:!0});P.on("close",(e=>{0===e&&console.log("\nāœ… Analysis completed successfully!"),process.exit(e)})),P.on("error",(e=>{console.error(`āŒ Error running analyzer: ${e}`),process.exit(1)}));