fix(codegen): ignore transient invalid watch input
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
aa282056fa
commit
aa00f27db4
2 changed files with 42 additions and 5 deletions
|
|
@ -81,6 +81,7 @@ func runDaemon(ctx context.Context, inputPath, outputPath string, emitTypes bool
|
|||
}
|
||||
|
||||
var lastInput string
|
||||
var lastOutput string
|
||||
for {
|
||||
input, err := coreio.Local.Read(inputPath)
|
||||
if err != nil {
|
||||
|
|
@ -90,12 +91,18 @@ func runDaemon(ctx context.Context, inputPath, outputPath string, emitTypes bool
|
|||
if input != lastInput {
|
||||
out, err := generate([]byte(input), emitTypes)
|
||||
if err != nil {
|
||||
return err
|
||||
// Watch mode should keep running through transient bad edits.
|
||||
log.Error("codegen watch skipped invalid input", "err", err)
|
||||
lastInput = input
|
||||
} else {
|
||||
if out != lastOutput {
|
||||
if err := coreio.Local.Write(outputPath, out); err != nil {
|
||||
return log.E("codegen", "writing output file", err)
|
||||
}
|
||||
lastOutput = out
|
||||
}
|
||||
lastInput = input
|
||||
}
|
||||
if err := coreio.Local.Write(outputPath, out); err != nil {
|
||||
return log.E("codegen", "writing output file", err)
|
||||
}
|
||||
lastInput = input
|
||||
}
|
||||
|
||||
select {
|
||||
|
|
|
|||
|
|
@ -96,6 +96,36 @@ func TestRunDaemon_WritesBundle(t *testing.T) {
|
|||
require.NoError(t, <-done)
|
||||
}
|
||||
|
||||
func TestRunDaemon_RecoversFromInvalidJSON(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
inputPath := dir + "/slots.json"
|
||||
outputPath := dir + "/bundle.js"
|
||||
|
||||
require.NoError(t, writeTestFile(inputPath, `not json`))
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
done <- runDaemon(ctx, inputPath, outputPath, false, 5*time.Millisecond)
|
||||
}()
|
||||
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
require.NoError(t, writeTestFile(inputPath, `{"H":"nav-bar","C":"main-content"}`))
|
||||
|
||||
require.Eventually(t, func() bool {
|
||||
got, err := readTestFile(outputPath)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return strings.Contains(got, "NavBar") && strings.Contains(got, "MainContent")
|
||||
}, time.Second, 10*time.Millisecond)
|
||||
|
||||
cancel()
|
||||
require.NoError(t, <-done)
|
||||
}
|
||||
|
||||
func TestRunDaemon_MissingPaths(t *testing.T) {
|
||||
err := runDaemon(context.Background(), "", "", false, time.Millisecond)
|
||||
require.Error(t, err)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue