Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ This allows you to organize your code in a modular way, making it easier to main
│ ├── auth
│ │ ├── controllers
│ │ ├── services
│ │ └── models
│ │ ├── models
| | └── router
│ └── users
│ ├── exceptions
│ ├── factories
│ └── validators
│ ├── validators
| ├── models
| └── router

```

Expand Down Expand Up @@ -47,6 +50,9 @@ This will create a new module named `auth` in the `/app` directory, as well as r
}
```

## Router Command (optional)
You could also create router by module with the `make:router` command. You must provide module name

## That's it !

You can now reuse the `@adonisjs/core` and `@adonisjs/lucid` `make` commands with flag `-m (--module)` to create controllers, models, views, etc., within your module. For example:
Expand Down
47 changes: 47 additions & 0 deletions commands/make/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { args, BaseCommand } from '@adonisjs/core/ace'
import stringHelpers from '@adonisjs/core/helpers/string'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { checkModule, registerRouter } from '../../src/utils.js'
import { stubsRoot } from '../../stubs/main.js'

/**
* The make router command to create a new router inside the given module
*/
export default class MakeRouter extends BaseCommand {
static description = 'Create a router inside the given module'
static commandName = 'make:router'

protected stubPath: string = 'make/router/main.stub'

@args.string({ description: 'Create a router inside the given module', })
declare module: string

async run() {
const moduleName = stringHelpers.snakeCase(this.module)
const modulePath = path.join(fileURLToPath(this.app.appRoot), 'app', moduleName)
const createFile = this.logger.action(`create router for module ${moduleName}`)

if (!checkModule(this.app, moduleName)) {
createFile.skipped(`Module doesn't exists (at: ${this.colors.grey(modulePath)})`)
return
}

createFile.succeeded()

const routerName = `${moduleName}Router`
const exportPath = this.app.makePath(modulePath, 'router.ts')

const codemods = await this.createCodemods()

await codemods.makeUsingStub(stubsRoot, this.stubPath, {
routerName,
moduleName,
exportPath,
})

const updateRcFile = this.logger.action('update adonisrc file')
await registerRouter(codemods, moduleName)
updateRcFile.succeeded()
}
}
11 changes: 9 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { Codemods } from '@adonisjs/core/ace/codemods'
import stringHelpers from '@adonisjs/core/helpers/string'
import { ApplicationService } from '@adonisjs/core/types'
import { readFileSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

export function checkModule(app: ApplicationService, value: string): boolean {
const moduleName = stringHelpers.snakeCase(value)
Expand Down Expand Up @@ -30,3 +31,9 @@ export function getPackageJson(app: ApplicationService): any {
const packageJsonPath = path.join(fileURLToPath(app.appRoot), 'package.json')
return JSON.parse(readFileSync(packageJsonPath, 'utf-8'))
}

export function registerRouter(codemods: Codemods, moduleName: string) {
return codemods.updateRcFile((rcFile) => {
rcFile.addPreloadFile(`#${moduleName}/router`)
})
}
9 changes: 9 additions & 0 deletions stubs/make/router/main.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import router from '@adonisjs/core/services/router'

{{{
exports({ to: exportPath })
}}}

export const {{ routerName }} = router
.group(() => {})
.as('{{ moduleName }}')