-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfigure.ts
108 lines (96 loc) · 2.53 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
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
/*
* @adonisjs/limiter
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import string from '@adonisjs/core/helpers/string'
import type Configure from '@adonisjs/core/commands/configure'
import { stubsRoot } from './stubs/main.js'
/**
* Available stores for selection
*/
const KNOWN_STORES = ['database', 'redis']
/**
* Configures the limiter package
*/
export async function configure(command: Configure) {
/**
* Read store from the "--store" CLI flag
*/
let selectedStore: string | undefined = command.parsedFlags.store
/**
* Display prompts when no store have been selected
* via the CLI flag
*/
if (!selectedStore) {
selectedStore = await command.prompt.choice(
'Select the storage layer you want to use',
KNOWN_STORES,
{
validate(value) {
return !value ? 'Please select a store' : true
},
}
)
}
/**
* Ensure the select store is valid
*/
if (!KNOWN_STORES.includes(selectedStore!)) {
command.exitCode = 1
command.logger.logError(
`Invalid limiter store "${selectedStore}". Supported stores are: ${string.sentence(
KNOWN_STORES
)}`
)
return
}
const codemods = await command.createCodemods()
/**
* Publish config file
*/
await codemods.makeUsingStub(stubsRoot, 'config/limiter.stub', {
store: selectedStore,
})
/**
* Publish start/limiter file
*/
await codemods.makeUsingStub(stubsRoot, 'start/limiter.stub', {})
/**
* Publish provider
*/
await codemods.updateRcFile((rcFile) => {
rcFile.addProvider('@adonisjs/limiter/limiter_provider')
})
/**
* Publish migration when using database to store
* rate limits
*/
if (selectedStore === 'database') {
await codemods.makeUsingStub(stubsRoot, 'make/migration/rate_limits.stub', {
entity: command.app.generators.createEntity('rate_limits'),
migration: {
folder: 'database/migrations',
fileName: `${new Date().getTime()}_create_rate_limits_table.ts`,
},
})
}
/**
* Define env variables for the selected store
*/
await codemods.defineEnvVariables({
LIMITER_STORE: selectedStore!,
})
/**
* Define env variables validation for the selected store
*/
await codemods.defineEnvValidations({
leadingComment: 'Variables for configuring the limiter package',
variables: {
LIMITER_STORE: `Env.schema.enum(['${selectedStore}', 'memory'] as const)`,
},
})
}