Skip to content

Commit 09440c3

Browse files
committed
fix: command template
1 parent 6482d66 commit 09440c3

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

commands/Make/Command.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
import { join } from 'path'
11+
import snakeCase from 'snake-case'
1112
import { args } from '@adonisjs/ace'
1213
import { RcFile } from '@ioc:Adonis/Core/Application'
1314
import { BaseGenerator } from './Base'
@@ -52,6 +53,17 @@ export default class MakeCommand extends BaseGenerator {
5253
return rcFile.directories.commands || 'commands'
5354
}
5455

56+
/**
57+
* Passed down to the template.
58+
*/
59+
protected $templateData () {
60+
return {
61+
toCommandName: (filename: string) => {
62+
return snakeCase(filename).replace(/_/, ':')
63+
},
64+
}
65+
}
66+
5567
public async handle () {
5668
this.$resourceName = this.name
5769
await super.handle()

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"fs-extra": "^8.1.0",
101101
"mem": "^6.0.0",
102102
"picomatch": "^2.1.1",
103-
"slash": "^3.0.0"
103+
"slash": "^3.0.0",
104+
"snake-case": "^2.1.0"
104105
}
105106
}

templates/command.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BaseCommand } from '@adonisjs/ace'
22

3-
export abstract class {filename} extends BaseCommand {
4-
public static commandName = ''
3+
export default class ${filename} extends BaseCommand {
4+
public static commandName = '${toCommandName(filename)}'
55
public static description = ''
66

77
public async handle () {

test/make-command.spec.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ test.group('Make Command', (group) => {
4545
const CommandTemplate = await templates.get('command.txt')
4646
assert.deepEqual(
4747
toNewlineArray(GreetCommand),
48-
toNewlineArray(CommandTemplate.replace('${filename}', 'Greet')),
48+
toNewlineArray(
49+
CommandTemplate
50+
.replace('${filename}', 'Greet')
51+
.replace('${toCommandName(filename)}', 'greet'),
52+
),
4953
)
5054
})
5155

@@ -66,7 +70,36 @@ test.group('Make Command', (group) => {
6670
const CommandTemplate = await templates.get('command.txt')
6771
assert.deepEqual(
6872
toNewlineArray(GreetCommand),
69-
toNewlineArray(CommandTemplate.replace('${filename}', 'Greet')),
73+
toNewlineArray(
74+
CommandTemplate
75+
.replace('${filename}', 'Greet')
76+
.replace('${toCommandName(filename)}', 'greet'),
77+
),
78+
)
79+
})
80+
81+
test('convert camelcase command path to colon seperated name', async (assert) => {
82+
await fs.add('.adonisrc.json', JSON.stringify({
83+
directories: {
84+
commands: './foo',
85+
},
86+
}))
87+
88+
const app = new Application(fs.basePath, new Ioc(), {}, {})
89+
90+
const command = new MakeCommand(app)
91+
command.name = 'RunInstructions'
92+
await command.handle()
93+
94+
const GreetCommand = await fs.get('foo/RunInstructions.ts')
95+
const CommandTemplate = await templates.get('command.txt')
96+
assert.deepEqual(
97+
toNewlineArray(GreetCommand),
98+
toNewlineArray(
99+
CommandTemplate
100+
.replace('${filename}', 'RunInstructions')
101+
.replace('${toCommandName(filename)}', 'run:instructions'),
102+
),
70103
)
71104
})
72105
})

0 commit comments

Comments
 (0)