{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["import { join, dirname } from 'node:path';\nimport handlebars from 'handlebars';\nimport fs from 'fs-extra';\n\n// Starts the Plop CLI programmatically\nexport const runCLI = async () => {\n  const { Plop, run } = await import('plop');\n\n  Plop.prepare(\n    {\n      configPath: join(__dirname, 'plopfile.js'),\n    },\n    (env) => {\n      Plop.execute(env, [], (env, argv) => {\n        const options = {\n          ...env,\n          dest: join(process.cwd(), 'src'), // this will make the destination path to be based on the cwd when calling the wrapper\n        };\n        return run(options, argv, true); // Pass the third argument 'true' for passArgsBeforeDashes\n      });\n    }\n  );\n};\n\ntype GenerateOptions = {\n  dir?: string;\n  plopFile?: string;\n};\n\ntype GeneratorAction = {\n  type: 'add' | 'modify';\n  path: string;\n  templateFile?: string;\n  data?: Record<string, any>;\n  transform?: (content: string) => string;\n};\n\nexport const generate = async <T extends Record<string, any>>(\n  generatorName: string,\n  options: T,\n  { dir = process.cwd(), plopFile = 'plopfile.js' }: GenerateOptions = {}\n) => {\n  // Resolve the absolute path to the plopfile (generator definitions)\n  const plopfilePath = join(__dirname, plopFile);\n  // Dynamically require the plopfile module.\n  // Note: This allows loading either CommonJS or transpiled ESM plopfiles.\n  // eslint-disable-next-line @typescript-eslint/no-var-requires\n  const plopModule = require(plopfilePath);\n\n  // Internal objects to store registered generators and helpers.\n  // These will be populated by the plopfile when it is executed.\n  const generators: Record<string, any> = {};\n  const helpers: Record<string, any> = {};\n\n  // Minimal mock Plop API implementation, exposing only the methods needed by our plopfile.\n  // This allows the plopfile to register generators and helpers as it would in a real Plop environment.\n  const plopApi = {\n    setGenerator(name: string, config: any) {\n      generators[name] = config;\n    },\n    setHelper(name: string, fn: any) {\n      helpers[name] = fn;\n    },\n    getDestBasePath() {\n      return join(dir, 'src');\n    },\n    setWelcomeMessage() {}, // no-op\n  };\n\n  // Execute the plopfile, passing in our API.\n  // This will populate the `generators` and `helpers` objects.\n  // Support both CommonJS and ESM default exports.\n  if (typeof plopModule.default === 'function') {\n    plopModule.default(plopApi);\n  } else {\n    plopModule(plopApi);\n  }\n\n  const generator = generators[generatorName];\n  if (!generator) {\n    throw new Error(`Generator \"${generatorName}\" not found`);\n  }\n\n  registerHandlebarsHelpers(helpers);\n  const actions: GeneratorAction[] =\n    typeof generator.actions === 'function' ? generator.actions(options) : generator.actions || [];\n\n  await executeActions(actions, options, dir);\n\n  return { success: true };\n};\n\nconst registerHandlebarsHelpers = (helpers: Record<string, any>) => {\n  Object.entries(helpers).forEach(([name, fn]) => handlebars.registerHelper(name, fn));\n};\n\n// Executes generator actions: add or modify files as specified\nconst executeActions = async (\n  actions: GeneratorAction[],\n  options: Record<string, any>,\n  dir: string\n) => {\n  for (const action of actions) {\n    const outputPath = handlebars.compile(action.path)(options);\n    const fullPath = join(dir, 'src', outputPath);\n\n    if (action.type === 'add' && action.templateFile) {\n      const templatePath = join(__dirname, action.templateFile);\n      const templateContent = await fs.readFile(templatePath, 'utf8');\n      const compiled = handlebars.compile(templateContent);\n      const output = compiled({ ...options, ...action.data });\n\n      await fs.ensureDir(dirname(fullPath));\n      await fs.writeFile(fullPath, output);\n    }\n\n    if (action.type === 'modify') {\n      if (await fs.pathExists(fullPath)) {\n        const content = await fs.readFile(fullPath, 'utf8');\n        const modified = action.transform ? action.transform(content) : content;\n        await fs.writeFile(fullPath, modified);\n      }\n    }\n  }\n};\n"],"names":["runCLI","Plop","run","prepare","configPath","join","__dirname","env","execute","argv","options","dest","process","cwd","generate","generatorName","dir","plopFile","plopfilePath","plopModule","require","generators","helpers","plopApi","setGenerator","name","config","setHelper","fn","getDestBasePath","setWelcomeMessage","default","generator","Error","registerHandlebarsHelpers","actions","executeActions","success","Object","entries","forEach","handlebars","registerHelper","action","outputPath","compile","path","fullPath","type","templateFile","templatePath","templateContent","fs","readFile","compiled","output","data","ensureDir","dirname","writeFile","pathExists","content","modified","transform"],"mappings":";;;;AAIA;MACaA,MAAAA,GAAS,UAAA;IACpB,MAAM,EAAEC,IAAI,E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