UNPKG

@autodev/backend-generator

Version:

Backend project generator with support for parsing and generating project configurations

5 lines (4 loc) 7.92 kB
#!/usr/bin/env node "use strict";const j=require("commander"),o=require("chalk"),n=require("./project-generator-BWy2ZtpX.js"),m=require("fs-extra"),y=require("path");function f(t){const r=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(t){for(const e in t)if(e!=="default"){const a=Object.getOwnPropertyDescriptor(t,e);Object.defineProperty(r,e,a.get?a:{enumerable:!0,get:()=>t[e]})}}return r.default=t,Object.freeze(r)}const d=f(m),p=f(y),i=new j.Command;i.name("backend-generator").description("Backend project generator with support for parsing and generating project configurations").version("0.1.0");i.command("add").description("Download and generate project from a Golden Path configuration URL").argument("<url>","URL to the Golden Path configuration API endpoint").option("-o, --output <dir>","Output directory for the generated project").option("--overwrite","Overwrite existing files").option("--dry-run","Show what would be generated without creating files").option("--save-config","Save the downloaded configuration to a local file").action(async(t,r)=>{try{console.log(o.blue("🌐 Downloading Golden Path configuration...")),console.log(o.gray(` URL: ${t}`));const e=await fetch(t);if(!e.ok)throw new Error(`Failed to fetch configuration: ${e.status} ${e.statusText}`);const a=await e.json();let c;a.config?(c=a.config,console.log(o.green("✅ Configuration downloaded successfully!")),console.log(o.cyan(` Name: ${a.name||"Unknown"}`)),console.log(o.cyan(` Description: ${a.description||"No description"}`))):(c=a,console.log(o.green("✅ Configuration downloaded successfully!")));const g=n.ProjectParser.validate(c);if(!g.success)throw new n.ProjectParseError("Downloaded configuration is invalid",g.error);const s=g.data;if(r.saveConfig){const u=`${s.projectConfig.name}-config.json`;await n.ProjectGenerator.saveToFile(s,u),console.log(o.green(`💾 Configuration saved to: ${u}`))}const l=r.output||`./${s.projectConfig.name}`;console.log(o.blue(`🏗️ Generating project structure to: ${l}`)),await new n.ProjectGenerator(s,{outputDir:l,overwrite:r.overwrite,dryRun:r.dryRun}).generate(),r.dryRun?console.log(o.yellow("🔍 Dry run completed. No files were created.")):(console.log(o.green("🎉 Project generated successfully!")),console.log(o.cyan(`📁 Project location: ${p.resolve(l)}`)),console.log(o.cyan("💡 Next steps:")),console.log(o.gray(` cd ${l}`)),console.log(o.gray(" # Follow the README.md for further instructions")))}catch(e){e instanceof TypeError&&e.message.includes("fetch")?console.error(o.red("❌ Network error:"),"Unable to connect to the URL. Please check your internet connection and URL."):e instanceof n.ProjectParseError||e instanceof n.ProjectGenerateError?(console.error(o.red("❌ Generation failed:"),e.message),e.details&&console.error(o.gray("Details:"),JSON.stringify(e.details,null,2))):console.error(o.red("❌ Unexpected error:"),e),process.exit(1)}});i.command("parse").description("Parse and validate a project configuration file").argument("<file>","Path to the project configuration JSON file").option("-v, --verbose","Show detailed validation information").action(async(t,r)=>{try{console.log(o.blue("🔍 Parsing project configuration..."));const e=await n.ProjectParser.parseFromFile(t);console.log(o.green("✅ Project configuration is valid!")),r.verbose&&(console.log(` `+o.cyan("Project Details:")),console.log(` Name: ${e.projectConfig.name}`),console.log(` Type: ${e.projectConfig.type}`),console.log(` Language: ${e.projectConfig.language}`),console.log(` Framework: ${e.projectConfig.framework}`),console.log(` Features: ${e.features.length} features`),console.log(` Directories: ${e.structure.directories.length} directories`),console.log(` Files: ${e.structure.files.length} files`),console.log(` Dependencies: ${Object.keys(e.dependencies).length} dependencies`))}catch(e){e instanceof n.ProjectParseError?(console.error(o.red("❌ Parsing failed:"),e.message),e.details&&r.verbose&&console.error(o.gray("Details:"),JSON.stringify(e.details,null,2))):console.error(o.red("❌ Unexpected error:"),e),process.exit(1)}});i.command("generate").description("Generate project structure from configuration file").argument("<file>","Path to the project configuration JSON file").option("-o, --output <dir>","Output directory","./generated-project").option("--overwrite","Overwrite existing files").option("--dry-run","Show what would be generated without creating files").action(async(t,r)=>{try{console.log(o.blue("🔍 Parsing project configuration..."));const e=await n.ProjectParser.parseFromFile(t);console.log(o.green("✅ Configuration parsed successfully!")),console.log(o.blue(`🏗️ Generating project structure to: ${r.output}`)),await new n.ProjectGenerator(e,{outputDir:r.output,overwrite:r.overwrite,dryRun:r.dryRun}).generate(),r.dryRun?console.log(o.yellow("🔍 Dry run completed. No files were created.")):(console.log(o.green("🎉 Project generated successfully!")),console.log(o.cyan(`📁 Project location: ${p.resolve(r.output)}`)))}catch(e){e instanceof n.ProjectParseError||e instanceof n.ProjectGenerateError?console.error(o.red("❌ Generation failed:"),e.message):console.error(o.red("❌ Unexpected error:"),e),process.exit(1)}});i.command("validate").description("Validate a project configuration without generating files").argument("<file>","Path to the project configuration JSON file").action(async t=>{try{const r=await d.readFile(t,"utf-8"),e=JSON.parse(r),a=n.ProjectParser.validate(e);if(a.success){console.log(o.green("✅ Configuration is valid!"));const c=a.data;console.log(o.cyan(` Summary:`)),console.log(` Project: ${c.projectConfig.name}`),console.log(` Type: ${c.projectConfig.type}`),console.log(` Language: ${c.projectConfig.language}`),console.log(` Framework: ${c.projectConfig.framework}`)}else console.error(o.red("❌ Configuration is invalid:"),a.error),process.exit(1)}catch(r){r instanceof SyntaxError?console.error(o.red("❌ Invalid JSON format:"),r.message):console.error(o.red("❌ Error reading file:"),r),process.exit(1)}});i.command("create").description("Create a new project configuration interactively").option("-o, --output <file>","Output file path","./project-config.json").action(async t=>{try{console.log(o.blue("🚀 Creating new project configuration..."));const r={projectConfig:{name:"sample-project",description:"A sample project configuration",type:"microservice",language:"java",framework:"spring3"},features:["authentication-authorization","database-integration","api-documentation"],structure:{directories:["src/main/java","src/main/resources","src/test/java"],files:["pom.xml","README.md"]},dependencies:{"spring-boot-starter":"3.0.0","spring-boot-starter-web":"3.0.0"},configurations:{"application.properties":["server.port=8080","spring.application.name=sample-project"]}};await n.ProjectGenerator.saveToFile(r,t.output),console.log(o.green(`✅ Sample configuration created: ${t.output}`)),console.log(o.cyan('💡 Edit the file and use "generate" command to create your project.'))}catch(r){console.error(o.red("❌ Failed to create configuration:"),r),process.exit(1)}});i.command("export").description("Export project configuration to different formats").argument("<file>","Path to the project configuration JSON file").option("-f, --format <format>","Output format (json, yaml)","json").option("-o, --output <file>","Output file path").action(async(t,r)=>{try{const e=await n.ProjectParser.parseFromFile(t);if(r.format==="json"){const a=n.ProjectGenerator.generateJson(e,!0),c=r.output||t.replace(/\.json$/,".formatted.json");await d.writeFile(c,a,"utf-8"),console.log(o.green(`✅ Exported to: ${c}`))}else console.error(o.red("❌ Unsupported format:"),r.format),console.log(o.cyan("Supported formats: json")),process.exit(1)}catch(e){console.error(o.red("❌ Export failed:"),e),process.exit(1)}});i.parse();