From 1b2e87eeda3a6e5837f14ca1dfd110720e48adca Mon Sep 17 00:00:00 2001 From: tianjos Date: Fri, 18 Jul 2025 19:58:41 -0300 Subject: [PATCH 1/4] feat: make:router --- commands/make/router.ts | 41 +++++++++++++++++++++++++++++++++++++ stubs/make/router/main.stub | 9 ++++++++ 2 files changed, 50 insertions(+) create mode 100644 commands/make/router.ts create mode 100644 stubs/make/router/main.stub diff --git a/commands/make/router.ts b/commands/make/router.ts new file mode 100644 index 0000000..7c4c26c --- /dev/null +++ b/commands/make/router.ts @@ -0,0 +1,41 @@ +import { args, BaseCommand } from '@adonisjs/core/ace' +import stringHelpers from '@adonisjs/core/helpers/string' +import * as fs from 'node:fs' +import path from 'node:path' +import { fileURLToPath } from 'node:url' +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 (!fs.existsSync(modulePath)) { + createFile.skipped(`Module does not exists (at: ${this.colors.grey(modulePath)})`) + return + } + + const routerName = `${moduleName}Router` + const exportPath = this.app.makePath(modulePath, moduleName, 'router.ts') + + const codemods = await this.createCodemods() + await codemods.makeUsingStub(stubsRoot, this.stubPath, { + routerName, + moduleName, + exportPath + }) + } +} diff --git a/stubs/make/router/main.stub b/stubs/make/router/main.stub new file mode 100644 index 0000000..ea18cff --- /dev/null +++ b/stubs/make/router/main.stub @@ -0,0 +1,9 @@ +import router from '@adonisjs/core/services/router' + +{{{ + exports({ to: exportPath}) +}}} + +export const {{ routerName }} = router + .group(() => {}) + .as('{{ moduleName }}') \ No newline at end of file From 64769c036a369bec2fb4fd41fa119ddae6f662ad Mon Sep 17 00:00:00 2001 From: tianjos Date: Fri, 18 Jul 2025 20:04:46 -0300 Subject: [PATCH 2/4] fix: path --- commands/make/router.ts | 2 +- stubs/make/router/main.stub | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/commands/make/router.ts b/commands/make/router.ts index 7c4c26c..3d3a153 100644 --- a/commands/make/router.ts +++ b/commands/make/router.ts @@ -29,7 +29,7 @@ export default class MakeRouter extends BaseCommand { } const routerName = `${moduleName}Router` - const exportPath = this.app.makePath(modulePath, moduleName, 'router.ts') + const exportPath = this.app.makePath(modulePath, 'router.ts') const codemods = await this.createCodemods() await codemods.makeUsingStub(stubsRoot, this.stubPath, { diff --git a/stubs/make/router/main.stub b/stubs/make/router/main.stub index ea18cff..6dca03c 100644 --- a/stubs/make/router/main.stub +++ b/stubs/make/router/main.stub @@ -1,7 +1,7 @@ import router from '@adonisjs/core/services/router' {{{ - exports({ to: exportPath}) + exports({ to: exportPath }) }}} export const {{ routerName }} = router From 7fd5e398bd40fd233ac721e67d4c65f85149e859 Mon Sep 17 00:00:00 2001 From: tianjos Date: Fri, 18 Jul 2025 20:10:42 -0300 Subject: [PATCH 3/4] doc: update --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f016d9c..61bccc3 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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: From 7cba1a0afcb84d8e0b4998ab81f1f50d3e4009b4 Mon Sep 17 00:00:00 2001 From: tianjos Date: Thu, 24 Jul 2025 21:14:13 -0300 Subject: [PATCH 4/4] refact --- commands/make/router.ts | 16 +++++++++++----- src/utils.ts | 11 +++++++++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/commands/make/router.ts b/commands/make/router.ts index 3d3a153..5abc477 100644 --- a/commands/make/router.ts +++ b/commands/make/router.ts @@ -1,8 +1,8 @@ import { args, BaseCommand } from '@adonisjs/core/ace' import stringHelpers from '@adonisjs/core/helpers/string' -import * as fs from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' +import { checkModule, registerRouter } from '../../src/utils.js' import { stubsRoot } from '../../stubs/main.js' /** @@ -20,22 +20,28 @@ export default class MakeRouter extends BaseCommand { 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 (!fs.existsSync(modulePath)) { - createFile.skipped(`Module does not exists (at: ${this.colors.grey(modulePath)})`) + 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 + exportPath, }) + + const updateRcFile = this.logger.action('update adonisrc file') + await registerRouter(codemods, moduleName) + updateRcFile.succeeded() } } diff --git a/src/utils.ts b/src/utils.ts index 6b3cfdc..0f5aa09 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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) @@ -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`) + }) +} \ No newline at end of file