fix(codegen): keep watch mode alive on missing input files
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
030a41f732
commit
575150d686
3 changed files with 41 additions and 0 deletions
|
|
@ -88,6 +88,17 @@ func runDaemon(ctx context.Context, inputPath, outputPath string, emitTypes bool
|
|||
for {
|
||||
input, err := coreio.Local.Read(inputPath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if errors.Is(ctx.Err(), context.Canceled) {
|
||||
return nil
|
||||
}
|
||||
return ctx.Err()
|
||||
case <-ticker.C:
|
||||
}
|
||||
continue
|
||||
}
|
||||
return log.E("codegen", "reading input file", err)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -149,6 +149,34 @@ func TestRunDaemon_RecoversFromInvalidJSON(t *testing.T) {
|
|||
require.NoError(t, <-done)
|
||||
}
|
||||
|
||||
func TestRunDaemon_RecoversFromMissingInputFile(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
inputPath := dir + "/slots.json"
|
||||
outputPath := dir + "/bundle.js"
|
||||
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -151,6 +151,8 @@ To run the daemon mode, point it at an input JSON file and an output bundle path
|
|||
go run ./cmd/codegen/ -watch -input slots.json -output components.js
|
||||
```
|
||||
|
||||
Watch mode keeps polling through transient missing files and invalid JSON edits, then rewrites the output bundle once the input becomes valid again.
|
||||
|
||||
Add `-dts` to emit TypeScript declarations instead of JavaScript in either mode.
|
||||
|
||||
To test the CLI:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue