-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigure.ts
More file actions
94 lines (81 loc) · 2.23 KB
/
configure.ts
File metadata and controls
94 lines (81 loc) · 2.23 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* @adonisjs/queue
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { stubsRoot } from './stubs/main.js'
import type Configure from '@adonisjs/core/commands/configure'
const DRIVERS = ['redis', 'database'] as const
const DRIVERS_INFO: Record<
(typeof DRIVERS)[number],
{
envVars?: Record<string, string>
envValidations?: Record<string, string>
}
> = {
redis: {},
database: {},
}
/**
* Configures the package
*/
export async function configure(command: Configure) {
const driver = await command.prompt.choice(
'Select the queue driver you plan to use',
['redis', 'database'],
{ hint: 'You can always change it later' }
)
const codemods = await command.createCodemods()
/**
* Register provider, command and preload file
*/
await codemods.updateRcFile((rcFile) => {
rcFile
.addProvider('@adonisjs/queue/queue_provider')
.addCommand('@adonisjs/queue/commands')
.addPreloadFile('#start/scheduler', ['web'])
})
/**
* Define environment variables
*/
await codemods.defineEnvVariables({ QUEUE_DRIVER: driver })
/**
* Define environment validations
*/
await codemods.defineEnvValidations({
variables: {
QUEUE_DRIVER: `Env.schema.enum(['redis', 'database', 'sync'] as const)`,
},
leadingComment: 'Variables for configuring @adonisjs/queue',
})
const { envVars, envValidations } = DRIVERS_INFO[driver]
if (envVars) {
await codemods.defineEnvVariables(envVars)
}
if (envValidations) {
await codemods.defineEnvValidations({ variables: envValidations })
}
/**
* Publish config file
*/
await codemods.makeUsingStub(stubsRoot, 'config/queue.stub', { driver })
/**
* Publish scheduler preload file
*/
await codemods.makeUsingStub(stubsRoot, 'start/scheduler.stub', {})
/**
* Create migration for database driver
*/
if (driver === 'database') {
await codemods.makeUsingStub(stubsRoot, 'migration.stub', {
entity: command.app.generators.createEntity('queue'),
migration: {
folder: 'database/migrations',
fileName: `${new Date().getTime()}_create_queue_tables.ts`,
},
})
}
}