Skip to content

Commit

Permalink
fix: make:middleware to properly register nested middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 25, 2023
1 parent 4280f5f commit b2f62b2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
26 changes: 19 additions & 7 deletions commands/make/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import string from '@poppinss/utils/string'
import { basename, extname } from 'node:path'
import { basename, extname, relative } from 'node:path'

import { stubsRoot } from '../../stubs/main.js'
import { args, BaseCommand, flags } from '../../modules/ace/main.js'
Expand Down Expand Up @@ -61,21 +61,33 @@ export default class MakeMiddleware extends BaseCommand {
* Create middleware
*/
const codemods = await this.createCodemods()
const { relativeFileName } = await codemods.makeUsingStub(stubsRoot, this.stubPath, {
const { destination } = await codemods.makeUsingStub(stubsRoot, this.stubPath, {
flags: this.parsed.flags,
entity: this.app.generators.createEntity(this.name),
})

/**
* Creative relative path for the middleware file from
* the "./app/middleware" directory
*/
const middlewareRelativePath = relative(this.app.middlewarePath(), destination).replace(
extname(destination),
''
)

/**
* Take the middleware relative path, remove `_middleware` prefix from it
* and convert everything to camelcase
*/
const name = string.camelCase(basename(middlewareRelativePath).replace(/_middleware$/, ''))

/**
* Register middleware
*/
const middlewareFileName = basename(relativeFileName).replace(extname(relativeFileName), '')
const importPath = `#middleware/${middlewareFileName}`
const namedReference = string.camelCase(middlewareFileName.replace(/_middleware$/, ''))
await codemods.registerMiddleware(this.stack, [
{
name: namedReference,
path: importPath,
name: name,
path: `#middleware/${middlewareRelativePath}`,
},
])
}
Expand Down
36 changes: 36 additions & 0 deletions tests/commands/make_middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,42 @@ test.group('Make middleware', (group) => {
)
})

test('create nested middleware', async ({ assert, fs }) => {
const ace = await new AceFactory().make(fs.baseUrl, {
importer: (filePath) => import(filePath),
})
await ace.app.init()
ace.ui.switchMode('raw')

await fs.createJson('tsconfig.json', {})
await fs.create('start/kernel.ts', 'export const middleware = router.named({})')

const command = await ace.create(MakeMiddleware, ['blog/auth', '--stack=named'])
await command.exec()

const { contents } = await new StubsFactory().prepare('make/middleware/main.stub', {
entity: ace.app.generators.createEntity('auth'),
})

await assert.fileEquals('app/middleware/blog/auth_middleware.ts', contents)

assert.deepEqual(ace.ui.logger.getLogs(), [
{
message: 'green(DONE:) create app/middleware/blog/auth_middleware.ts',
stream: 'stdout',
},
{
message: 'green(DONE:) update start/kernel.ts file',
stream: 'stdout',
},
])

await assert.fileContains(
'start/kernel.ts',
`auth: () => import('#middleware/blog/auth_middleware')`
)
})

test('show error when selected middleware stack is invalid', async ({ assert, fs }) => {
const ace = await new AceFactory().make(fs.baseUrl, {
importer: (filePath) => import(filePath),
Expand Down

0 comments on commit b2f62b2

Please sign in to comment.