| 27 | * Configures the package |
| 28 | */ |
| 29 | export async function configure(command: Configure) { |
| 30 | /** |
| 31 | * Prompt to select the store when --store flag is not used |
| 32 | */ |
| 33 | const store: string = |
| 34 | command.parsedFlags.store ?? |
| 35 | (await command.prompt.choice( |
| 36 | 'Select the store you want to use for sessions', |
| 37 | Object.entries(STORES).map(([name, { name: message }]) => ({ name, message })) |
| 38 | )) |
| 39 | |
| 40 | if (!Object.hasOwn(STORES, store)) { |
| 41 | command.logger.error( |
| 42 | `The selected store "${store}" is invalid. Select one from: ${string.sentence(Object.keys(STORES))}` |
| 43 | ) |
| 44 | command.exitCode = 1 |
| 45 | return |
| 46 | } |
| 47 | |
| 48 | const codemods = await command.createCodemods() |
| 49 | |
| 50 | /** |
| 51 | * Publish config file |
| 52 | */ |
| 53 | await codemods.makeUsingStub(stubsRoot, 'config/session.stub', { store }) |
| 54 | |
| 55 | /** |
| 56 | * Publish the sessions table migration for the database store |
| 57 | */ |
| 58 | if (store === 'database') { |
| 59 | await codemods.makeUsingStub(stubsRoot, 'make/migration/sessions.stub', { |
| 60 | migration: { tableName: 'sessions', prefix: Date.now() }, |
| 61 | }) |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Define environment variables |
| 66 | */ |
| 67 | await codemods.defineEnvVariables({ SESSION_DRIVER: store }) |
| 68 | |
| 69 | /** |
| 70 | * Define environment variables validations. The memory store is |
| 71 | * always available and commonly used in the testing environment. |
| 72 | */ |
| 73 | await codemods.defineEnvValidations({ |
| 74 | variables: { |
| 75 | SESSION_DRIVER: `Env.schema.enum(['${store}', 'memory'] as const)`, |
| 76 | }, |
| 77 | leadingComment: 'Variables for configuring session package', |
| 78 | }) |
| 79 | |
| 80 | /** |
| 81 | * Register middleware |
| 82 | */ |
| 83 | await codemods.registerMiddleware('router', [ |
| 84 | { |
| 85 | path: '@adonisjs/session/session_middleware', |
| 86 | }, |