balda 0.0.34 → 0.0.36

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/lib/cli.js CHANGED
@@ -193,8 +193,10 @@ server.listen(({ url }) => {
193
193
  // The GraphQL endpoint is automatically available at /graphql
194
194
  // You can extend the schema using server.graphql.addTypeDef() and server.graphql.addResolver()
195
195
  `}static getLoggerTemplate(){return `import pino from "pino";
196
+ import { CommandRegistry } from "../command_registry.js";
196
197
 
197
198
  export const logger = pino({ level: "info" });
199
+ CommandRegistry.setLogger(logger);
198
200
  `}};x([P.string({description:"The path to the project, default is the current directory /src",aliases:"p",name:"path",required:false,defaultValue:"./src"})],H,"srcPath"),x([P.boolean({description:"Whether to use typescript, default is true",aliases:"t",name:"typescript",required:false,defaultValue:true})],H,"typescript"),x([P.boolean({description:"Initialize MQTT service connection",aliases:"m",name:"mqtt",required:false,defaultValue:false})],H,"mqtt"),x([P.boolean({description:"Initialize Cron service",aliases:"c",name:"cron",required:false,defaultValue:false})],H,"cron"),x([P.boolean({description:"Initialize GraphQL service",aliases:"g",name:"graphql",required:false,defaultValue:false})],H,"graphql");var ae=class extends q{static commandName="init-mailer";static description="Initialize mailer configuration with required dependencies";static help=["Initialize a mailer configuration file with basic setup","Automatically installs required packages for nodemailer and optional template engines","Supports Handlebars, EJS, Edge.js, Mustache, or custom adapters","Example: npx balda init-mailer -t handlebars -o src/mailer"];static templateEngine;static outputPath;static mailerDependencies={base:["nodemailer","@types/nodemailer"],handlebars:["handlebars","@types/handlebars"],ejs:["ejs","@types/ejs"],edge:["edge.js"],mustache:["mustache","@types/mustache"]};static async handle(){if(this.logger.info("Initializing mailer configuration..."),!["handlebars","ejs","edge","mustache","custom","none"].includes(this.templateEngine)){this.logger.error(`Invalid template engine: ${this.templateEngine}. Must be one of: handlebars, ejs, edge, mustache, custom, none`);return}let[e,t]=await F();if(["npm","yarn","pnpm"].includes(e)){let c=[...this.mailerDependencies.base,...this.templateEngine!=="none"&&this.templateEngine!=="custom"?this.mailerDependencies[this.templateEngine]||[]:[]],a=await V(c);if(a.length>0&&(this.logger.info(`Found ${a.length} missing dependencies for mailer`),!await A(`${e} ${t} ${a.join(" ")}`,e,a,{stdio:"inherit"},false))){this.logger.info("Installation cancelled by user. Mailer initialization aborted.");return}a.length===0&&this.logger.info("All mailer dependencies are already installed");}let r=this.getConfigTemplate(),o="mailer.config.ts",s=w.join(this.outputPath,o);await f.exists(this.outputPath)||await f.mkdir(this.outputPath,{recursive:true}),this.logger.info(`Creating ${o} file at ${this.outputPath}...`),await f.writeFile(s,new TextEncoder().encode(r)),this.logger.info(`Mailer configuration initialized successfully at ${s}`),this.logger.info("Remember to update the configuration with your actual SMTP credentials"),this.templateEngine!=="none"&&this.logger.info(`Template engine '${this.templateEngine}' configured and ready to use`);}static getConfigTemplate(){let e=this.templateEngine!=="none";return `import { createTransport } from "nodemailer";
199
201
  import { Mailer${e?`, ${this.getAdapterImport()}`:""} } from "balda";
200
202
 
@@ -592,7 +594,7 @@ export const storage = new Storage(
592
594
  defaultProvider: "local",
593
595
  },
594
596
  );
595
- `}[e]||""}};x([P({description:"Storage provider type (s3, azure, local)",type:"string",aliases:["t"],name:"type",required:true})],me,"storageType"),x([P({description:"Output directory for storage setup",type:"string",aliases:["o"],name:"output",required:false,defaultValue:"src/storage/"})],me,"outputPath");var Nt=[ie,ve,se,ne,Z,re,oe,k,H,ae,le,Se,D,pe,me,we,ue,te],W=class l{commands;builtInCommands;static commandsPattern="src/commands/**/*.{ts,js}";static loggerPath="src/logger.ts";static logger=M.child({scope:"CommandRegistry"});static setLogger(e){l.logger=e.child({scope:"CommandRegistry"}),q.logger=e.child({scope:"Command"});}static async loadLogger(e){let t=e||l.loggerPath;try{let r=await import(w.join(N.getCwd(),t));r.logger&&l.setLogger(r.logger);}catch{l.logger.debug(`Could not load logger from ${t}, using default logger`);}}constructor(){this.commands=new Map,this.builtInCommands=new Set;}static getInstance(){return new l}static setCommandsPattern(e){this.commandsPattern=e;}getCommand(e){return this.commands.get(e)??null}getCommands(){return Array.from(this.commands.values())}getBuiltInCommands(){return Array.from(this.commands.values()).filter(e=>this.builtInCommands.has(e.commandName))}getUserDefinedCommands(){return Array.from(this.commands.values()).filter(e=>!this.builtInCommands.has(e.commandName))}isBuiltInCommand(e){return this.builtInCommands.has(e)}async loadCommands(e){l.logger.info(`Loading commands from ${e}`);let t=await f.glob(e,{cwd:N.getCwd()});if(t.some(r=>r.endsWith(".ts")))try{let{register:r}=await import('module');r("ts-node/esm",import.meta.url);}catch{l.logger.error("Failed to register ts-node/esm, you need to install it in your project in order to use typescript in the cli\ntry running: `npm install -D ts-node`"),process.exit(1);}for(let r of t){let o=await import(r).then(s=>s.default?s.default:s).catch(s=>(l.logger.error(`Error loading command ${r}: ${s}`),null));o&&this.commands.set(o.commandName,o);}for(let r of Nt)this.commands.set(r.commandName,r),this.builtInCommands.add(r.commandName);}},U=W.getInstance();var _t=l=>{let e=new Map;for(let t of l){let r=t.options?.category||"other";e.has(r)||e.set(r,[]),e.get(r).push(t);}return e},Ft=l=>{let e=Array.from(l.keys()).sort();for(let t of e){let r=l.get(t).filter(a=>a&&a.commandName);if(!r.length)continue;let s={generator:"\x1B[35m",setup:"\x1B[34m",production:"\x1B[32m",utility:"\x1B[36m",other:"\x1B[37m"}[t]||"\x1B[37m";console.log(` ${s}${t.toUpperCase()}:\x1B[0m`);let c=Math.max(...r.map(a=>a.commandName.length));for(let a of r){let d=a.commandName.padEnd(c+2),u=a.description||"No description available",i="";a.options?.deprecated&&(i=" \x1B[33m[deprecated]\x1B[0m"),console.log(` \x1B[36m${d}\x1B[0m ${u}${i}`);}console.log("");}},Ye=async()=>{await U.loadCommands(W.commandsPattern);let[l,...e]=fe.getCliArgs();if(!l||l==="-h"||l==="--help"){let s=U.getBuiltInCommands(),c=U.getUserDefinedCommands();if(console.log(`
597
+ `}[e]||""}};x([P({description:"Storage provider type (s3, azure, local)",type:"string",aliases:["t"],name:"type",required:true})],me,"storageType"),x([P({description:"Output directory for storage setup",type:"string",aliases:["o"],name:"output",required:false,defaultValue:"src/storage/"})],me,"outputPath");var Nt=[ie,ve,se,ne,Z,re,oe,k,H,ae,le,Se,D,pe,me,we,ue,te],W=class l{commands;builtInCommands;static commandsPattern="src/commands/**/*.{ts,js}";static loggerPath="src/logger.ts";static logger=M.child({scope:"CommandRegistry"});static setLogger(e){l.logger=e.child({scope:"CommandRegistry"}),q.logger=e.child({scope:"Command"});}static async loadLogger(e){let t=e||l.loggerPath;try{let r=await import(w.join(N.getCwd(),t));r.logger&&l.setLogger(r.logger);}catch{l.logger.debug(`Could not load logger from ${t}, using default logger`);}}constructor(){this.commands=new Map,this.builtInCommands=new Set;}static getInstance(){return new l}static setCommandsPattern(e){this.commandsPattern=e;}getLogger(){return l.logger}setLogger(e){l.logger=e.child({scope:"CommandRegistry"}),q.logger=e.child({scope:"Command"});}getCommand(e){return this.commands.get(e)??null}getCommands(){return Array.from(this.commands.values())}getBuiltInCommands(){return Array.from(this.commands.values()).filter(e=>this.builtInCommands.has(e.commandName))}getUserDefinedCommands(){return Array.from(this.commands.values()).filter(e=>!this.builtInCommands.has(e.commandName))}isBuiltInCommand(e){return this.builtInCommands.has(e)}async loadCommands(e){l.logger.info(`Loading commands from ${e}`);let t=await f.glob(e,{cwd:N.getCwd()});if(t.some(r=>r.endsWith(".ts")))try{let{register:r}=await import('module');r("ts-node/esm",import.meta.url);}catch{l.logger.error("Failed to register ts-node/esm, you need to install it in your project in order to use typescript in the cli\ntry running: `npm install -D ts-node`"),process.exit(1);}for(let r of t){let o=await import(r).then(s=>s.default?s.default:s).catch(s=>(l.logger.error(`Error loading command ${r}: ${s}`),null));o&&this.commands.set(o.commandName,o);}for(let r of Nt)this.commands.set(r.commandName,r),this.builtInCommands.add(r.commandName);}},U=W.getInstance();var _t=l=>{let e=new Map;for(let t of l){let r=t.options?.category||"other";e.has(r)||e.set(r,[]),e.get(r).push(t);}return e},Ft=l=>{let e=Array.from(l.keys()).sort();for(let t of e){let r=l.get(t).filter(a=>a&&a.commandName);if(!r.length)continue;let s={generator:"\x1B[35m",setup:"\x1B[34m",production:"\x1B[32m",utility:"\x1B[36m",other:"\x1B[37m"}[t]||"\x1B[37m";console.log(` ${s}${t.toUpperCase()}:\x1B[0m`);let c=Math.max(...r.map(a=>a.commandName.length));for(let a of r){let d=a.commandName.padEnd(c+2),u=a.description||"No description available",i="";a.options?.deprecated&&(i=" \x1B[33m[deprecated]\x1B[0m"),console.log(` \x1B[36m${d}\x1B[0m ${u}${i}`);}console.log("");}},Ye=async()=>{await U.loadCommands(W.commandsPattern);let[l,...e]=fe.getCliArgs();if(!l||l==="-h"||l==="--help"){let s=U.getBuiltInCommands(),c=U.getUserDefinedCommands();if(console.log(`
596
598
  \u2728 Available Balda Commands:
597
599
  `),c.length&&(console.log(`\x1B[1;33mUser Commands:\x1B[0m
598
600
  `),Ft(_t(c))),s.length){console.log(`\x1B[1;32mBuilt-in Commands:\x1B[0m