Skip to content

Commit 7c9e6e4

Browse files
committed
feat: add MakeCommand for generating new console commands
1 parent 0f69a7a commit 7c9e6e4

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Command } from '@h3ravel/musket'
2+
import { resolve } from 'path'
3+
import { writeFile } from 'fs/promises'
4+
5+
export class MakeCommand extends Command {
6+
protected signature = `make:command
7+
{name : name of the command to create}
8+
`
9+
10+
protected description = 'Creates a new console command class.'
11+
12+
async handle () {
13+
const name = String(this.argument('name'))
14+
.replace(/\s+/g, '')
15+
.replace(/\.ts$/, '').trim()
16+
17+
if (!name) return void this.error('Command name is required')
18+
19+
const stubContent = this.stub(name)
20+
const filePath = resolve(process.cwd(), 'src', `app/console/commands/${name}.ts`)
21+
22+
await writeFile(filePath, stubContent, { flag: 'wx' })
23+
this.success(`Command ${name} created successfully at ${filePath}`)
24+
}
25+
26+
stub (name: string) {
27+
const signature = `app:${name.toLowerCase()}`
28+
const description = `Description for ${signature} command`
29+
30+
const stub = [
31+
'import { Command } from \'@h3ravel/musket\'',
32+
'',
33+
`export class ${name} extends Command {`,
34+
` signature = '${signature}'`,
35+
'',
36+
` description = '${description}'`,
37+
'',
38+
' async handle () {',
39+
' // Command logic goes here',
40+
' }',
41+
'}',
42+
]
43+
44+
return stub.join('\n')
45+
}
46+
}

packages/console/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ArkstackConsoleApp } from './app'
66
import { BuildCommand } from './commands/BuildCommand'
77
import { DevCommand } from './commands/DevCommand'
88
import { Kernel } from '@h3ravel/musket'
9+
import { MakeCommand } from './commands/MakeCommand'
910
import { MakeController } from './commands/MakeController'
1011
import { MakeFactoryCommand } from './commands/MakeFactoryCommand'
1112
import { MakeFullResource } from './commands/MakeFullResource'
@@ -66,8 +67,13 @@ export const runConsoleKernel = async (options: RunConsoleOptions = {}) => {
6667
MigrateCommand,
6768
ModelsSyncCommand,
6869
SeedCommand,
70+
MakeCommand
71+
],
72+
discoveryPaths: [
73+
join(process.cwd(), 'src', 'app/console/commands/*.ts'),
74+
join(process.cwd(), 'src', 'app/console/commands/*.js'),
75+
join(process.cwd(), 'src', 'app/console/commands/*.mjs'),
6976
],
70-
discoveryPaths: [join(process.cwd(), 'src/app/console/commands/*.ts')],
7177
exceptionHandler (exception) {
7278
throw exception
7379
},

0 commit comments

Comments
 (0)