Skip to content

Commit b218961

Browse files
authored
add Stat() to vfs and ModTime to FileInfo when filewriting to vfs (microsoft#388)
1 parent 01b4017 commit b218961

File tree

6 files changed

+47
-15
lines changed

6 files changed

+47
-15
lines changed

internal/bundled/embed.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ var rootEntries = []fs.DirEntry{
8181
fs.FileInfoToDirEntry(&fileInfo{name: "libs", mode: fs.ModeDir}),
8282
}
8383

84-
func (vfs *wrappedFS) GetEntries(path string) []fs.DirEntry {
84+
func (vfs *wrappedFS) GetEntries(path string) []vfs.DirEntry {
8585
if rest, ok := splitPath(path); ok {
8686
if rest == "" {
8787
return slices.Clone(rootEntries)
@@ -94,6 +94,21 @@ func (vfs *wrappedFS) GetEntries(path string) []fs.DirEntry {
9494
return vfs.fs.GetEntries(path)
9595
}
9696

97+
func (vfs *wrappedFS) Stat(path string) vfs.FileInfo {
98+
if rest, ok := splitPath(path); ok {
99+
if rest == "" || rest == "libs" {
100+
return &fileInfo{name: rest, mode: fs.ModeDir}
101+
}
102+
if libName, ok := strings.CutPrefix(rest, "libs/"); ok {
103+
if lib, ok := embeddedContents[libName]; ok {
104+
return &fileInfo{name: libName, size: int64(len(lib))}
105+
}
106+
}
107+
return nil
108+
}
109+
return vfs.fs.Stat(path)
110+
}
111+
97112
func (vfs *wrappedFS) WalkDir(root string, walkFn vfs.WalkDirFunc) error {
98113
if rest, ok := splitPath(root); ok {
99114
if err := vfs.walkDir(rest, walkFn); err != nil {

internal/vfs/internal/internal.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (vfs *Common) RootAndPath(path string) (fsys fs.FS, rootName string, rest s
4040
return vfs.RootFor(rootName), rootName, rest
4141
}
4242

43-
func (vfs *Common) stat(path string) fs.FileInfo {
43+
func (vfs *Common) Stat(path string) vfs.FileInfo {
4444
fsys, _, rest := vfs.RootAndPath(path)
4545
if fsys == nil {
4646
return nil
@@ -53,12 +53,12 @@ func (vfs *Common) stat(path string) fs.FileInfo {
5353
}
5454

5555
func (vfs *Common) FileExists(path string) bool {
56-
stat := vfs.stat(path)
56+
stat := vfs.Stat(path)
5757
return stat != nil && !stat.IsDir()
5858
}
5959

6060
func (vfs *Common) DirectoryExists(path string) bool {
61-
stat := vfs.stat(path)
61+
stat := vfs.Stat(path)
6262
return stat != nil && stat.IsDir()
6363
}
6464

@@ -86,7 +86,7 @@ func (vfs *Common) GetAccessibleEntries(path string) (result vfs.Entries) {
8686

8787
if entryType&fs.ModeSymlink != 0 {
8888
// Easy case; UNIX-like system will clearly mark symlinks.
89-
if stat := vfs.stat(path + "/" + entry.Name()); stat != nil {
89+
if stat := vfs.Stat(path + "/" + entry.Name()); stat != nil {
9090
addToResult(entry.Name(), stat.Mode())
9191
}
9292
continue
@@ -97,7 +97,7 @@ func (vfs *Common) GetAccessibleEntries(path string) (result vfs.Entries) {
9797
// TODO(jakebailey): use syscall.Win32FileAttributeData instead
9898
fullPath := path + "/" + entry.Name()
9999
if realpath := vfs.Realpath(fullPath); fullPath != realpath {
100-
if stat := vfs.stat(realpath); stat != nil {
100+
if stat := vfs.Stat(realpath); stat != nil {
101101
addToResult(entry.Name(), stat.Mode())
102102
}
103103
}
@@ -108,7 +108,7 @@ func (vfs *Common) GetAccessibleEntries(path string) (result vfs.Entries) {
108108
return result
109109
}
110110

111-
func (vfs *Common) GetEntries(path string) []fs.DirEntry {
111+
func (vfs *Common) GetEntries(path string) []vfs.DirEntry {
112112
fsys, _, rest := vfs.RootAndPath(path)
113113
if fsys == nil {
114114
return nil

internal/vfs/iovfs/iofs.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,15 @@ func (vfs *ioFS) GetAccessibleEntries(path string) vfs.Entries {
122122
return vfs.common.GetAccessibleEntries(path)
123123
}
124124

125-
func (vfs *ioFS) GetEntries(path string) []fs.DirEntry {
125+
func (vfs *ioFS) GetEntries(path string) []vfs.DirEntry {
126126
return vfs.common.GetEntries(path)
127127
}
128128

129+
func (vfs *ioFS) Stat(path string) vfs.FileInfo {
130+
_ = internal.RootLength(path) // Assert path is rooted
131+
return vfs.common.Stat(path)
132+
}
133+
129134
func (vfs *ioFS) ReadFile(path string) (contents string, ok bool) {
130135
return vfs.common.ReadFile(path)
131136
}

internal/vfs/osvfs/os.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package osvfs
22

33
import (
44
"fmt"
5-
"io/fs"
65
"os"
76
"path/filepath"
87
"runtime"
@@ -93,10 +92,14 @@ func (vfs *osFS) GetAccessibleEntries(path string) vfs.Entries {
9392
return vfs.common.GetAccessibleEntries(path)
9493
}
9594

96-
func (vfs *osFS) GetEntries(path string) []fs.DirEntry {
95+
func (vfs *osFS) GetEntries(path string) []vfs.DirEntry {
9796
return vfs.common.GetEntries(path)
9897
}
9998

99+
func (vfs *osFS) Stat(path string) vfs.FileInfo {
100+
return vfs.common.Stat(path)
101+
}
102+
100103
func (vfs *osFS) WalkDir(root string, walkFn vfs.WalkDirFunc) error {
101104
return vfs.common.WalkDir(root, walkFn)
102105
}

internal/vfs/vfs.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ type FS interface {
2626
GetAccessibleEntries(path string) Entries
2727

2828
// GetEntries returns the entries in the specified directory.
29-
GetEntries(path string) []fs.DirEntry
29+
GetEntries(path string) []DirEntry
30+
31+
Stat(path string) FileInfo
3032

3133
// WalkDir walks the file tree rooted at root, calling walkFn for each file or directory in the tree.
3234
// It is has the same behavior as [fs.WalkDir], but with paths as [string].
@@ -42,8 +44,13 @@ type Entries struct {
4244
Directories []string
4345
}
4446

45-
// DirEntry is [fs.DirEntry].
46-
type DirEntry = fs.DirEntry
47+
type (
48+
// DirEntry is [fs.DirEntry].
49+
DirEntry = fs.DirEntry
50+
51+
// FileInfo is [fs.FileInfo].
52+
FileInfo = fs.FileInfo
53+
)
4754

4855
var (
4956
ErrInvalid = fs.ErrInvalid // "invalid argument"

internal/vfs/vfstest/vfstest.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"sync"
1111
"testing/fstest"
12+
"time"
1213

1314
"github.com/microsoft/typescript-go/internal/tspath"
1415
"github.com/microsoft/typescript-go/internal/vfs"
@@ -333,8 +334,9 @@ func (m *mapFS) WriteFile(path string, data []byte, perm fs.FileMode) error {
333334
}
334335

335336
m.setEntry(path, m.getCanonicalPath(path), fstest.MapFile{
336-
Data: data,
337-
Mode: perm &^ umask,
337+
Data: data,
338+
ModTime: time.Now(),
339+
Mode: perm &^ umask,
338340
})
339341

340342
return nil

0 commit comments

Comments
 (0)