tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/engine/fs/livedir.go (about)

     1  package fs
     2  
     3  import (
     4  	"embed"
     5  	"io/fs"
     6  	"os"
     7  	"path/filepath"
     8  	"runtime"
     9  
    10  	"tractor.dev/toolkit-go/engine/fs/watchfs"
    11  )
    12  
    13  // LiveDir uses the filename of the calling source file and sees if it exists on the system
    14  // to return a watchable version of that directory, otherwise it returns the assets passed in.
    15  func LiveDir(assets embed.FS) FS {
    16  	_, filename, _, _ := runtime.Caller(1)
    17  	_, err := os.Stat(filename)
    18  	if os.IsNotExist(err) {
    19  		return assets
    20  	}
    21  	return watchfs.New(os.DirFS(filepath.Dir(filename)).(fs.StatFS))
    22  }