Skip to content

Commit 5a71f46

Browse files
committed
feat: add make:listener command
1 parent 59fe855 commit 5a71f46

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

commands/Make/Listener.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) Harminder Virk <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import { join } from 'path'
11+
import { args } from '@adonisjs/ace'
12+
import { RcFile } from '@ioc:Adonis/Core/Application'
13+
import { BaseGenerator } from './Base'
14+
15+
/**
16+
* Command to make a new event listener class
17+
*/
18+
export default class MakeListener extends BaseGenerator {
19+
/**
20+
* Required by BaseGenerator
21+
*/
22+
protected $form = 'singular' as const
23+
protected $pattern = 'pascalcase' as const
24+
protected $resourceName: string
25+
26+
/**
27+
* Command meta data
28+
*/
29+
public static commandName = 'make:listener'
30+
public static description = 'Make a new event listener class'
31+
32+
@args.string({ description: 'Name of the event listener class' })
33+
public name: string
34+
35+
/**
36+
* Returns the template stub
37+
*/
38+
protected $getStub (): string {
39+
return join(
40+
__dirname,
41+
'..',
42+
'..',
43+
'templates',
44+
'event-listener.txt',
45+
)
46+
}
47+
48+
/**
49+
* Pull path from the `listeners` directory declaration from
50+
* the `.adonisrc.json` file or fallback to `app/Listeners`
51+
*/
52+
protected $getDestinationPath (rcContents: RcFile): string {
53+
return this.$getPathForNamespace(rcContents, 'eventListeners') || 'app/Listeners'
54+
}
55+
56+
public async handle () {
57+
this.$resourceName = this.name
58+
await super.generate()
59+
}
60+
}

templates/event-listener.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export default class ${filename} {
2+
}

test/make-listener.spec.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) Harminder Virk <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
import test from 'japa'
11+
import { join } from 'path'
12+
import { Ioc } from '@adonisjs/fold'
13+
import { Kernel } from '@adonisjs/ace'
14+
import { Filesystem } from '@poppinss/dev-utils'
15+
import { Application } from '@adonisjs/application/build/standalone'
16+
17+
import { toNewlineArray } from '../test-helpers'
18+
import MakeListener from '../commands/Make/Listener'
19+
20+
const fs = new Filesystem(join(__dirname, '__app'))
21+
const templates = new Filesystem(join(__dirname, '..', 'templates'))
22+
23+
test.group('Make Listener', (group) => {
24+
group.before(() => {
25+
process.env.ADONIS_ACE_CWD = fs.basePath
26+
})
27+
28+
group.after(() => {
29+
delete process.env.ADONIS_ACE_CWD
30+
})
31+
32+
group.afterEach(async () => {
33+
await fs.cleanup()
34+
})
35+
36+
test('make a listener inside the default directory', async (assert) => {
37+
await fs.add('.adonisrc.json', JSON.stringify({}))
38+
39+
const app = new Application(fs.basePath, new Ioc(), {}, {})
40+
41+
const listener = new MakeListener(app, new Kernel(app))
42+
listener.name = 'user'
43+
await listener.handle()
44+
45+
const UserListener = await fs.get('app/Listeners/User.ts')
46+
const ListenerTemplate = await templates.get('event-listener.txt')
47+
assert.deepEqual(
48+
toNewlineArray(UserListener),
49+
toNewlineArray(ListenerTemplate.replace('${filename}', 'User')),
50+
)
51+
})
52+
53+
test('make a listener inside a custom directory', async (assert) => {
54+
await fs.add('.adonisrc.json', JSON.stringify({
55+
namespaces: {
56+
eventListeners: 'App/Events/Listeners',
57+
},
58+
aliases: {
59+
App: './app'
60+
}
61+
}))
62+
63+
const app = new Application(fs.basePath, new Ioc(), {}, {})
64+
65+
const listener = new MakeListener(app, new Kernel(app))
66+
listener.name = 'user'
67+
await listener.handle()
68+
69+
const UserListener = await fs.get('app/Events/Listeners/User.ts')
70+
const ListenerTemplate = await templates.get('event-listener.txt')
71+
assert.deepEqual(
72+
toNewlineArray(UserListener),
73+
toNewlineArray(ListenerTemplate.replace('${filename}', 'User')),
74+
)
75+
})
76+
})

0 commit comments

Comments
 (0)