Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lute/cli/commands/setup/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ local args = { ... }

-- TODO we're assuming the files are completely unmodified, but we really shouldn't do that.
print(`Writing definitions at {BASE_PATH_PRETTY}`)
if fs.exists(BASE_PATH) then
fs.removedirectory(BASE_PATH, { recursive = true })
end
fs.createdirectory(BASE_PATH, { makeparents = true })

for key, value in definitions :: { [string]: string } do
Expand Down
24 changes: 24 additions & 0 deletions lute/std/libs/fs.luau
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export type createdirectoryoptions = {
makeparents: boolean?,
}

export type removedirectoryoptions = {
recursive: boolean?,
}

function fslib.open(path: pathlike, mode: handlemode?): filehandle
return fs.open(pathlib.format(path), mode)
end
Expand Down Expand Up @@ -117,4 +121,24 @@ function fslib.createdirectory(path: pathlike, options: createdirectoryoptions?)
end
end

function fslib.removedirectory(path: pathlike, options: removedirectoryoptions?): ()
if options and options.recursive then
-- Recursively delete all contents first
if fslib.exists(path) and fslib.type(path) == "dir" then
local entries = fslib.listdir(path)
for _, entry in entries do
local entryPath = pathlib.join(path, entry.name)
if entry.type == "dir" then
fslib.removedirectory(entryPath, { recursive = true })
else
fslib.remove(entryPath)
end
end
fs.rmdir(pathlib.format(path))
end
else
fs.rmdir(pathlib.format(path))
end
end

return table.freeze(fslib)
58 changes: 58 additions & 0 deletions tests/std/fs.test.luau
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,64 @@ test.suite("FsSuite", function(suite)

fs.remove(file)
end)

suite:case("removedirectory_non_recursive", function(assert)
local dir = path.join(tmpdir, "empty_dir")
fs.createdirectory(dir, { makeparents = true })
assert.eq(fs.exists(dir), true)

fs.removedirectory(dir)
assert.eq(fs.exists(dir), false)
end)

suite:case("removedirectory_recursive", function(assert)
local dir = path.join(tmpdir, "recursive_test")
local subdir1 = path.join(dir, "subdir1")
local subdir2 = path.join(dir, "subdir2")
local nestedDir = path.join(subdir1, "nested")

fs.createdirectory(nestedDir, { makeparents = true })
fs.createdirectory(subdir2, { makeparents = true })

local file1 = path.join(dir, "file1.txt")
local file2 = path.join(subdir1, "file2.txt")
local file3 = path.join(nestedDir, "file3.txt")

fs.writestringtofile(file1, "content1")
fs.writestringtofile(file2, "content2")
fs.writestringtofile(file3, "content3")

assert.eq(fs.exists(dir), true)
assert.eq(fs.exists(file1), true)
assert.eq(fs.exists(file2), true)
assert.eq(fs.exists(file3), true)

fs.removedirectory(dir, { recursive = true })

assert.eq(fs.exists(dir), false)
assert.eq(fs.exists(file1), false)
assert.eq(fs.exists(file2), false)
assert.eq(fs.exists(file3), false)
end)

suite:case("removedirectory_recursive_false_with_contents", function(assert)
local dir = path.join(tmpdir, "non_empty_dir")
fs.createdirectory(dir, { makeparents = true })

local file = path.join(dir, "file.txt")
fs.writestringtofile(file, "content")

local success, err = pcall(function()
fs.removedirectory(dir, { recursive = false })
end)

assert.neq(success, true)
assert.neq(err, nil)

-- Cleanup
fs.remove(file)
fs.rmdir(dir)
end)
end)

test.run()