diff --git a/modules/ace/main.ts b/modules/ace/main.ts index 5b7569e9..bf47f16c 100644 --- a/modules/ace/main.ts +++ b/modules/ace/main.ts @@ -9,4 +9,13 @@ export { Kernel } from './kernel.js' export { BaseCommand, ListCommand } from './commands.js' -export { args, flags, errors, Parser, FsLoader, ListLoader, HelpCommand } from '@adonisjs/ace' +export { + args, + flags, + errors, + Parser, + FsLoader, + ListLoader, + HelpCommand, + IndexGenerator, +} from '@adonisjs/ace' diff --git a/package.json b/package.json index 0455608b..4a615770 100644 --- a/package.json +++ b/package.json @@ -11,12 +11,16 @@ "build/providers", "build/services", "build/factories", + "build/toolkit", "build/types", "build/src", "build/stubs", "build/index.d.ts", "build/index.js" ], + "bin": { + "toolkit": "./build/toolkit/main.js" + }, "exports": { ".": "./build/index.js", "./commands": "./build/commands/main.js", @@ -89,7 +93,7 @@ "format": "prettier --write .", "sync-labels": "github-label-sync --labels .github/labels.json adonisjs/core", "vscode:test": "node --loader=ts-node/esm --experimental-import-meta-resolve bin/test.ts", - "index:commands": "ace-toolkit index build/commands" + "index:commands": "node --loader=ts-node/esm toolkit/main.js index build/commands" }, "keywords": [ "adonisjs", diff --git a/toolkit/commands/index_commands.ts b/toolkit/commands/index_commands.ts new file mode 100644 index 00000000..90a55c5b --- /dev/null +++ b/toolkit/commands/index_commands.ts @@ -0,0 +1,27 @@ +/* + * @adonisjs/core + * + * (c) AdonisJS + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +import { join } from 'node:path' +import { args, BaseCommand, IndexGenerator } from '@adonisjs/ace' + +/** + * Generates index of commands with a loader. Must be called against + * the TypeScript compiled output. + */ +export default class IndexCommand extends BaseCommand { + static commandName = 'index' + static description: string = 'Create an index of commands along with a lazy loader' + + @args.string({ description: 'Relative path from cwd to the commands directory' }) + declare commandsDir: string + + async run(): Promise { + await new IndexGenerator(join(process.cwd(), this.commandsDir)).generate() + } +} diff --git a/toolkit/main.ts b/toolkit/main.ts new file mode 100644 index 00000000..53f7c312 --- /dev/null +++ b/toolkit/main.ts @@ -0,0 +1,33 @@ +#!/usr/bin/env node + +/* + * @adonisjs/core + * + * (c) AdonisJS + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +import IndexCommand from './commands/index_commands.js' +import { Kernel, ListLoader, HelpCommand } from '@adonisjs/ace' + +const kernel = Kernel.create() +kernel.addLoader(new ListLoader([IndexCommand])) + +kernel.defineFlag('help', { + type: 'boolean', + description: HelpCommand.description, +}) + +/** + * Flag listener to display the help + */ +kernel.on('help', async (command, $kernel, parsed) => { + parsed.args.unshift(command.commandName) + const help = new HelpCommand($kernel, parsed, kernel.ui, kernel.prompt) + await help.exec() + return $kernel.shortcircuit() +}) + +await kernel.handle(process.argv.splice(2))