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: diff --git a/commands/make/router.ts b/commands/make/router.ts new file mode 100644 index 0000000..5abc477 --- /dev/null +++ b/commands/make/router.ts @@ -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() + } +} 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 diff --git a/stubs/make/router/main.stub b/stubs/make/router/main.stub new file mode 100644 index 0000000..6dca03c --- /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