Skip to content

Commit

Permalink
docs(README): add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 24, 2023
1 parent c80c462 commit bca7fce
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 16 deletions.
83 changes: 83 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,86 @@ These presets are a collection of functions you can execute to configure an Adon
For example: When you create a new AdonisJS application, you can configure Lucid and also you can create a new app without Lucid and configure it later.

So, instead of duplicating the code in multiple places, we create re-usable presets and use them with individual packages and the [create-adonisjs](https://npm.im/create-adonisjs) initializer.

> [!NOTE]
> Presets do not trigger any prompts and exposes a coding interface.
## Auth preset

The `auth` presets configures the `@adonisjs/auth` package. The package must be installed to run this preset.

```ts
import { Kernel } from '@adonisjs/core/ace'
import { Application } from '@adonisjs/core/app'
import { presetAuth } from '@adonisjs/presets/auth'
import { Codemods } from '@adonisjs/core/ace/codemods'

/**
* Create application instance. Inside an Ace command, you
* get access to it using `this.app` property.
*/
const app = new Application(baseURL, {
importer: () => {},
})

/**
* Create Ace kernel instance to get CLI logger reference.
* Inside an Ace command you can access it using this.logger
* property.
*/
const cliLogger = new Kernel(app).ui.logger

/**
* Create codemods instance. Codemods are needed to modify
* source files.
*/
const codemods = new Codemods(app, cliLogger)

/**
* Apply preset
*/
await presetAuth(codemods, {
guard: 'session',
userProvider: 'lucid',
})
```

## Lucid preset

The `auth` presets configures the `@adonisjs/lucid` package. The package must be installed to run this preset.

```ts
import { Kernel } from '@adonisjs/core/ace'
import { Application } from '@adonisjs/core/app'
import { presetLucid } from '@adonisjs/presets/lucid'
import { Codemods } from '@adonisjs/core/ace/codemods'

/**
* Create application instance. Inside an Ace command, you
* get access to it using `this.app` property.
*/
const app = new Application(baseURL, {
importer: () => {},
})

/**
* Create Ace kernel instance to get CLI logger reference.
* Inside an Ace command you can access it using this.logger
* property.
*/
const cliLogger = new Kernel(app).ui.logger

/**
* Create codemods instance. Codemods are needed to modify
* source files.
*/
const codemods = new Codemods(app, cliLogger)

/**
* Apply preset
*/
await presetLucid(codemods, app, {
dialect: 'sqlite',
installPackages: true,
})
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"quick:test": "node --loader ts-node/esm --enable-source-maps bin/test.ts"
},
"exports": {
"./lucid": "./lucid/main.js",
"./auth": "./auth/main.js"
"./lucid": "./src/lucid/main.js",
"./auth": "./src/auth/main.js"
},
"keywords": [
"adonisjs",
Expand Down
19 changes: 11 additions & 8 deletions tests/auth/preset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

import dedent from 'dedent'
import { test } from '@japa/runner'
import { cliui } from '@poppinss/cliui'
import { Kernel } from '@adonisjs/core/ace'
import { presetAuth } from '../../src/auth/main.js'
import { createKernelFile, createSetupFiles, createCodeMods } from '../helpers.js'
import { createKernelFile, createSetupFiles, createCodeMods, createApp } from '../helpers.js'

test.group('Preset | Auth', (group) => {
group.each.disableTimeout()
Expand All @@ -20,8 +20,9 @@ test.group('Preset | Auth', (group) => {
await createSetupFiles(fs)
await createKernelFile(fs)

const logger = cliui({ mode: 'normal' }).logger
const codemods = await createCodeMods(fs, logger)
const app = await createApp(fs)
const logger = new Kernel(app).ui.logger
const codemods = await createCodeMods(fs, logger, app)

await presetAuth(codemods, { guard: 'session', userProvider: 'lucid' })
await assert.fileContains('adonisrc.ts', ['@adonisjs/auth/auth_provider'])
Expand All @@ -36,8 +37,9 @@ test.group('Preset | Auth', (group) => {
await createSetupFiles(fs)
await createKernelFile(fs)

const logger = cliui({ mode: 'normal' }).logger
const codemods = await createCodeMods(fs, logger)
const app = await createApp(fs)
const logger = new Kernel(app).ui.logger
const codemods = await createCodeMods(fs, logger, app)

await presetAuth(codemods, { guard: 'session', userProvider: 'lucid' })
await assert.fileEquals(
Expand Down Expand Up @@ -79,8 +81,9 @@ test.group('Preset | Auth', (group) => {
await createSetupFiles(fs)
await createKernelFile(fs)

const logger = cliui({ mode: 'normal' }).logger
const codemods = await createCodeMods(fs, logger)
const app = await createApp(fs)
const logger = new Kernel(app).ui.logger
const codemods = await createCodeMods(fs, logger, app)

await presetAuth(codemods, { guard: 'session', userProvider: 'database' })
await assert.fileEquals(
Expand Down
12 changes: 6 additions & 6 deletions tests/lucid/preset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import dedent from 'dedent'
import { test } from '@japa/runner'
import { cliui } from '@poppinss/cliui'
import { Kernel } from '@adonisjs/core/ace'
import { presetLucid } from '../../src/lucid/main.js'
import { createEnvFile, createSetupFiles, createCodeMods, createApp } from '../helpers.js'

Expand All @@ -20,8 +20,8 @@ test.group('Preset | Lucid', (group) => {
await createSetupFiles(fs)
await createEnvFile(fs)

const logger = cliui({ mode: 'normal' }).logger
const app = await createApp(fs)
const logger = new Kernel(app).ui.logger
const codemods = await createCodeMods(fs, logger, app)

await presetLucid(codemods, app, { dialect: 'postgres', installPackages: false })
Expand Down Expand Up @@ -75,8 +75,8 @@ test.group('Preset | Lucid', (group) => {
await createSetupFiles(fs)
await createEnvFile(fs)

const logger = cliui({ mode: 'normal' }).logger
const app = await createApp(fs)
const logger = new Kernel(app).ui.logger
const codemods = await createCodeMods(fs, logger, app)

await presetLucid(codemods, app, { dialect: 'mysql', installPackages: false })
Expand Down Expand Up @@ -130,8 +130,8 @@ test.group('Preset | Lucid', (group) => {
await createSetupFiles(fs)
await createEnvFile(fs)

const logger = cliui({ mode: 'normal' }).logger
const app = await createApp(fs)
const logger = new Kernel(app).ui.logger
const codemods = await createCodeMods(fs, logger, app)

await presetLucid(codemods, app, { dialect: 'mssql', installPackages: false })
Expand Down Expand Up @@ -185,8 +185,8 @@ test.group('Preset | Lucid', (group) => {
await createSetupFiles(fs)
await createEnvFile(fs)

const logger = cliui({ mode: 'normal' }).logger
const app = await createApp(fs)
const logger = new Kernel(app).ui.logger
const codemods = await createCodeMods(fs, logger, app)

await presetLucid(codemods, app, { dialect: 'sqlite', installPackages: false })
Expand Down Expand Up @@ -225,8 +225,8 @@ test.group('Preset | Lucid', (group) => {
await createSetupFiles(fs)
await createEnvFile(fs)

const logger = cliui({ mode: 'normal' }).logger
const app = await createApp(fs)
const logger = new Kernel(app).ui.logger
const codemods = await createCodeMods(fs, logger, app)

await presetLucid(codemods, app, { dialect: 'postgres', installPackages: true })
Expand Down

0 comments on commit bca7fce

Please sign in to comment.