140 lines
2.9 KiB
Lua
140 lines
2.9 KiB
Lua
local fs = {}
|
|
|
|
-- File Operations
|
|
fs.read_file = function(path)
|
|
if type(path) ~= "string" then
|
|
error("fs.read_file: path must be a string", 2)
|
|
end
|
|
return __fs_read_file(path)
|
|
end
|
|
|
|
fs.write_file = function(path, content)
|
|
if type(path) ~= "string" then
|
|
error("fs.write_file: path must be a string", 2)
|
|
end
|
|
if type(content) ~= "string" then
|
|
error("fs.write_file: content must be a string", 2)
|
|
end
|
|
return __fs_write_file(path, content)
|
|
end
|
|
|
|
fs.append_file = function(path, content)
|
|
if type(path) ~= "string" then
|
|
error("fs.append_file: path must be a string", 2)
|
|
end
|
|
if type(content) ~= "string" then
|
|
error("fs.append_file: content must be a string", 2)
|
|
end
|
|
return __fs_append_file(path, content)
|
|
end
|
|
|
|
fs.exists = function(path)
|
|
if type(path) ~= "string" then
|
|
error("fs.exists: path must be a string", 2)
|
|
end
|
|
return __fs_exists(path)
|
|
end
|
|
|
|
fs.remove_file = function(path)
|
|
if type(path) ~= "string" then
|
|
error("fs.remove_file: path must be a string", 2)
|
|
end
|
|
return __fs_remove_file(path)
|
|
end
|
|
|
|
fs.get_info = function(path)
|
|
if type(path) ~= "string" then
|
|
error("fs.get_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
|
|
fs.make_dir = function(path, mode)
|
|
if type(path) ~= "string" then
|
|
error("fs.make_dir: path must be a string", 2)
|
|
end
|
|
mode = mode or 0755
|
|
return __fs_make_dir(path, mode)
|
|
end
|
|
|
|
fs.list_dir = function(path)
|
|
if type(path) ~= "string" then
|
|
error("fs.list_dir: path must be a string", 2)
|
|
end
|
|
return __fs_list_dir(path)
|
|
end
|
|
|
|
fs.remove_dir = function(path, recursive)
|
|
if type(path) ~= "string" then
|
|
error("fs.remove_dir: path must be a string", 2)
|
|
end
|
|
recursive = recursive or false
|
|
return __fs_remove_dir(path, recursive)
|
|
end
|
|
|
|
-- Path Operations
|
|
fs.join_paths = function(...)
|
|
return __fs_join_paths(...)
|
|
end
|
|
|
|
fs.dir_name = function(path)
|
|
if type(path) ~= "string" then
|
|
error("fs.dir_name: path must be a string", 2)
|
|
end
|
|
return __fs_dir_name(path)
|
|
end
|
|
|
|
fs.base_name = function(path)
|
|
if type(path) ~= "string" then
|
|
error("fs.base_name: path must be a string", 2)
|
|
end
|
|
return __fs_base_name(path)
|
|
end
|
|
|
|
fs.extension = function(path)
|
|
if type(path) ~= "string" then
|
|
error("fs.extension: path must be a string", 2)
|
|
end
|
|
return __fs_extension(path)
|
|
end
|
|
|
|
-- Utility Functions
|
|
fs.read_json = function(path)
|
|
local content = fs.read_file(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
|
|
|
|
fs.write_json = function(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_file(path, content)
|
|
end
|
|
|
|
return fs
|