This repository has been archived by the owner on Nov 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
configure.ts
122 lines (108 loc) · 3.35 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
/*
|--------------------------------------------------------------------------
| Configure hook
|--------------------------------------------------------------------------
|
| The configure hook is called when someone runs "node ace configure <package>"
| command. You are free to perform any operations inside this function to
| configure the package.
|
| To make things easier, you have access to the underlying "ConfigureCommand"
| instance and you can use codemods to modify the source files.
|
*/
import ConfigureCommand from '@adonisjs/core/commands/configure'
import string from '@adonisjs/core/helpers/string'
import { stubsRoot } from './stubs/main.js'
type EngineConfig = {
name: string
dependencies: string[]
envVars: Record<string, string>
envValidation: Record<string, string>
}
const ENGINES: Record<string, EngineConfig> = {
algolia: {
name: 'Algolia',
dependencies: ['algoliasearch'],
envVars: {
ALGOLIA_APP_ID: '',
ALGOLIA_API_KEY: '',
},
envValidation: {
ALGOLIA_APP_ID: `Env.schema.string()`,
ALGOLIA_API_KEY: `Env.schema.string()`,
},
},
meilisearch: {
name: 'Meilisearch',
dependencies: ['meilisearch'],
envVars: {
MEILISEARCH_HOST: ``,
MEILISEARCH_API_KEY: ``,
},
envValidation: {
MEILISEARCH_HOST: `Env.schema.string({ format: 'host' })`,
MEILISEARCH_API_KEY: `Env.schema.string.optional()`,
},
},
typesense: {
name: 'Typesense',
dependencies: ['typesense'],
envVars: {
TYPESENSE_NODE_URL: ``,
TYPESENSE_API_KEY: ``,
},
envValidation: {
TYPESENSE_NODE_URL: `Env.schema.string({ format: 'host' })`,
TYPESENSE_API_KEY: `Env.schema.string()`,
},
},
}
export async function configure(command: ConfigureCommand) {
let engineName: string | undefined = command.parsedFlags.engine
let shouldInstallPackages: boolean | undefined = command.parsedFlags.install
if (engineName === undefined) {
engineName = await command.prompt.choice(
'Select the Search engine you want to use',
Object.entries(ENGINES).map(([key, value]) => ({
name: key,
message: value.name,
})),
{ validate: (value) => !!value }
)
}
const engine = ENGINES[engineName!]
if (!engine) {
command.logger.error(
`The selected search engine "${engineName}" is invalid. Select one from: ${string.sentence(
Object.values(ENGINES).map((e) => e.name)
)}`
)
command.exitCode = 1
return
}
if (shouldInstallPackages === undefined) {
shouldInstallPackages = await command.prompt.confirm(
'Do you want to install additional packages required by "@foadonis/magnify" and the selected search engine?'
)
}
const codemods = await command.createCodemods()
await codemods.updateRcFile((rcFile) => {
rcFile.addCommand('@foadonis/magnify/commands')
rcFile.addProvider('@foadonis/magnify/magnify_provider')
})
await codemods.makeUsingStub(stubsRoot, `config/${engineName}.stub`, {})
await codemods.defineEnvValidations({
variables: engine.envValidation,
leadingComment: 'Variables for configuring Magnify search engine',
})
await codemods.defineEnvVariables(engine.envVars)
if (shouldInstallPackages) {
await codemods.installPackages(
engine.dependencies.map((name) => ({
name,
isDevDependency: false,
}))
)
}
}