-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfigure.ts
73 lines (63 loc) · 2.03 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
import type Configure from '@adonisjs/core/commands/configure'
import { stubsRoot } from './stubs/main.js'
import string from '@poppinss/utils/string'
/**
* List of supported transports
*/
const KNOWN_CHANNELS = ['database', 'mail']
export async function configure(command: Configure) {
/**
* Read channels from the "--channels" CLI flag
*/
let selectedChannels: string | string[] | undefined = command.parsedFlags.channels
/**
* Display prompts when channels have been selected
* via the CLI flag
*/
if (!selectedChannels) {
selectedChannels = await command.prompt.multiple(
'Select the notification channels you want to use',
KNOWN_CHANNELS,
{
validate(values) {
return !values || !values.length ? 'Please select one or more channels' : true
},
}
)
}
/**
* Normalized list of transports
*/
const channels = typeof selectedChannels === 'string' ? [selectedChannels] : selectedChannels!
const unknownChannel = channels.find((transport) => !KNOWN_CHANNELS.includes(transport))
if (unknownChannel) {
command.exitCode = 1
command.logger.logError(
`Invalid channel "${unknownChannel}". Supported transports are: ${string.sentence(
KNOWN_CHANNELS
)}`
)
return
}
const codemods = await command.createCodemods()
// Publish config file
await codemods.makeUsingStub(stubsRoot, 'config/notification.stub', {
channels,
})
// Publish migration
await codemods.makeUsingStub(stubsRoot, 'make/migration/notifications.stub', {
entity: command.app.generators.createEntity('notifications'),
migration: {
folder: 'database/migrations',
fileName: `${new Date().getTime()}_create_notifications_table.ts`,
},
})
/**
* Publish provider and command
*/
await codemods.updateRcFile((rcFile) => {
rcFile.addProvider('@osenco/adonisjs-notifications/notification_provider')
rcFile.addCommand('@osenco/adonisjs-notifications/commands')
rcFile.setDirectory('notifications', 'app/notifications')
})
}