2024-01-14 16:06:41 +00:00
|
|
|
local DateTime = require("./datetime")
|
|
|
|
type DateTime = DateTime.DateTime
|
|
|
|
|
2023-07-20 11:25:36 +01:00
|
|
|
export type MetadataKind = "file" | "dir" | "symlink"
|
|
|
|
|
|
|
|
--[=[
|
2023-07-22 10:36:09 +01:00
|
|
|
@interface MetadataPermissions
|
2023-07-20 11:25:36 +01:00
|
|
|
@within FS
|
|
|
|
|
|
|
|
Permissions for the given file or directory.
|
|
|
|
|
|
|
|
This is a dictionary that will contain the following values:
|
|
|
|
|
|
|
|
* `readOnly` - If the target path is read-only or not
|
|
|
|
]=]
|
|
|
|
export type MetadataPermissions = {
|
|
|
|
readOnly: boolean,
|
|
|
|
}
|
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
-- FIXME: We lose doc comments here below in Metadata because of the union type
|
2023-07-20 11:25:36 +01:00
|
|
|
|
|
|
|
--[=[
|
2023-07-22 10:36:09 +01:00
|
|
|
@interface Metadata
|
2023-07-20 11:25:36 +01:00
|
|
|
@within FS
|
|
|
|
|
|
|
|
Metadata for the given file or directory.
|
|
|
|
|
|
|
|
This is a dictionary that will contain the following values:
|
|
|
|
|
|
|
|
* `kind` - If the target path is a `file`, `dir` or `symlink`
|
|
|
|
* `exists` - If the target path exists
|
2024-03-11 12:05:13 +00:00
|
|
|
* `createdAt` - The timestamp represented as a `DateTime` object at which the file or directory was created
|
|
|
|
* `modifiedAt` - The timestamp represented as a `DateTime` object at which the file or directory was last modified
|
|
|
|
* `accessedAt` - The timestamp represented as a `DateTime` object at which the file or directory was last accessed
|
2023-07-20 11:25:36 +01:00
|
|
|
* `permissions` - Current permissions for the file or directory
|
|
|
|
|
|
|
|
Note that timestamps are relative to the unix epoch, and
|
|
|
|
may not be accurate if the system clock is not accurate.
|
|
|
|
]=]
|
|
|
|
export type Metadata = {
|
|
|
|
kind: MetadataKind,
|
|
|
|
exists: true,
|
2024-01-14 16:06:41 +00:00
|
|
|
createdAt: DateTime,
|
|
|
|
modifiedAt: DateTime,
|
|
|
|
accessedAt: DateTime,
|
2023-07-20 11:25:36 +01:00
|
|
|
permissions: MetadataPermissions,
|
|
|
|
} | {
|
|
|
|
kind: nil,
|
|
|
|
exists: false,
|
|
|
|
createdAt: nil,
|
|
|
|
modifiedAt: nil,
|
|
|
|
accessedAt: nil,
|
|
|
|
permissions: nil,
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:45:18 +00:00
|
|
|
--[=[
|
2023-07-22 10:36:09 +01:00
|
|
|
@interface WriteOptions
|
2023-03-24 09:45:18 +00:00
|
|
|
@within FS
|
|
|
|
|
|
|
|
Options for filesystem APIs what write to files and/or directories.
|
|
|
|
|
|
|
|
This is a dictionary that may contain one or more of the following values:
|
|
|
|
|
|
|
|
* `overwrite` - If the target path should be overwritten or not, in the case that it already exists
|
|
|
|
]=]
|
2023-05-08 12:13:34 +01:00
|
|
|
export type WriteOptions = {
|
2023-03-24 09:45:18 +00:00
|
|
|
overwrite: boolean?,
|
|
|
|
}
|
|
|
|
|
|
|
|
--[=[
|
|
|
|
@class FS
|
|
|
|
|
|
|
|
Built-in library for filesystem access
|
|
|
|
|
|
|
|
### Example usage
|
|
|
|
|
|
|
|
```lua
|
|
|
|
local fs = require("@lune/fs")
|
|
|
|
|
|
|
|
-- Reading a file
|
|
|
|
local myTextFile: string = fs.readFile("myFileName.txt")
|
|
|
|
|
|
|
|
-- Reading entries (files & dirs) in a directory
|
|
|
|
for _, entryName in fs.readDir("myDirName") do
|
|
|
|
if fs.isFile("myDirName/" .. entryName) then
|
|
|
|
print("Found file " .. entryName)
|
2023-05-10 01:06:03 +01:00
|
|
|
elseif fs.isDir("myDirName/" .. entryName) then
|
2023-03-24 09:45:18 +00:00
|
|
|
print("Found subdirectory " .. entryName)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
]=]
|
2023-07-22 12:42:29 +01:00
|
|
|
local fs = {}
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
--[=[
|
|
|
|
@within FS
|
|
|
|
@tag must_use
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
Reads a file at `path`.
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
An error will be thrown in the following situations:
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
* `path` does not point to an existing file.
|
|
|
|
* The current process lacks permissions to read the file.
|
|
|
|
* Some other I/O error occurred.
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
@param path The path to the file to read
|
|
|
|
@return The contents of the file
|
|
|
|
]=]
|
|
|
|
function fs.readFile(path: string): string
|
|
|
|
return nil :: any
|
|
|
|
end
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
--[=[
|
|
|
|
@within FS
|
|
|
|
@tag must_use
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
Reads entries in a directory at `path`.
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
An error will be thrown in the following situations:
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
* `path` does not point to an existing directory.
|
|
|
|
* The current process lacks permissions to read the contents of the directory.
|
|
|
|
* Some other I/O error occurred.
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
@param path The directory path to search in
|
|
|
|
@return A list of files & directories found
|
|
|
|
]=]
|
|
|
|
function fs.readDir(path: string): { string }
|
|
|
|
return {}
|
|
|
|
end
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
--[=[
|
|
|
|
@within FS
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
Writes to a file at `path`.
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
An error will be thrown in the following situations:
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
* The file's parent directory does not exist.
|
|
|
|
* The current process lacks permissions to write to the file.
|
|
|
|
* Some other I/O error occurred.
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
@param path The path of the file
|
|
|
|
@param contents The contents of the file
|
|
|
|
]=]
|
2024-04-20 15:44:19 +01:00
|
|
|
function fs.writeFile(path: string, contents: buffer | string) end
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
--[=[
|
|
|
|
@within FS
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
Creates a directory and its parent directories if they are missing.
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
An error will be thrown in the following situations:
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
* `path` already points to an existing file or directory.
|
|
|
|
* The current process lacks permissions to create the directory or its missing parents.
|
|
|
|
* Some other I/O error occurred.
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
@param path The directory to create
|
|
|
|
]=]
|
|
|
|
function fs.writeDir(path: string) end
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
--[=[
|
|
|
|
@within FS
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
Removes a file.
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
An error will be thrown in the following situations:
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
* `path` does not point to an existing file.
|
|
|
|
* The current process lacks permissions to remove the file.
|
|
|
|
* Some other I/O error occurred.
|
2023-07-20 11:25:36 +01:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
@param path The file to remove
|
|
|
|
]=]
|
|
|
|
function fs.removeFile(path: string) end
|
2023-07-20 11:25:36 +01:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
--[=[
|
|
|
|
@within FS
|
2023-07-20 11:25:36 +01:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
Removes a directory and all of its contents.
|
2023-07-20 11:25:36 +01:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
An error will be thrown in the following situations:
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
* `path` is not an existing and empty directory.
|
|
|
|
* The current process lacks permissions to remove the directory.
|
|
|
|
* Some other I/O error occurred.
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
@param path The directory to remove
|
|
|
|
]=]
|
|
|
|
function fs.removeDir(path: string) end
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
--[=[
|
|
|
|
@within FS
|
|
|
|
@tag must_use
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
Gets metadata for the given path.
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
An error will be thrown in the following situations:
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
* The current process lacks permissions to read at `path`.
|
|
|
|
* Some other I/O error occurred.
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
@param path The path to get metadata for
|
|
|
|
@return Metadata for the path
|
|
|
|
]=]
|
|
|
|
function fs.metadata(path: string): Metadata
|
|
|
|
return nil :: any
|
|
|
|
end
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
--[=[
|
|
|
|
@within FS
|
|
|
|
@tag must_use
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
Checks if a given path is a file.
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
An error will be thrown in the following situations:
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
* The current process lacks permissions to read at `path`.
|
|
|
|
* Some other I/O error occurred.
|
2023-03-24 09:45:18 +00:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
@param path The file path to check
|
|
|
|
@return If the path is a file or not
|
|
|
|
]=]
|
|
|
|
function fs.isFile(path: string): boolean
|
|
|
|
return nil :: any
|
|
|
|
end
|
2023-07-20 18:58:18 +01:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
--[=[
|
|
|
|
@within FS
|
|
|
|
@tag must_use
|
2023-07-20 18:58:18 +01:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
Checks if a given path is a directory.
|
2023-07-20 18:58:18 +01:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
An error will be thrown in the following situations:
|
2023-07-20 18:58:18 +01:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
* The current process lacks permissions to read at `path`.
|
|
|
|
* Some other I/O error occurred.
|
2023-07-20 18:58:18 +01:00
|
|
|
|
2023-07-22 12:42:29 +01:00
|
|
|
@param path The directory path to check
|
|
|
|
@return If the path is a directory or not
|
|
|
|
]=]
|
|
|
|
function fs.isDir(path: string): boolean
|
|
|
|
return nil :: any
|
|
|
|
end
|
|
|
|
|
|
|
|
--[=[
|
|
|
|
@within FS
|
|
|
|
|
|
|
|
Moves a file or directory to a new path.
|
|
|
|
|
|
|
|
Throws an error if a file or directory already exists at the target path.
|
|
|
|
This can be bypassed by passing `true` as the third argument, or a dictionary of options.
|
|
|
|
Refer to the documentation for `WriteOptions` for specific option keys and their values.
|
|
|
|
|
|
|
|
An error will be thrown in the following situations:
|
|
|
|
|
|
|
|
* The current process lacks permissions to read at `from` or write at `to`.
|
|
|
|
* The new path exists on a different mount point.
|
|
|
|
* Some other I/O error occurred.
|
|
|
|
|
|
|
|
@param from The path to move from
|
|
|
|
@param to The path to move to
|
|
|
|
@param overwriteOrOptions Options for the target path, such as if should be overwritten if it already exists
|
|
|
|
]=]
|
|
|
|
function fs.move(from: string, to: string, overwriteOrOptions: (boolean | WriteOptions)?) end
|
|
|
|
|
|
|
|
--[=[
|
|
|
|
@within FS
|
|
|
|
|
|
|
|
Copies a file or directory recursively to a new path.
|
|
|
|
|
|
|
|
Throws an error if a file or directory already exists at the target path.
|
|
|
|
This can be bypassed by passing `true` as the third argument, or a dictionary of options.
|
|
|
|
Refer to the documentation for `WriteOptions` for specific option keys and their values.
|
|
|
|
|
|
|
|
An error will be thrown in the following situations:
|
|
|
|
|
|
|
|
* The current process lacks permissions to read at `from` or write at `to`.
|
|
|
|
* Some other I/O error occurred.
|
|
|
|
|
|
|
|
@param from The path to copy from
|
|
|
|
@param to The path to copy to
|
|
|
|
@param overwriteOrOptions Options for the target path, such as if should be overwritten if it already exists
|
|
|
|
]=]
|
|
|
|
function fs.copy(from: string, to: string, overwriteOrOptions: (boolean | WriteOptions)?) end
|
|
|
|
|
|
|
|
return fs
|