Skip to content
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

Open
wants to merge 8 commits into
base: feat/kafka-authentication
Choose a base branch
from

Conversation

ThisIsMissEm
Copy link
Collaborator

This was really annoying to implement but gives us a node ace make:producer <topic-name> which can optionally have a --producer <producerName> passed. The annoying part was mostly in that tsup / tsc wasn't emitting the build/commands/make_producer.js file, and no matter what I tried I couldn't get it to emit this file.

This creates the app/producers/<TopicName>.ts file, and if necessary adds a producer to #start/kafka.ts:

export const DefaultProducer = await Kafka.createProducer('default')

The app/producers/<TopicName>.ts file needs to be able to import the <ProducerName>Producer from #start/kafka.ts, hence doing a codemod and automatically adding it.

E.g., in Webhooks, I would've run:

node ace make:producer webhook-events --producer webhooks
node ace make:producer webhook-failures --producer webhooks

Which would have given me the files that I have in that branch.

If you omit --producer <produceName> then it creates a default producer for you called DefaultProducer and automatically inserts that to #start/kafka.ts if necessary.

import { stubsRoot } from '../stubs/main.js'
import { CommandOptions } from '@adonisjs/core/types/ace'

export default class MakeProducer extends BaseCommand {
Copy link
Collaborator Author

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.

},
],
})
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds the export const DefaultProducer = await Kafka.createProducer('default') or if --producer webhooks then export const WebhookProducer = await Kafka.createProducer('webhooks') to #start/kafka.ts

It's a little cludgey but I think it's what we'd want.

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', {})
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 stubs/ originally.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 stubs/ even though none of the examples I based this off had them and I tried this in an act of desperation. I’m glad this work correctly now.

}
if (shouldInstallPackages) {
await codemods.installPackages([{ name: 'kafkajs', isDevDependency: false }])
}
Copy link
Collaborator Author

@ThisIsMissEm ThisIsMissEm May 29, 2024

Choose a reason for hiding this comment

The 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)

@ThisIsMissEm ThisIsMissEm changed the title Feat/make producer command Feature: add make:producer command May 29, 2024
@ThisIsMissEm ThisIsMissEm force-pushed the feat/kafka-authentication branch from 9d5d98e to f887cd0 Compare May 29, 2024 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants