|
| 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 | +} |
0 commit comments