|
| 1 | +import { |
| 2 | + AssemblerHookNode, |
| 3 | + SourceFileChangedHookHandler, |
| 4 | + AssemblerHookHandler, |
| 5 | + RcFile, |
| 6 | +} from '@adonisjs/application/types' |
| 7 | +import { RuntimeException } from '@poppinss/utils' |
| 8 | +import Hooks from '@poppinss/hooks' |
| 9 | + |
| 10 | +export class AssemblerHooks { |
| 11 | + #config: RcFile['unstable_assembler'] |
| 12 | + |
| 13 | + #hooks = new Hooks<{ |
| 14 | + onDevServerStarted: [Parameters<AssemblerHookHandler>, []] |
| 15 | + onSourceFileChanged: [Parameters<SourceFileChangedHookHandler>, []] |
| 16 | + onBuildStarting: [Parameters<AssemblerHookHandler>, []] |
| 17 | + onBuildCompleted: [Parameters<AssemblerHookHandler>, []] |
| 18 | + }>() |
| 19 | + |
| 20 | + constructor(config: RcFile['unstable_assembler']) { |
| 21 | + this.#config = config |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Resolve the hook by importing the file and returning the default export |
| 26 | + */ |
| 27 | + async #resolveHookNode(node: AssemblerHookNode<any>) { |
| 28 | + const exports = await node() |
| 29 | + |
| 30 | + if (!exports.default) { |
| 31 | + throw new RuntimeException('Assembler hook must be defined using the default export') |
| 32 | + } |
| 33 | + |
| 34 | + return exports.default |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Resolve hooks needed for dev-time and register them to the Hooks instance |
| 39 | + */ |
| 40 | + async registerDevServerHooks() { |
| 41 | + await Promise.all([ |
| 42 | + this.#config?.onDevServerStarted?.map(async (node) => |
| 43 | + this.#hooks.add('onDevServerStarted', await this.#resolveHookNode(node)) |
| 44 | + ), |
| 45 | + this.#config?.onSourceFileChanged?.map(async (node) => |
| 46 | + this.#hooks.add('onSourceFileChanged', await this.#resolveHookNode(node)) |
| 47 | + ), |
| 48 | + ]) |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Resolve hooks needed for build-time and register them to the Hooks instance |
| 53 | + */ |
| 54 | + async registerBuildHooks() { |
| 55 | + await Promise.all([ |
| 56 | + this.#config?.onBuildStarting?.map(async (node) => |
| 57 | + this.#hooks.add('onBuildStarting', await this.#resolveHookNode(node)) |
| 58 | + ), |
| 59 | + this.#config?.onBuildCompleted?.map(async (node) => |
| 60 | + this.#hooks.add('onBuildCompleted', await this.#resolveHookNode(node)) |
| 61 | + ), |
| 62 | + ]) |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * When the dev server is started |
| 67 | + */ |
| 68 | + async onDevServerStarted(...args: Parameters<AssemblerHookHandler>) { |
| 69 | + await this.#hooks.runner('onDevServerStarted').run(...args) |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * When a source file changes |
| 74 | + */ |
| 75 | + async onSourceFileChanged(...args: Parameters<SourceFileChangedHookHandler>) { |
| 76 | + await this.#hooks.runner('onSourceFileChanged').run(...args) |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * When the build process is starting |
| 81 | + */ |
| 82 | + async onBuildStarting(...args: Parameters<AssemblerHookHandler>) { |
| 83 | + await this.#hooks.runner('onBuildStarting').run(...args) |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * When the build process is completed |
| 88 | + */ |
| 89 | + async onBuildCompleted(...args: Parameters<AssemblerHookHandler>) { |
| 90 | + await this.#hooks.runner('onBuildCompleted').run(...args) |
| 91 | + } |
| 92 | +} |
0 commit comments