Skip to content

Commit 2cfe1a2

Browse files
committed
feat: introduce hooks helpers
1 parent 5fa0566 commit 2cfe1a2

File tree

9 files changed

+349
-95
lines changed

9 files changed

+349
-95
lines changed

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* file that was distributed with this source code.
88
*/
99

10+
export { hooks } from './src/hooks.ts'
1011
export { Bundler } from './src/bundler.ts'
1112
export { DevServer } from './src/dev_server.ts'
1213
export { TestRunner } from './src/test_runner.ts'

src/bundler.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ import type Hooks from '@poppinss/hooks'
1616
import { join, relative } from 'node:path'
1717
import string from '@poppinss/utils/string'
1818
import { detectPackageManager } from '@antfu/install-pkg'
19-
import { type UnWrapLazyImport } from '@poppinss/utils/types'
2019

21-
import { type BundlerHooks } from './types/hooks.ts'
2220
import type { BundlerOptions } from './types/common.ts'
2321
import { run, parseConfig, copyFiles, loadHooks } from './utils.ts'
22+
import { type HookParams, type BundlerHooks } from './types/hooks.ts'
2423
import { type SupportedPackageManager } from './types/code_transformer.ts'
2524

2625
/**
@@ -77,10 +76,7 @@ export class Bundler {
7776
* Hooks to execute custom actions during the build process
7877
*/
7978
#hooks!: Hooks<{
80-
[K in keyof BundlerHooks]: [
81-
Parameters<UnWrapLazyImport<BundlerHooks[K][number]>>,
82-
Parameters<UnWrapLazyImport<BundlerHooks[K][number]>>,
83-
]
79+
[K in keyof BundlerHooks]: [HookParams<K>, HookParams<K>]
8480
}>
8581

8682
/**

src/dev_server.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { type ResultPromise } from 'execa'
1717
import string from '@poppinss/utils/string'
1818
import { join, relative } from 'node:path/posix'
1919
import { RuntimeException } from '@poppinss/utils/exception'
20-
import { type UnWrapLazyImport } from '@poppinss/utils/types'
2120

2221
import debug from './debug.ts'
2322
import { FileSystem } from './file_system.ts'
@@ -26,6 +25,7 @@ import type { DevServerOptions } from './types/common.ts'
2625
import { IndexGenerator } from './index_generator/main.ts'
2726
import { getPort, loadHooks, parseConfig, runNode, throttle, watch } from './utils.ts'
2827
import {
28+
type HookParams,
2929
type RouterHooks,
3030
type CommonHooks,
3131
type WatcherHooks,
@@ -137,25 +137,13 @@ export class DevServer {
137137
*/
138138
#hooks!: Hooks<
139139
{
140-
[K in keyof CommonHooks]: [
141-
Parameters<UnWrapLazyImport<CommonHooks[K][number]>>,
142-
Parameters<UnWrapLazyImport<CommonHooks[K][number]>>,
143-
]
140+
[K in keyof CommonHooks]: [HookParams<K>, HookParams<K>]
144141
} & {
145-
[K in keyof RouterHooks]: [
146-
Parameters<UnWrapLazyImport<RouterHooks[K][number]>>,
147-
Parameters<UnWrapLazyImport<RouterHooks[K][number]>>,
148-
]
142+
[K in keyof RouterHooks]: [HookParams<K>, HookParams<K>]
149143
} & {
150-
[K in keyof DevServerHooks]: [
151-
Parameters<UnWrapLazyImport<DevServerHooks[K][number]>>,
152-
Parameters<UnWrapLazyImport<DevServerHooks[K][number]>>,
153-
]
144+
[K in keyof DevServerHooks]: [HookParams<K>, HookParams<K>]
154145
} & {
155-
[K in keyof WatcherHooks]: [
156-
Parameters<UnWrapLazyImport<WatcherHooks[K][number]>>,
157-
Parameters<UnWrapLazyImport<WatcherHooks[K][number]>>,
158-
]
146+
[K in keyof WatcherHooks]: [HookParams<K>, HookParams<K>]
159147
}
160148
>
161149

src/hooks.ts

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
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 { type AsyncOrSync } from '@poppinss/utils/types'
11+
import { CommonHooks, type HookParams } from './types/hooks.ts'
12+
13+
/**
14+
* Collection of hooks that can be used to listen for various events during
15+
* the application lifecycle. These hooks allow you to execute custom logic
16+
* at specific points in the development server, build process, and testing.
17+
*
18+
* @example
19+
* ```js
20+
* const { hooks } = await import('@adonisjs/assembler/hooks')
21+
*
22+
* hooks.init((app) => {
23+
* console.log('Application initialized')
24+
* })
25+
*
26+
* hooks.devServerStarted((server) => {
27+
* console.log('Dev server started on port', server.port)
28+
* })
29+
* ```
30+
*/
31+
export const hooks = {
32+
/**
33+
* Hook called during application initialization. This is the first hook
34+
* that gets executed when the assembler starts up.
35+
*
36+
* @param callback - Function to execute when the init event occurs
37+
*
38+
* @example
39+
* ```js
40+
* hooks.init((app) => {
41+
* console.log('Application is initializing')
42+
* // Setup global configurations
43+
* })
44+
* ```
45+
*/
46+
init(callback: (...args: HookParams<'init'>) => AsyncOrSync<void>) {
47+
return callback
48+
},
49+
/**
50+
* Hook called after routes have been committed to the router.
51+
* This occurs after all route definitions have been processed.
52+
*
53+
* @param callback - Function to execute when routes are committed
54+
*
55+
* @example
56+
* ```js
57+
* hooks.routesCommitted((router) => {
58+
* console.log('All routes have been committed to the router')
59+
* // Perform route-based setup
60+
* })
61+
* ```
62+
*/
63+
routesCommitted(callback: (...args: HookParams<'routesCommitted'>) => AsyncOrSync<void>) {
64+
return callback
65+
},
66+
/**
67+
* Hook called when the assembler starts scanning for route files.
68+
* This happens before any route files are actually processed.
69+
*
70+
* @param callback - Function to execute when route scanning begins
71+
*
72+
* @example
73+
* ```js
74+
* hooks.routesScanning(() => {
75+
* console.log('Starting to scan for route files')
76+
* // Setup route scanning configurations
77+
* })
78+
* ```
79+
*/
80+
routesScanning(callback: (...args: HookParams<'routesScanning'>) => AsyncOrSync<void>) {
81+
return callback
82+
},
83+
/**
84+
* Hook called after all route files have been scanned and processed.
85+
* This occurs once the route scanning phase is complete.
86+
*
87+
* @param callback - Function to execute when route scanning is finished
88+
*
89+
* @example
90+
* ```js
91+
* hooks.routesScanned((scannedRoutes) => {
92+
* console.log('Route scanning completed')
93+
* // Process scanned route information
94+
* })
95+
* ```
96+
*/
97+
routesScanned(callback: (...args: HookParams<'routesScanned'>) => AsyncOrSync<void>) {
98+
return callback
99+
},
100+
/**
101+
* Hook called when a file is modified during development.
102+
* This is triggered by the file watcher when changes are detected.
103+
*
104+
* @param callback - Function to execute when a file changes
105+
*
106+
* @example
107+
* ```js
108+
* hooks.fileChanged((filePath, stats) => {
109+
* console.log(`File changed: ${filePath}`)
110+
* // Handle file change logic
111+
* })
112+
* ```
113+
*/
114+
fileChanged(callback: (...args: HookParams<'fileChanged'>) => AsyncOrSync<void>) {
115+
return callback
116+
},
117+
/**
118+
* Hook called when a new file is added during development.
119+
* This is triggered by the file watcher when new files are created.
120+
*
121+
* @param callback - Function to execute when a file is added
122+
*
123+
* @example
124+
* ```js
125+
* hooks.fileAdded((filePath, stats) => {
126+
* console.log(`New file added: ${filePath}`)
127+
* // Handle new file logic
128+
* })
129+
* ```
130+
*/
131+
fileAdded(callback: (...args: HookParams<'fileAdded'>) => AsyncOrSync<void>) {
132+
return callback
133+
},
134+
/**
135+
* Hook called when a file is removed during development.
136+
* This is triggered by the file watcher when files are deleted.
137+
*
138+
* @param callback - Function to execute when a file is removed
139+
*
140+
* @example
141+
* ```js
142+
* hooks.fileRemoved((filePath) => {
143+
* console.log(`File removed: ${filePath}`)
144+
* // Handle file removal logic
145+
* })
146+
* ```
147+
*/
148+
fileRemoved(callback: (...args: HookParams<'fileRemoved'>) => AsyncOrSync<void>) {
149+
return callback
150+
},
151+
/**
152+
* Hook called when the development server is about to start.
153+
* This occurs before the server begins listening for connections.
154+
*
155+
* @param callback - Function to execute when dev server is starting
156+
*
157+
* @example
158+
* ```js
159+
* hooks.devServerStarting((server) => {
160+
* console.log('Development server is starting')
161+
* // Setup server configurations
162+
* })
163+
* ```
164+
*/
165+
devServerStarting(callback: (...args: HookParams<'devServerStarting'>) => AsyncOrSync<void>) {
166+
return callback
167+
},
168+
/**
169+
* Hook called after the development server has successfully started.
170+
* This occurs once the server is listening and ready to accept requests.
171+
*
172+
* @param callback - Function to execute when dev server has started
173+
*
174+
* @example
175+
* ```js
176+
* hooks.devServerStarted((server) => {
177+
* console.log(`Development server started on port ${server.port}`)
178+
* // Notify external services or open browser
179+
* })
180+
* ```
181+
*/
182+
devServerStarted(callback: (...args: HookParams<'devServerStarted'>) => AsyncOrSync<void>) {
183+
return callback
184+
},
185+
/**
186+
* Hook called when the build process is about to start.
187+
* This occurs before any build tasks are executed.
188+
*
189+
* @param callback - Function to execute when build is starting
190+
*
191+
* @example
192+
* ```js
193+
* hooks.buildStarting((buildConfig) => {
194+
* console.log('Build process is starting')
195+
* // Setup build configurations or clean directories
196+
* })
197+
* ```
198+
*/
199+
buildStarting(callback: (...args: HookParams<'buildStarting'>) => AsyncOrSync<void>) {
200+
return callback
201+
},
202+
/**
203+
* Hook called after the build process has completed.
204+
* This occurs once all build tasks have finished executing.
205+
*
206+
* @param callback - Function to execute when build is finished
207+
*
208+
* @example
209+
* ```js
210+
* hooks.buildFinished((buildResult) => {
211+
* console.log('Build process completed')
212+
* // Deploy artifacts or notify build completion
213+
* })
214+
* ```
215+
*/
216+
buildFinished(callback: (...args: HookParams<'buildFinished'>) => AsyncOrSync<void>) {
217+
return callback
218+
},
219+
/**
220+
* Hook called when the test suite is about to start.
221+
* This occurs before any test files are executed.
222+
*
223+
* @param callback - Function to execute when tests are starting
224+
*
225+
* @example
226+
* ```js
227+
* hooks.testsStarting((testConfig) => {
228+
* console.log('Test suite is starting')
229+
* // Setup test database or mock services
230+
* })
231+
* ```
232+
*/
233+
testsStarting(callback: (...args: HookParams<'testsStarting'>) => AsyncOrSync<void>) {
234+
return callback
235+
},
236+
/**
237+
* Hook called after the test suite has completed.
238+
* This occurs once all test files have finished executing.
239+
*
240+
* @param callback - Function to execute when tests are finished
241+
*
242+
* @example
243+
* ```js
244+
* hooks.testsFinished((testResults) => {
245+
* console.log('Test suite completed')
246+
* // Generate test reports or cleanup test resources
247+
* })
248+
* ```
249+
*/
250+
testsFinished(callback: (...args: HookParams<'testsFinished'>) => AsyncOrSync<void>) {
251+
return callback
252+
},
253+
}

src/test_runner.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ import { type FSWatcher } from 'chokidar'
1616
import { type ResultPromise } from 'execa'
1717
import string from '@poppinss/utils/string'
1818
import { RuntimeException } from '@poppinss/utils/exception'
19-
import { type UnWrapLazyImport } from '@poppinss/utils/types'
2019

2120
import debug from './debug.ts'
2221
import { FileSystem } from './file_system.ts'
2322
import type { TestRunnerOptions } from './types/common.ts'
2423
import { IndexGenerator } from './index_generator/main.ts'
2524
import { getPort, loadHooks, parseConfig, runNode, throttle, watch } from './utils.ts'
26-
import { type WatcherHooks, type TestRunnerHooks, type CommonHooks } from './types/hooks.ts'
25+
import {
26+
type HookParams,
27+
type CommonHooks,
28+
type WatcherHooks,
29+
type TestRunnerHooks,
30+
} from './types/hooks.ts'
2731

2832
/**
2933
* Exposes the API to run Japa tests and optionally watch for file
@@ -83,20 +87,11 @@ export class TestRunner {
8387
*/
8488
#hooks!: Hooks<
8589
{
86-
[K in keyof CommonHooks]: [
87-
Parameters<UnWrapLazyImport<CommonHooks[K][number]>>,
88-
Parameters<UnWrapLazyImport<CommonHooks[K][number]>>,
89-
]
90+
[K in keyof CommonHooks]: [HookParams<K>, HookParams<K>]
9091
} & {
91-
[K in keyof TestRunnerHooks]: [
92-
Parameters<UnWrapLazyImport<TestRunnerHooks[K][number]>>,
93-
Parameters<UnWrapLazyImport<TestRunnerHooks[K][number]>>,
94-
]
92+
[K in keyof TestRunnerHooks]: [HookParams<K>, HookParams<K>]
9593
} & {
96-
[K in keyof WatcherHooks]: [
97-
Parameters<UnWrapLazyImport<WatcherHooks[K][number]>>,
98-
Parameters<UnWrapLazyImport<WatcherHooks[K][number]>>,
99-
]
94+
[K in keyof WatcherHooks]: [HookParams<K>, HookParams<K>]
10095
}
10196
>
10297

0 commit comments

Comments
 (0)