Skip to content

Commit

Permalink
feat: add configure hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien-R44 committed Jul 15, 2023
1 parent 48cb1c2 commit c8dce18
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 0 deletions.
42 changes: 42 additions & 0 deletions configure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* @adonisjs/redis
*
* (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'

/**
* Configures the package
*/
export async function configure(command: Configure) {
/**
* Publish config file
*/
await command.publishStub('config/redis.stub')

/**
* Publish typings file
*/
await command.publishStub('types/redis.stub')

/**
* Add environment variables
*/
await command.defineEnvVariables({
REDIS_CONNECTION: 'local',
REDIS_HOST: '127.0.0.1',
REDIS_PORT: '6379',
REDIS_PASSWORD: '',
})

/**
* Add provider to rc file
*/
await command.updateRcFile((rcFile) => {
rcFile.addProvider('@adonisjs/redis/providers/redis_provider')
})
}
2 changes: 2 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
import './src/types/extended.js'

export { defineConfig } from './src/define_config.js'
export { stubsRoot } from './stubs/index.js'
export { configure } from './configure.js'
30 changes: 30 additions & 0 deletions stubs/config/redis.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
to: {{ app.configPath('redis.ts') }}
---

import env from '#start/env'
import { defineConfig } from '@adonisjs/redis'

export default defineConfig({
connection: env.get('REDIS_CONNECTION'),

connections: {
/*
|--------------------------------------------------------------------------
| The default connection
|--------------------------------------------------------------------------
|
| The main connection you want to use to execute redis commands. The same
| connection will be used by the session provider, if you rely on the
| redis driver.
|
*/
local: {
host: env.get('REDIS_HOST'),
port: env.get('REDIS_PORT'),
password: env.get('REDIS_PASSWORD', ''),
db: 0,
keyPrefix: '',
},
},
})
12 changes: 12 additions & 0 deletions stubs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* @adonisjs/redis
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { getDirname } from '@poppinss/utils'

export const stubsRoot = getDirname(import.meta.url)
8 changes: 8 additions & 0 deletions stubs/types/redis.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
to: {{ app.makePath('types/redis.ts') }}
---
import redis from '#config/redis'

declare module '@adonisjs/redis/types' {
export interface RedisConnections extends InferConnections<typeof redis> {}
}
80 changes: 80 additions & 0 deletions test/configure.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* @adonisjs/mail
*
* (c) AdonisJS
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { fileURLToPath } from 'node:url'
import { test } from '@japa/runner'
import { IgnitorFactory } from '@adonisjs/core/factories'
import Configure from '@adonisjs/core/commands/configure'

export const BASE_URL = new URL('./tmp/', import.meta.url)

async function setupConfigureCommand() {
const ignitor = new IgnitorFactory()
.withCoreProviders()
.withCoreConfig()
.create(BASE_URL, {
importer: (filePath) => {
if (filePath.startsWith('./') || filePath.startsWith('../')) {
return import(new URL(filePath, BASE_URL).href)
}

return import(filePath)
},
})

const app = ignitor.createApp('web')
await app.init()
await app.boot()

const ace = await app.container.make('ace')
const command = await ace.create(Configure, ['../../index.js'])

command.ui.switchMode('raw')

return { command }
}

test.group('Configure', (group) => {
group.each.setup(({ context }) => {
context.fs.baseUrl = BASE_URL
context.fs.basePath = fileURLToPath(BASE_URL)
})

test('publish config and types files', async ({ assert }) => {
const { command } = await setupConfigureCommand()

await command.exec()

await assert.fileExists('config/redis.ts')
await assert.fileContains('config/redis.ts', 'export default defineConfig({')
await assert.fileExists('types/redis.ts')
})

test('add redis_provider to the rc file', async ({ assert }) => {
const { command } = await setupConfigureCommand()

await command.exec()

await assert.fileExists('.adonisrc.json')
await assert.fileContains('.adonisrc.json', '"@adonisjs/redis/providers/redis_provider"')
})

test('add env variables for the selected drivers', async ({ assert, fs }) => {
const { command } = await setupConfigureCommand()

await fs.create('.env', '')
await command.exec()

await assert.fileContains('.env', 'REDIS_CONNECTION=local')
await assert.fileContains('.env', 'REDIS_HOST=127.0.0.1')

await assert.fileContains('.env', 'REDIS_PORT=6379')
await assert.fileContains('.env', 'REDIS_PASSWORD=')
})
})

0 comments on commit c8dce18

Please sign in to comment.