137 lines
2.8 KiB
Lua

-- fs.lua
function fs_read(path)
if type(path) ~= "string" then
error("fs_read: path must be a string", 2)
end
return __fs_read_file(path)
end
function fs_write(path, content)
if type(path) ~= "string" then
error("fs_write: path must be a string", 2)
end
if type(content) ~= "string" then
error("fs_write: content must be a string", 2)
end
return __fs_write_file(path, content)
end
function fs_append(path, content)
if type(path) ~= "string" then
error("fs_append: path must be a string", 2)
end
if type(content) ~= "string" then
error("fs_append: content must be a string", 2)
end
return __fs_append_file(path, content)
end
function fs_exists(path)
if type(path) ~= "string" then
error("fs_exists: path must be a string", 2)
end
return __fs_exists(path)
end
function fs_remove(path)
if type(path) ~= "string" then
error("fs_remove: path must be a string", 2)
end
return __fs_remove_file(path)
end
function fs_info(path)
if type(path) ~= "string" then
error("fs_info: path must be a string", 2)
end
local info = __fs_get_info(path)
-- Convert the Unix timestamp to a readable date
if info and info.mod_time then
info.mod_time_str = os.date("%Y-%m-%d %H:%M:%S", info.mod_time)
end
return info
end
-- Directory Operations
function fs_mkdir(path, mode)
if type(path) ~= "string" then
error("fs_mkdir: path must be a string", 2)
end
mode = mode or 0755
return __fs_make_dir(path, mode)
end
function fs_ls(path)
if type(path) ~= "string" then
error("fs_ls: path must be a string", 2)
end
return __fs_list_dir(path)
end
function fs_rmdir(path, recursive)
if type(path) ~= "string" then
error("fs_rmdir: path must be a string", 2)
end
recursive = recursive or false
return __fs_remove_dir(path, recursive)
end
-- Path Operations
function fs_join_paths(...)
return __fs_join_paths(...)
end
function fs_dir_name(path)
if type(path) ~= "string" then
error("fs_dir_name: path must be a string", 2)
end
return __fs_dir_name(path)
end
function fs_base_name(path)
if type(path) ~= "string" then
error("fs_base_name: path must be a string", 2)
end
return __fs_base_name(path)
end
function fs_extension(path)
if type(path) ~= "string" then
error("fs_extension: path must be a string", 2)
end
return __fs_extension(path)
end
-- Utility Functions
function fs_read_json(path)
local content = fs_read(path)
if not content then
return nil, "Could not read file"
end
local ok, result = pcall(json.decode, content)
if not ok then
return nil, "Invalid JSON: " .. tostring(result)
end
return result
end
function fs_write_json(path, data, pretty)
if type(data) ~= "table" then
error("fs_write_json: data must be a table", 2)
end
local content
if pretty then
content = json.pretty_print(data)
else
content = json.encode(data)
end
return fs_write(path, content)
end