Skip to content

Commit

Permalink
feat: add makeUsingStub to Ace BaseCommand (#4147)
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed May 9, 2023
1 parent 4e002e2 commit 9cbbc09
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
26 changes: 1 addition & 25 deletions commands/make/_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* file that was distributed with this source code.
*/

import { slash } from '@poppinss/utils'
import { stubsRoot } from '../../stubs/index.js'
import { BaseCommand } from '../../modules/ace/main.js'
import type { CommandOptions } from '../../types/ace.js'
Expand All @@ -27,29 +26,6 @@ export default abstract class extends BaseCommand {
* Generates the resource from stub
*/
protected async generate(stubPath: string, stubState: Record<string, any>) {
const stub = await this.app.stubs.build(stubPath, { source: stubsRoot })
const output = await stub.generate(
Object.assign(
{
flags: this.parsed.flags,
},
stubState
)
)

const entityFileName = slash(this.app.relativePath(output.destination))
if (output.status === 'skipped') {
this.logger.action(`create ${entityFileName}`).skipped(output.skipReason)
return {
...output,
relativeFileName: entityFileName,
}
}

this.logger.action(`create ${entityFileName}`).succeeded()
return {
...output,
relativeFileName: entityFileName,
}
return this.makeUsingStub(stubPath, stubState, stubsRoot)
}
}
48 changes: 48 additions & 0 deletions modules/ace/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,49 @@
*/

import { BaseCommand as AceBaseCommand, ListCommand as AceListCommand } from '@adonisjs/ace'
import { slash } from '@poppinss/utils'

import { Kernel } from './kernel.js'
import type { ApplicationService } from '../../src/types.js'
import type { CommandOptions, ParsedOutput, UIPrimitives } from '../../types/ace.js'

/**
* Wrapper around the stub generation logic.
* Allow commands to easily generate files from given stubs
*/
class StubGenerator {
#command: BaseCommand | ListCommand
#flags: Record<string, any>

constructor(command: BaseCommand, flags: Record<string, any>) {
this.#command = command
this.#flags = flags
}

async generate(stubsRoot: string, stubPath: string, stubState: Record<string, any>) {
const stub = await this.#command.app.stubs.build(stubPath, { source: stubsRoot })
const output = await stub.generate(Object.assign({ flags: this.#flags }, stubState))

const entityFileName = slash(this.#command.app.relativePath(output.destination))
const result = { ...output, relativeFileName: entityFileName }

if (output.status === 'skipped') {
this.#command.logger.action(`create ${entityFileName}`).skipped(output.skipReason)
return result
}

this.#command.logger.action(`create ${entityFileName}`).succeeded()
return result
}
}

/**
* The base command to create custom ace commands. The AdonisJS base commands
* receives the application instance
*/
export class BaseCommand extends AceBaseCommand {
stubGenerator: StubGenerator = new StubGenerator(this, this.parsed?.flags || {})

static options: CommandOptions = {}

get staysAlive() {
Expand Down Expand Up @@ -62,6 +95,13 @@ export class BaseCommand extends AceBaseCommand {
*/
completed?(..._: any[]): any

/**
* Make a new file using the given stub
*/
async makeUsingStub(stubPath: string, stubState: Record<string, any>, stubsRoot: string) {
return this.stubGenerator.generate(stubsRoot, stubPath, stubState)
}

/**
* Executes the command
*/
Expand Down Expand Up @@ -118,6 +158,7 @@ export class BaseCommand extends AceBaseCommand {
* The List command is used to display a list of commands
*/
export class ListCommand extends AceListCommand implements BaseCommand {
stubGenerator: StubGenerator = new StubGenerator(this, this.parsed?.flags || {})
static options: CommandOptions = {}

get staysAlive() {
Expand All @@ -138,6 +179,13 @@ export class ListCommand extends AceListCommand implements BaseCommand {
super(kernel, parsed, ui, prompt)
}

/**
* Make a new file using the given stub
*/
async makeUsingStub(stubPath: string, stubState: Record<string, any>, stubsRoot: string) {
return this.stubGenerator.generate(stubsRoot, stubPath, stubState)
}

/**
* Terminate the app. A command should prefer calling this method
* over the "app.terminate", because this method only triggers
Expand Down

0 comments on commit 9cbbc09

Please sign in to comment.