1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-11-21 13:35:49 +02:00

added timestamp to the generated JSVM types file to prevent creating it every time on app startup

This commit is contained in:
Gani Georgiev 2023-12-16 23:20:38 +02:00
parent 0b4f3b2adf
commit d6569b445c
5 changed files with 4518 additions and 4496 deletions

View File

@ -15,6 +15,8 @@
A negative or zero value means no tests timeout. A negative or zero value means no tests timeout.
If a single API test takes more than 3s to complete it will have a log message visible when the test fails or when `go test -v` flag is used. If a single API test takes more than 3s to complete it will have a log message visible when the test fails or when `go test -v` flag is used.
- Added timestamp at the beginning of the generated JSVM types file to avoid creating it everytime with the app startup.
## v0.20.0 ## v0.20.0

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,14 @@
package main package main
import ( import (
"fmt"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"runtime" "runtime"
"strings" "strings"
"time"
"github.com/pocketbase/pocketbase/core" "github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/plugins/jsvm" "github.com/pocketbase/pocketbase/plugins/jsvm"
@ -1095,6 +1097,10 @@ func main() {
result = strings.ReplaceAll(result, "ORIGINAL_CORE_APP", "core.App") result = strings.ReplaceAll(result, "ORIGINAL_CORE_APP", "core.App")
result = strings.ReplaceAll(result, "ORIGINAL_POCKETBASE", "pocketbase.PocketBase") result = strings.ReplaceAll(result, "ORIGINAL_POCKETBASE", "pocketbase.PocketBase")
// prepend a timestamp with the generation time
// so that it can be compared without reading the entire file
result = fmt.Sprintf("// %d\n%s", time.Now().Unix(), result)
parentDir := filepath.Dir(filename) parentDir := filepath.Dir(filename)
typesFile := filepath.Join(parentDir, "generated", "types.d.ts") typesFile := filepath.Join(parentDir, "generated", "types.d.ts")

View File

@ -9,8 +9,10 @@
package jsvm package jsvm
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"io"
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
@ -77,6 +79,9 @@ type Config struct {
// TypeScript declarations file. // TypeScript declarations file.
// //
// If not set it fallbacks to "pb_data". // If not set it fallbacks to "pb_data".
//
// Note: Avoid using the same directory as the HooksDir when HooksWatch is enabled
// to prevent unnecessary app restarts when the types file is initially created.
TypesDir string TypesDir string
} }
@ -117,10 +122,9 @@ func Register(app core.App, config Config) error {
} }
p.app.OnAfterBootstrap().Add(func(e *core.BootstrapEvent) error { p.app.OnAfterBootstrap().Add(func(e *core.BootstrapEvent) error {
// always update the app types on start to ensure that // ensure that the user has the latest types declaration
// the user has the latest generated declarations if err := p.refreshTypesFile(); err != nil {
if err := p.saveTypesFile(); err != nil { color.Yellow("Unable to refresh app types file: %v", err)
color.Yellow("Unable to save app types file: %v", err)
} }
return nil return nil
@ -423,8 +427,8 @@ func (p *plugin) relativeTypesPath(basepath string) string {
return rel return rel
} }
// saveTypesFile saves the embedded TS declarations as a file on the disk. // refreshTypesFile saves the embedded TS declarations as a file on the disk.
func (p *plugin) saveTypesFile() error { func (p *plugin) refreshTypesFile() error {
fullPath := p.fullTypesPath() fullPath := p.fullTypesPath()
// ensure that the types directory exists // ensure that the types directory exists
@ -439,11 +443,20 @@ func (p *plugin) saveTypesFile() error {
return err return err
} }
if err := os.WriteFile(fullPath, data, 0644); err != nil { // read the first timestamp line of the old file (if exists) and compare it to the embedded one
return err // (note: ignore errors to allow always overwriting the file if it is invalid)
existingFile, err := os.Open(fullPath)
if err == nil {
timestamp := make([]byte, 13)
io.ReadFull(existingFile, timestamp)
existingFile.Close()
if len(data) >= len(timestamp) && bytes.Equal(data[:13], timestamp) {
return nil // nothing new to save
}
} }
return nil return os.WriteFile(fullPath, data, 0644)
} }
// prependToEmptyFile prepends the specified text to an empty file. // prependToEmptyFile prepends the specified text to an empty file.

View File

@ -203,7 +203,7 @@ func (pb *PocketBase) eagerParseFlags(config *Config) error {
&pb.devFlag, &pb.devFlag,
"dev", "dev",
config.DefaultDev, config.DefaultDev,
"enable dev mode, aka. printing logs and sql statements", "enable dev mode, aka. printing logs and sql statements to the console",
) )
return pb.RootCmd.ParseFlags(os.Args[1:]) return pb.RootCmd.ParseFlags(os.Args[1:])