-
Notifications
You must be signed in to change notification settings - Fork 8
/
instructions.ts
186 lines (158 loc) · 4.99 KB
/
instructions.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import * as sinkStatic from '@adonisjs/sink'
import { ApplicationContract } from '@ioc:Adonis/Core/Application'
import pluralize from 'pluralize'
import { join } from 'path'
type InstructionsState = {
notificationsTableName: string
notificationsSchemaName: string
notifiableTableName: string
channels: ('database' | 'mail')[]
hasChannel: {
database: boolean
mail: boolean
}
}
const CONTRACTS_PARTIALS_BASE = './contract/partials'
const CONFIG_PARTIALS_BASE = './config/partials'
const CHANNEL_PROMPT_CHOICES = [
{
name: 'database' as const,
message: 'Database',
hint: '(Stores notifications in a database table)',
},
{
name: 'mail' as const,
message: 'Mail',
hint: '(Sends notifications via mail)',
},
]
function getStub(...paths: string[]) {
return join(__dirname, 'templates', ...paths)
}
function makeNotificationsMigration(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
state: InstructionsState
) {
const migrationsDirectory = app.directoriesMap.get('migrations') || 'database'
const migrationPath = join(
migrationsDirectory,
`${Date.now()}_${state.notificationsTableName}.ts`
)
const template = new sink.files.MustacheFile(projectRoot, migrationPath, getStub('migration.txt'))
if (template.exists()) {
sink.logger.action('create').skipped(`${migrationPath} file already exists`)
return
}
template.apply(state).commit()
sink.logger.action('create').succeeded(migrationPath)
}
function makeContract(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
state: InstructionsState
) {
const contractsDirectory = app.directoriesMap.get('contracts') || 'contracts'
const contractPath = join(contractsDirectory, 'notification.ts')
const template = new sink.files.MustacheFile(
projectRoot,
contractPath,
getStub('contract/contract.txt')
)
template.overwrite = true
const partials: any = {}
state.channels.forEach((channel) => {
partials[`${channel}_channel`] = getStub(CONTRACTS_PARTIALS_BASE, `${channel}-channel.txt`)
})
template.apply(state).partials(partials).commit()
sink.logger.action('create').succeeded(contractPath)
}
function makeConfig(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic,
state: InstructionsState
) {
const configDirectory = app.directoriesMap.get('config') || 'config'
const configPath = join(configDirectory, 'notification.ts')
const template = new sink.files.MustacheFile(
projectRoot,
configPath,
getStub('config/config.txt')
)
template.overwrite = true
const partials: any = {}
state.channels.forEach((channel) => {
partials[`${channel}_channel`] = getStub(CONFIG_PARTIALS_BASE, `${channel}-channel.txt`)
})
template.apply(state).partials(partials).commit()
sink.logger.action('create').succeeded(configPath)
}
async function getChannels(sink: typeof sinkStatic) {
return sink
.getPrompt()
.multiple(
'Select which channels you want to use for notifications (select using space)',
CHANNEL_PROMPT_CHOICES,
{
validate(choices) {
return choices && choices.length
? true
: 'Select one or more channels for notifying users'
},
}
)
}
async function getMigrationConsent(sink: typeof sinkStatic): Promise<boolean> {
return sink.getPrompt().confirm(`You want to create a notifications table?`)
}
async function getNotificationTableName(
sink: typeof sinkStatic,
notifiableTableName: string
): Promise<string> {
const singularNotifiableTableName = pluralize.singular(notifiableTableName)
return sink.getPrompt().ask('Enter the notifications table name', {
default: `${singularNotifiableTableName}_notifications`,
validate(value) {
return !!value.trim().length
},
})
}
async function getNotifiableTableName(sink: typeof sinkStatic): Promise<string> {
return sink.getPrompt().ask('Enter the table name of the model you want to notify', {
default: 'users',
validate(value) {
return !!value.trim().length
},
})
}
export default async function instructions(
projectRoot: string,
app: ApplicationContract,
sink: typeof sinkStatic
) {
const state: InstructionsState = {
notificationsSchemaName: 'Notifications',
notificationsTableName: '',
notifiableTableName: '',
channels: [],
hasChannel: {
database: false,
mail: false,
},
}
state.channels = await getChannels(sink)
state.channels.forEach((channel) => (state.hasChannel[channel] = true))
if (state.hasChannel.database) {
const notificationMigrationConsent = await getMigrationConsent(sink)
if (notificationMigrationConsent) {
state.notifiableTableName = await getNotifiableTableName(sink)
state.notificationsTableName = await getNotificationTableName(sink, state.notifiableTableName)
makeNotificationsMigration(projectRoot, app, sink, state)
}
}
makeContract(projectRoot, app, sink, state)
makeConfig(projectRoot, app, sink, state)
}