registerHooks registers the JS app hooks loader.
()
| 235 | |
| 236 | // registerHooks registers the JS app hooks loader. |
| 237 | func (p *plugin) registerHooks() error { |
| 238 | // fetch all js hooks sorted by their filename |
| 239 | files, err := filesContent(p.config.HooksDir, p.config.HooksFilesPattern) |
| 240 | if err != nil { |
| 241 | return err |
| 242 | } |
| 243 | |
| 244 | // prepend the types reference directive |
| 245 | // |
| 246 | // note: it is loaded during startup to handle conveniently also |
| 247 | // the case when the HooksWatch option is enabled and the application |
| 248 | // restart on newly created file |
| 249 | for name, content := range files { |
| 250 | if len(content) != 0 { |
| 251 | // skip non-empty files for now to prevent accidental overwrite |
| 252 | continue |
| 253 | } |
| 254 | path := filepath.Join(p.config.HooksDir, name) |
| 255 | directive := `/// <reference path="` + p.relativeTypesPath(p.config.HooksDir) + `" />` |
| 256 | if err := prependToEmptyFile(path, directive+"\n\n"); err != nil { |
| 257 | color.Yellow("Unable to prepend the types reference: %v", err) |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // initialize the hooks dir watcher |
| 262 | if p.config.HooksWatch { |
| 263 | if err := p.watchHooks(); err != nil { |
| 264 | color.Yellow("Unable to init hooks watcher: %v", err) |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if len(files) == 0 { |
| 269 | // no need to register the vms since there are no entrypoint files anyway |
| 270 | return nil |
| 271 | } |
| 272 | |
| 273 | absHooksDir, err := filepath.Abs(p.config.HooksDir) |
| 274 | if err != nil { |
| 275 | return err |
| 276 | } |
| 277 | |
| 278 | p.app.OnServe().BindFunc(func(e *core.ServeEvent) error { |
| 279 | e.Router.BindFunc(p.normalizeServeExceptions) |
| 280 | |
| 281 | return e.Next() |
| 282 | }) |
| 283 | |
| 284 | // safe to be shared across multiple vms |
| 285 | requireRegistry := new(require.Registry) |
| 286 | templateRegistry := template.NewRegistry() |
| 287 | |
| 288 | sharedBinds := func(vm *goja.Runtime) { |
| 289 | requireRegistry.Enable(vm) |
| 290 | console.Enable(vm) |
| 291 | process.Enable(vm) |
| 292 | buffer.Enable(vm) |
| 293 | |
| 294 | BindCore(vm) |
no test coverage detected