-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
configure.ts
203 lines (186 loc) · 5.58 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* @adonisjs/drive
*
* (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 ConfigureCommand from '@adonisjs/core/commands/configure'
import { stubsRoot } from './stubs/main.js'
/**
* List of available storage services
*/
const STORAGE_SERVICES = {
fs: {
name: 'Local filesystem',
env: [],
dependencies: [],
},
s3: {
name: 'AWS S3',
env: [
{ name: 'AWS_ACCESS_KEY_ID', value: '', schema: 'Env.schema.string()' },
{ name: 'AWS_SECRET_ACCESS_KEY', value: '', schema: 'Env.schema.string()' },
{ name: 'AWS_REGION', value: '', schema: 'Env.schema.string()' },
{ name: 'S3_BUCKET', value: '', schema: 'Env.schema.string()' },
],
dependencies: ['@aws-sdk/client-s3', '@aws-sdk/s3-request-presigner'],
},
spaces: {
name: 'Digital Ocean Spaces',
env: [
{ name: 'SPACES_KEY', value: '', schema: 'Env.schema.string()' },
{ name: 'SPACES_SECRET', value: '', schema: 'Env.schema.string()' },
{ name: 'SPACES_REGION', value: '', schema: 'Env.schema.string()' },
{ name: 'SPACES_BUCKET', value: '', schema: 'Env.schema.string()' },
{
name: 'SPACES_ENDPOINT',
value: `https://\${SPACES_REGION}.digitaloceanspaces.com`,
schema: 'Env.schema.string()',
},
],
dependencies: ['@aws-sdk/client-s3', '@aws-sdk/s3-request-presigner'],
},
r2: {
name: 'Cloudflare R2',
env: [
{ name: 'R2_KEY', value: '', schema: 'Env.schema.string()' },
{ name: 'R2_SECRET', value: '', schema: 'Env.schema.string()' },
{ name: 'R2_BUCKET', value: '', schema: 'Env.schema.string()' },
{ name: 'R2_ENDPOINT', value: '', schema: 'Env.schema.string()' },
],
dependencies: ['@aws-sdk/client-s3', '@aws-sdk/s3-request-presigner'],
},
gcs: {
name: 'Google Cloud Storage',
env: [
{ name: 'GCS_KEY', value: 'file://./gcs_key.json', schema: 'Env.schema.string()' },
{ name: 'GCS_BUCKET', value: '', schema: 'Env.schema.string()' },
],
dependencies: ['@google-cloud/storage'],
},
}
/**
* List of known services
*/
const SERVICES_NAMES = Object.keys(STORAGE_SERVICES) as (keyof typeof STORAGE_SERVICES)[]
export async function configure(command: ConfigureCommand) {
/**
* Read services from the "--services" CLI flag
*/
let selectedServices:
| keyof typeof STORAGE_SERVICES
| (keyof typeof STORAGE_SERVICES)[]
| undefined = command.parsedFlags.services
/**
* Should dependencies be installed
*/
let shouldInstallPackages: boolean | undefined = command.parsedFlags.install
/**
* Display prompt when no services are specified
* via the CLI flag.
*/
if (!selectedServices) {
selectedServices = await command.prompt.multiple(
'Select the storage services you want to use',
SERVICES_NAMES.map((service) => {
return {
name: service,
message: STORAGE_SERVICES[service].name,
}
}),
{
validate(values) {
return !values || !values.length ? 'Please select one or more services' : true
},
}
)
}
/**
* Normalized list of services
*/
const services = typeof selectedServices === 'string' ? [selectedServices] : selectedServices!
const unknownServices = services.find((service) => !SERVICES_NAMES.includes(service))
if (unknownServices) {
command.exitCode = 1
command.logger.logError(
`Invalid service "${unknownServices}". Supported services are: ${string.sentence(
SERVICES_NAMES
)}`
)
return
}
/**
* Create a flat collection of dependencies to install
* based upon the configured services.
*/
const pkgsToInstall = services
.flatMap((service) => STORAGE_SERVICES[service].dependencies)
.map((pkg) => {
return { name: pkg, isDevDependency: false }
})
/**
* Prompt to install additional services
*/
if (!shouldInstallPackages && pkgsToInstall.length) {
shouldInstallPackages = await command.prompt.confirm(
'Do you want to install additional packages required by "@adonisjs/drive"?'
)
}
const codemods = await command.createCodemods()
/**
* Publish config file
*/
await codemods.makeUsingStub(stubsRoot, 'config/drive.stub', {
services,
})
/**
* Publish provider
*/
await codemods.updateRcFile((rcFile) => {
rcFile.addProvider('@adonisjs/drive/drive_provider')
})
/**
* Define env variables for the selected services
*/
await codemods.defineEnvVariables(
services.reduce<Record<string, string>>(
(result, service) => {
STORAGE_SERVICES[service].env.forEach((envVariable) => {
result[envVariable.name] = envVariable.value
})
return result
},
{
DRIVE_DISK: services[0],
}
)
)
/**
* Define env variables validation for the selected services
*/
await codemods.defineEnvValidations({
leadingComment: 'Variables for configuring the drive package',
variables: services.reduce<Record<string, string>>(
(result, service) => {
STORAGE_SERVICES[service].env.forEach((envVariable) => {
result[envVariable.name] = envVariable.schema
})
return result
},
{
DRIVE_DISK: `Env.schema.enum(['${services.join("', '")}'] as const)`,
}
),
})
if (!pkgsToInstall.length) {
return
}
if (shouldInstallPackages) {
await codemods.installPackages(pkgsToInstall)
} else {
await codemods.listPackagesToInstall(pkgsToInstall)
}
}