Skip to content

Commit 668b6c1

Browse files
authored
stdlib: cleanup @std/fs (renaming/removing APIs) (#606)
As part of the API audit/cleanup, we are fixing `@std/fs` APIs. This is mostly renaming and removing unnecessary functions, gonna leave `fs.watch` to be its separate PR Parallel changes to `@lute/fs` will be in a subsequent PR after we've agreed on how @std should look like
1 parent dc6b178 commit 668b6c1

File tree

8 files changed

+30
-63
lines changed

8 files changed

+30
-63
lines changed

docs/scripts/reference.luau

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ local function processDirectory(
235235
)
236236
fs.createdirectory(documentationPath, { makeparents = true })
237237

238-
local entries = fs.listdir(modulePath)
238+
local entries = fs.listdirectory(modulePath)
239239

240240
for _, entry in entries do
241241
local entryPath = path.join(modulePath, entry.name)

examples/async_read.luau

Lines changed: 0 additions & 16 deletions
This file was deleted.

examples/create_directory.luau

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ else
1313
print("Failed to create directory")
1414
end
1515

16-
fs.rmdir(directory)
16+
fs.removedirectory(directory)
1717

1818
if not fs.exists(directory) then
1919
print("Directory successfully removed")

lute/cli/commands/test/finder.luau

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ end
1111
local function findtestfilesrec(directory: path.path): { path.path }
1212
local results = {}
1313

14-
for _, entry in fs.listdir(directory) do
14+
for _, entry in fs.listdirectory(directory) do
1515
local fullPath = path.join(directory, entry.name)
1616

1717
if entry.type == "dir" then

lute/std/libs/fs.luau

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ local sys = require("@std/system")
1010
local fslib = {}
1111

1212
export type handlemode = fs.HandleMode
13-
export type filehandle = fs.FileHandle
14-
export type filetype = fs.FileType
15-
export type filemetadata = fs.FileMetadata
13+
export type file = fs.FileHandle
14+
export type type = fs.FileType
15+
export type metadata = fs.FileMetadata
1616
export type directoryentry = fs.DirectoryEntry
1717
export type watchhandle = fs.WatchHandle
1818
export type watchevent = fs.WatchEvent
19-
export type pathlike = pathlib.pathlike
19+
20+
type pathlike = pathlib.pathlike
2021

2122
export type createdirectoryoptions = {
2223
makeparents: boolean?,
@@ -30,39 +31,39 @@ export type walkoptions = {
3031
recursive: boolean?,
3132
}
3233

33-
function fslib.open(path: pathlike, mode: handlemode?): filehandle
34+
function fslib.open(path: pathlike, mode: handlemode?): file
3435
return fs.open(pathlib.format(path), mode)
3536
end
3637

37-
function fslib.read(handle: filehandle): string
38-
return fs.read(handle)
38+
function fslib.read(file: file): string
39+
return fs.read(file)
3940
end
4041

41-
function fslib.write(handle: filehandle, contents: string): ()
42-
return fs.write(handle, contents)
42+
function fslib.write(file: file, contents: string): ()
43+
return fs.write(file, contents)
4344
end
4445

45-
function fslib.close(handle: filehandle): ()
46-
return fs.close(handle)
46+
function fslib.close(file: file): ()
47+
return fs.close(file)
4748
end
4849

4950
function fslib.remove(path: pathlike): ()
5051
return fs.remove(pathlib.format(path))
5152
end
5253

53-
function fslib.stat(path: pathlike): filemetadata
54+
function fslib.metadata(path: pathlike): metadata
5455
return fs.stat(pathlib.format(path))
5556
end
5657

57-
function fslib.type(path: pathlike): filetype
58+
function fslib.type(path: pathlike): type
5859
return fs.type(pathlib.format(path))
5960
end
6061

6162
function fslib.link(src: pathlike, dest: pathlike): ()
6263
return fs.link(pathlib.format(src), pathlib.format(dest))
6364
end
6465

65-
function fslib.symlink(src: pathlike, dest: pathlike): ()
66+
function fslib.symboliclink(src: pathlike, dest: pathlike): ()
6667
return fs.symlink(pathlib.format(src), pathlib.format(dest))
6768
end
6869

@@ -78,14 +79,10 @@ function fslib.copy(src: pathlike, dest: pathlike): ()
7879
return fs.copy(pathlib.format(src), pathlib.format(dest))
7980
end
8081

81-
function fslib.listdir(path: pathlike): { directoryentry }
82+
function fslib.listdirectory(path: pathlike): { directoryentry }
8283
return fs.listdir(pathlib.format(path))
8384
end
8485

85-
function fslib.rmdir(path: pathlike): ()
86-
return fs.rmdir(pathlib.format(path))
87-
end
88-
8986
function fslib.readfiletostring(filepath: pathlike): string
9087
return fs.readfiletostring(pathlib.format(filepath))
9188
end
@@ -94,10 +91,6 @@ function fslib.writestringtofile(filepath: pathlike, contents: string): ()
9491
return fs.writestringtofile(pathlib.format(filepath), contents)
9592
end
9693

97-
function fslib.readasync(filepath: pathlike): string
98-
return fs.readasync(pathlib.format(filepath))
99-
end
100-
10194
function fslib.createdirectory(path: pathlike, options: createdirectoryoptions?): ()
10295
if options and options.makeparents then
10396
local parsed = pathlib.parse(path)
@@ -129,7 +122,7 @@ function fslib.removedirectory(path: pathlike, options: removedirectoryoptions?)
129122
if options and options.recursive then
130123
-- Recursively delete all contents first
131124
if fslib.exists(path) and fslib.type(path) == "dir" then
132-
local entries = fslib.listdir(path)
125+
local entries = fslib.listdirectory(path)
133126
for _, entry in entries do
134127
local entryPath = pathlib.join(path, entry.name)
135128
if entry.type == "dir" then
@@ -157,7 +150,7 @@ function fslib.walk(path: pathlike, options: walkoptions?): () -> path?
157150
end
158151

159152
if fslib.type(current) == "dir" and options and options.recursive then
160-
local entries = fslib.listdir(current)
153+
local entries = fslib.listdirectory(current)
161154
for _, entry in entries do
162155
local fullPath = pathlib.join(current, entry.name)
163156
table.insert(queue, fullPath)

tests/cli/loadbypath.test.luau

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ test.suite("Lute CLI Run", function(suite)
5252

5353
suite:afterall(function()
5454
if fs.exists(testDirStr) then
55-
fs.rmdir(testDirStr)
55+
fs.removedirectory(testDirStr)
5656
end
5757
end)
5858
end)

tests/std/fs.test.luau

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test.suite("FsSuite", function(suite)
1414
fs.write(h, "abc")
1515
fs.close(h)
1616

17-
local m = fs.stat(file)
17+
local m = fs.metadata(file)
1818
assert.eq(m.size, 3)
1919

2020
local hr = fs.open(file, "r")
@@ -55,25 +55,15 @@ test.suite("FsSuite", function(suite)
5555
assert.eq(fs.exists(dir), true)
5656
assert.eq(fs.type(dir), "dir")
5757

58-
local entries = fs.listdir(tmpdir)
58+
local entries = fs.listdirectory(tmpdir)
5959
local found = false
6060
for _, e in entries do
6161
if e.name == "subdir" then
6262
found = true
6363
end
6464
end
6565
assert.eq(found, true)
66-
fs.rmdir(dir)
67-
end)
68-
69-
suite:case("readasync_and_writestring", function(assert)
70-
local file = path.join(tmpdir, "async.txt")
71-
fs.writestringtofile(file, "async content")
72-
73-
local got = fs.readasync(file)
74-
assert.eq(got, "async content")
75-
76-
fs.remove(file)
66+
fs.removedirectory(dir)
7767
end)
7868

7969
suite:case("link_and_symlink_and_copy", function(assert)
@@ -92,7 +82,7 @@ test.suite("FsSuite", function(suite)
9282
fs.remove(dstcopy)
9383

9484
local dstsymlink = path.join(tmpdir, "dstsymlink")
95-
fs.symlink(srcfile, dstsymlink)
85+
fs.symboliclink(srcfile, dstsymlink)
9686
assert.eq(fs.readfiletostring(dstsymlink), "linkcontent")
9787
fs.remove(dstsymlink)
9888

@@ -131,8 +121,8 @@ test.suite("FsSuite", function(suite)
131121
fs.createdirectory(nestedDir, { makeparents = true })
132122
assert.eq(fs.exists(nestedDir), true)
133123

134-
fs.rmdir(nestedDir)
135-
fs.rmdir(path.join(tmpdir, "nested"))
124+
fs.removedirectory(nestedDir)
125+
fs.removedirectory(path.join(tmpdir, "nested"))
136126
end)
137127

138128
suite:case("createdirectory_makeparents_false", function(assert)
@@ -223,7 +213,7 @@ test.suite("FsSuite", function(suite)
223213

224214
-- Cleanup
225215
fs.remove(file)
226-
fs.rmdir(dir)
216+
fs.removedirectory(dir)
227217
end)
228218

229219
suite:case("walk_directory_recursive", function(assert)

tests/std/syntax/parser.test.luau

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ test.suite("Parser", function(suite)
124124

125125
suite:case("roundtrippableAst", function(assert)
126126
local function visitDirectory(directory: string)
127-
for _, entry in fs.listdir(directory) do
127+
for _, entry in fs.listdirectory(directory) do
128128
if entry.type ~= "file" then
129129
continue
130130
end

0 commit comments

Comments
 (0)