-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathconfigure.ts
110 lines (99 loc) · 2.71 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
/*
* @adonisjs/ally
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import type Configure from '@adonisjs/core/commands/configure'
import { stubsRoot } from './stubs/main.js'
/**
* List of available providers
*/
const AVAILABLE_PROVIDERS = [
'discord',
'facebook',
'github',
'google',
'linkedin',
'linkedinOpenidConnect',
'spotify',
'twitter',
]
/**
* Configures the package
*/
export async function configure(command: Configure) {
/**
* Read providers from the CLI flags
*/
let selectedProviders: string[] | string | undefined = command.parsedFlags.providers
/**
* Otherwise force prompt for selection
*/
if (!selectedProviders) {
selectedProviders = await command.prompt.multiple(
'Select the social auth providers you plan to use',
AVAILABLE_PROVIDERS,
{
validate(value) {
return !value || !value.length
? 'Select a social provider to configure the package'
: true
},
}
)
}
/**
* Cast CLI string value to an array
*/
let providers = (
typeof selectedProviders === 'string' ? [selectedProviders] : selectedProviders
) as string[]
/**
* Validate CLI selection to contain known providers
*/
const unknownProvider = providers.find((provider) => !AVAILABLE_PROVIDERS.includes(provider))
if (unknownProvider) {
command.exitCode = 1
command.logger.error(`Invalid social provider "${unknownProvider}"`)
return
}
const codemods = await command.createCodemods()
/**
* Publish config file
*/
await codemods.makeUsingStub(stubsRoot, 'config/ally.stub', {
providers: providers.map((provider) => {
return { provider, envPrefix: provider.toUpperCase() }
}),
})
/**
* Publish provider
*/
await codemods.updateRcFile((rcFile) => {
rcFile.addProvider('@adonisjs/ally/ally_provider')
})
/**
* Define env variables for the selected providers
*/
await codemods.defineEnvVariables(
providers.reduce<Record<string, string>>((result, provider) => {
result[`${provider.toUpperCase()}_CLIENT_ID`] = ''
result[`${provider.toUpperCase()}_CLIENT_SECRET`] = ''
return result
}, {})
)
/**
* Define env variables validation for the selected providers
*/
await codemods.defineEnvValidations({
variables: providers.reduce<Record<string, string>>((result, provider) => {
result[`${provider.toUpperCase()}_CLIENT_ID`] = 'Env.schema.string()'
result[`${provider.toUpperCase()}_CLIENT_SECRET`] = 'Env.schema.string()'
return result
}, {}),
leadingComment: 'Variables for configuring ally package',
})
}