registerMigrations registers the JS migrations loader.
()
| 181 | |
| 182 | // registerMigrations registers the JS migrations loader. |
| 183 | func (p *plugin) registerMigrations() error { |
| 184 | // fetch all js migrations sorted by their filename |
| 185 | files, err := filesContent(p.config.MigrationsDir, p.config.MigrationsFilesPattern) |
| 186 | if err != nil { |
| 187 | return err |
| 188 | } |
| 189 | |
| 190 | absHooksDir, err := filepath.Abs(p.config.HooksDir) |
| 191 | if err != nil { |
| 192 | return err |
| 193 | } |
| 194 | |
| 195 | registry := new(require.Registry) // this can be shared by multiple runtimes |
| 196 | templateRegistry := template.NewRegistry() |
| 197 | |
| 198 | for file, content := range files { |
| 199 | vm := goja.New() |
| 200 | |
| 201 | registry.Enable(vm) |
| 202 | console.Enable(vm) |
| 203 | process.Enable(vm) |
| 204 | buffer.Enable(vm) |
| 205 | |
| 206 | BindCore(vm) |
| 207 | BindDbx(vm) |
| 208 | BindSecurity(vm) |
| 209 | BindOS(vm) |
| 210 | BindFilepath(vm) |
| 211 | BindHTTP(vm) |
| 212 | BindFilesystem(vm) |
| 213 | BindForms(vm) |
| 214 | BindMails(vm) |
| 215 | |
| 216 | vm.Set("$template", templateRegistry) |
| 217 | vm.Set("__hooks", absHooksDir) |
| 218 | |
| 219 | vm.Set("migrate", func(up, down func(txApp core.App) error) { |
| 220 | core.AppMigrations.Register(up, down, file) |
| 221 | }) |
| 222 | |
| 223 | if p.config.OnInit != nil { |
| 224 | p.config.OnInit(vm) |
| 225 | } |
| 226 | |
| 227 | _, err := vm.RunScript(defaultScriptPath, string(content)) |
| 228 | if err != nil { |
| 229 | return fmt.Errorf("failed to run migration %s: %w", file, err) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | return nil |
| 234 | } |
| 235 | |
| 236 | // registerHooks registers the JS app hooks loader. |
| 237 | func (p *plugin) registerHooks() error { |
no test coverage detected