-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: add make:producer command #13
base: feat/kafka-authentication
Are you sure you want to change the base?
Changes from all commits
3385609
bef6439
b80b53d
9d5d98e
29c399f
6100d47
5f82683
7b1ff9d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { BaseCommand, args, flags } from '@adonisjs/core/ace' | ||
import string from '@adonisjs/core/helpers/string' | ||
import { VariableDeclarationKind } from 'ts-morph' | ||
import { stubsRoot } from '../stubs/main.js' | ||
import { CommandOptions } from '@adonisjs/core/types/ace' | ||
|
||
export default class MakeProducer extends BaseCommand { | ||
static commandName = 'make:producer' | ||
static description = 'Make a new Kafka producer' | ||
static options: CommandOptions = { | ||
allowUnknownFlags: true, | ||
} | ||
|
||
/** | ||
* The name of the model file. | ||
*/ | ||
@args.string({ description: 'Kafka topic to producer data to' }) | ||
declare topic: string | ||
|
||
/** | ||
* Defines if we generate the factory for the model. | ||
*/ | ||
@flags.string({ | ||
name: 'producer', | ||
alias: 'p', | ||
description: 'The producer group in #start/kafka.ts', | ||
default: 'default', | ||
}) | ||
declare producer: string | ||
|
||
/** | ||
* Execute command | ||
*/ | ||
async run(): Promise<void> { | ||
const codemods = await this.createCodemods() | ||
|
||
const ProducerVariable = `${this.app.generators.modelName(this.parsed.flags.producer)}Producer` | ||
const ProducerId = string.create(this.parsed.flags.producer).dashCase() | ||
|
||
await codemods.makeUsingStub(stubsRoot, 'make/producer.stub', { | ||
topic: this.topic, | ||
ProducerClass: ProducerVariable, | ||
entity: this.app.generators.createEntity(this.topic), | ||
}) | ||
|
||
const project = await codemods.getTsMorphProject() | ||
if (project) { | ||
const startFile = await project.getSourceFileOrThrow(this.app.startPath('kafka.ts')) | ||
|
||
const producerDeclaration = startFile.getVariableDeclaration(ProducerVariable) | ||
if (!producerDeclaration) { | ||
// adds the following to #start/kafka.ts: | ||
// export const {{ProducerVariable}} = await Kafka.createProducer('{{ProducerId}}') | ||
startFile.insertVariableStatement(2, { | ||
isExported: true, | ||
declarationKind: VariableDeclarationKind.Const, | ||
declarations: [ | ||
{ | ||
name: ProducerVariable, | ||
initializer: `await Kafka.createProducer('${ProducerId}')`, | ||
}, | ||
], | ||
}) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This adds the It's a little cludgey but I think it's what we'd want. |
||
|
||
await startFile.save() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,8 +25,8 @@ export async function configure(command: ConfigureCommand) { | |
/** | ||
* Publish config file | ||
*/ | ||
await codemods.makeUsingStub(stubsRoot, 'stubs/config/kafka.stub', {}) | ||
await codemods.makeUsingStub(stubsRoot, 'stubs/start/kafka.stub', {}) | ||
await codemods.makeUsingStub(stubsRoot, 'config/kafka.stub', {}) | ||
await codemods.makeUsingStub(stubsRoot, 'start/kafka.stub', {}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have no idea why I needed to change these, but after adjusting tsc config, suddenly it didn't find the files correctly. Looking at other packages, and it seems I shouldn't have included the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, I had the reverse when I messed with this last, it wouldn’t work without |
||
|
||
/** | ||
* Define environment variables | ||
|
@@ -69,7 +69,22 @@ export async function configure(command: ConfigureCommand) { | |
* Register provider | ||
*/ | ||
await codemods.updateRcFile((rcFile) => { | ||
rcFile.addCommand(`${command.name}/commands`) | ||
rcFile.addProvider(`${command.name}/kafka_provider`) | ||
rcFile.addPreloadFile(`#start/kafka`) | ||
}) | ||
|
||
/** | ||
* Install packages | ||
*/ | ||
// Prompt when `install` or `--no-install` flags are not used | ||
let shouldInstallPackages: boolean | undefined = command.parsedFlags.install | ||
if (shouldInstallPackages === undefined) { | ||
shouldInstallPackages = await command.prompt.confirm( | ||
`Do you want to install additional packages required by "${command.name}"?` | ||
) | ||
} | ||
if (shouldInstallPackages) { | ||
await codemods.installPackages([{ name: 'kafkajs', isDevDependency: false }]) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even though kafka is a peerDep, this makes sure we install it. There isn't a way to specify a version here though. In theory the peerDep will have us covered, so maybe we don't need this (was inspired by lucid's configure.ts) |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file seems to not be emitted using the previous tsc / tsup configuration, hence needing to change.