-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.ts
75 lines (65 loc) · 2.25 KB
/
configure.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
|--------------------------------------------------------------------------
| Configure hook
|--------------------------------------------------------------------------
|
| The configure hook is called when someone runs "node ace configure <package>"
| command. You are free to perform any operations inside this function to
| configure the package.
|
| To make things easier, you have access to the underlying "ConfigureCommand"
| instance and you can use codemods to modify the source files.
|
*/
import ConfigureCommand from '@adonisjs/core/commands/configure'
import { stubsRoot } from './stubs/main.js'
/**
* Configures the package
*/
export async function configure(command: ConfigureCommand) {
const codemods = await command.createCodemods()
/**
* Publish config file
*/
await codemods.makeUsingStub(stubsRoot, 'stubs/config/kafka.stub', {})
await codemods.makeUsingStub(stubsRoot, 'stubs/start/kafka.stub', {})
/**
* Define environment variables
*/
await codemods.defineEnvVariables({
KAFKA_BROKERS: 'localhost:9092',
})
const project = await codemods.getTsMorphProject()
if (project) {
const envValidationsFile = await project.getSourceFileOrThrow(command.app.startPath('env.ts'))
const kafkaEnvModule = `${command.name}/env`
if (!envValidationsFile.getImportDeclaration(kafkaEnvModule)) {
envValidationsFile.addImportDeclaration({
namedImports: ['KafkaEnv'],
moduleSpecifier: kafkaEnvModule,
})
}
await envValidationsFile.emit()
}
/**
* Define environment variables validations
*/
await codemods.defineEnvValidations({
variables: {
KAFKA_BROKERS: `KafkaEnv.schema.brokers()`,
KAFKA_CLIENT_ID: `Env.schema.string.optional()`,
KAFKA_GROUP_ID: `Env.schema.string.optional()`,
KAFKA_CONNECTION_TIMEOUT: `Env.schema.number.optional()`,
KAFKA_REQUEST_TIMEOUT: `Env.schema.number.optional()`,
KAFKA_LOG_LEVEL: `Env.schema.enum.optional(['fatal', 'error', 'warn', 'info', 'debug', 'trace'])`,
},
leadingComment: 'Variables for configuring kafka package',
})
/**
* Register provider
*/
await codemods.updateRcFile((rcFile) => {
rcFile.addProvider(`${command.name}/kafka_provider`)
rcFile.addPreloadFile(`#start/kafka`)
})
}