Skip to content

Commit 6304c94

Browse files
committed
fix: dev hooks not executing
1 parent c11f9d2 commit 6304c94

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

src/dev_server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ export class DevServer {
300300
* Start the development server
301301
*/
302302
async start() {
303+
await this.#hooks.registerDevServerHooks()
304+
303305
this.#clearScreen()
304306
this.#logger.info('starting HTTP server...')
305307
this.#startHTTPServer(String(await getPort(this.#cwd)), 'nonblocking')
@@ -310,6 +312,8 @@ export class DevServer {
310312
* Start the development server in watch mode
311313
*/
312314
async startAndWatch(ts: typeof tsStatic, options?: { poll: boolean }) {
315+
await this.#hooks.registerDevServerHooks()
316+
313317
const port = String(await getPort(this.#cwd))
314318
this.#isWatching = true
315319

src/hooks.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) AdonisJS
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
110
import {
211
AssemblerHookNode,
312
SourceFileChangedHookHandler,

tests/dev_server.spec.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* @adonisjs/assembler
3+
*
4+
* (c) AdonisJS
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 ts from 'typescript'
11+
import { test } from '@japa/runner'
12+
import { DevServer } from '../index.js'
13+
import { setTimeout as sleep } from 'node:timers/promises'
14+
15+
test.group('DevServer', () => {
16+
test('start() execute onDevServerStarted hook', async ({ assert, fs }) => {
17+
assert.plan(1)
18+
await fs.create('bin/server.js', `process.send({ isAdonisJS: true, environment: 'web' })`)
19+
20+
const devServer = new DevServer(fs.baseUrl, {
21+
assets: {
22+
enabled: false,
23+
},
24+
nodeArgs: [],
25+
scriptArgs: [],
26+
hooks: {
27+
onDevServerStarted: [
28+
async () => ({
29+
default: () => {
30+
assert.isTrue(true)
31+
},
32+
}),
33+
],
34+
},
35+
})
36+
37+
await devServer.start()
38+
await sleep(600)
39+
})
40+
41+
test('startAndWatch() execute onDevServerStarted hook', async ({ assert, fs, cleanup }) => {
42+
assert.plan(1)
43+
await fs.create('bin/server.js', `process.send({ isAdonisJS: true, environment: 'web' })`)
44+
45+
const devServer = new DevServer(fs.baseUrl, {
46+
assets: {
47+
enabled: false,
48+
},
49+
nodeArgs: [],
50+
scriptArgs: [],
51+
hooks: {
52+
onDevServerStarted: [
53+
async () => ({
54+
default: () => {
55+
assert.isTrue(true)
56+
},
57+
}),
58+
],
59+
},
60+
})
61+
62+
await devServer.startAndWatch(ts)
63+
cleanup(() => devServer.close())
64+
await sleep(600)
65+
})
66+
67+
test('execute onSourceFileChanged hook', async ({ assert, fs, cleanup }) => {
68+
assert.plan(1)
69+
70+
await fs.createJson('tsconfig.json', {
71+
include: ['**/*'],
72+
exclude: [],
73+
})
74+
await fs.create('index.ts', 'console.log("hey")')
75+
await fs.create('bin/server.js', `process.send({ isAdonisJS: true, environment: 'web' })`)
76+
77+
const devServer = new DevServer(fs.baseUrl, {
78+
assets: {
79+
enabled: false,
80+
},
81+
nodeArgs: [],
82+
scriptArgs: [],
83+
hooks: {
84+
onSourceFileChanged: [
85+
async () => ({
86+
default: () => {
87+
assert.isTrue(true)
88+
},
89+
}),
90+
],
91+
},
92+
})
93+
94+
await devServer.startAndWatch(ts)
95+
cleanup(() => devServer.close())
96+
97+
await sleep(100)
98+
await fs.create('index.ts', 'sdf')
99+
await sleep(10)
100+
})
101+
})

0 commit comments

Comments
 (0)