import { mkdir } from 'node:fs/promises' import { dirname, join, relative } from 'node:path' import { Str } from '@elvel/support' import { Command } from './command.ts' import type { CommandDefinition } from './signature.ts' /** * Base class for `make:*` commands. * * Stubs are resolved from the application first (`stubs/` in the project * root) and fall back to the ones shipped with this package — the same override * mechanism as Laravel's `stub:publish`. */ export abstract class GeneratorCommand extends Command { /** * `--pretend` is added to every generator here rather than to each signature. * * Twenty commands would otherwise have to remember to declare it, and the one * that forgot would reject the flag with "option does not exist" — which reads * as though the feature is missing rather than the declaration. */ override get definition(): CommandDefinition { const definition = super.definition if (definition.options.some((option) => option.name === 'pretend')) return definition return { ...definition, options: [ ...definition.options, { name: 'pretend', description: 'Show what would be written, without writing it', acceptsValue: false, isArray: false, default: false } ] } } /** Stub file name, e.g. `controller.stub`. */ protected abstract stub(): string /** Human label used in output: "Controller", "View". */ protected abstract type(): string /** Absolute destination path for the generated file. */ protected abstract destination(name: string): string /** Placeholder values substituted into the stub. */ protected replacements(name: string): Record { // Casing derives from the base name: `pages.about` is the view "about" // inside `pages`, not a thing called "PagesAbout". const base = this.baseName(name) return { class: this.className(name), name, studly: Str.studly(base), camel: Str.camel(base), kebab: Str.kebab(base), snake: Str.snake(base), plural: Str.kebab(Str.plural(base)), singular: Str.kebab(Str.singular(base)) } } async handle(): Promise { const name = this.argument('name') if (name === '') { this.error('A name is required.') return 1 } const destination = this.destination(name) const file = Bun.file(destination) if ((await file.exists()) && !this.flag('force')) { this.error(`${this.type()} already exists: ${this.relative(destination)}`) this.comment('Pass --force to overwrite it.') return 1 } const stub = await this.readStub() const contents = Str.replacePlaceholders(stub, this.replacements(name)) /** * `--pretend` writes nothing and shows what would have been written. * * Worth having for the same reason `migrate --pretend` is: a generator that * lands a file in the wrong directory is easy to do and tedious to undo, * especially when it is one of several a single command writes. */ if (this.flag('pretend')) { this.output.tag('INFO', `${this.type()} would be created: ${this.relative(destination)}`) this.output.line() this.output.comment(contents.trimEnd()) return 0 } await mkdir(dirname(destination), { recursive: true }) await Bun.write(destination, contents) this.output.tag('INFO', `${this.type()} created: ${this.relative(destination)}`) return 0 } protected className(name: string): string { return Str.studly(this.baseName(name)) } /** `admin/posts` -> `posts`, so nested names keep their class name clean. */ protected baseName(name: string): string { return Str.afterLast(name.replace(/\\/g, '/'), '/') } /** `admin/posts` -> `admin`, the subdirectory part of a nested name. */ protected subDirectory(name: string): string { const normalized = name.replace(/\\/g, '/') return normalized.includes('/') ? normalized.slice(0, normalized.lastIndexOf('/')) : '' } protected relative(path: string): string { return relative(this.app.basePath(), path) } /** * Resolve the stub contents. Overridable so a package outside @elvel/console * can ship its own stubs directory. */ protected async readStub(): Promise { const published = Bun.file(this.app.basePath('stubs', this.stub())) if (await published.exists()) return published.text() const shipped = Bun.file(join(import.meta.dir, '..', 'stubs', this.stub())) if (await shipped.exists()) return shipped.text() throw new Error(`Stub "${this.stub()}" not found.`) } }