fs
Built-in library for filesystem access
Example usage
Section titled “Example usage”local fs = require("@lune/fs")
-- Reading a filelocal myTextFile: string = fs.readFile("myFileName.txt")
-- Reading entries (files & dirs) in a directoryfor _, entryName in fs.readDir("myDirName") do if fs.isFile("myDirName/" .. entryName) then print("Found file " .. entryName) elseif fs.isDir("myDirName/" .. entryName) then print("Found subdirectory " .. entryName) endendFunctions
Section titled “Functions”readFile
Section titled “readFile”function fs.readFile(path: string): stringmust_use
Reads a file at path.
An error will be thrown in the following situations:
pathdoes not point to an existing file.- The current process lacks permissions to read the file.
- Some other I/O error occurred.
Parameters
path- The path to the file to read
Returns
- The contents of the file
readDir
Section titled “readDir”function fs.readDir(path: string): { string }must_use
Reads entries in a directory at path.
An error will be thrown in the following situations:
pathdoes not point to an existing directory.- The current process lacks permissions to read the contents of the directory.
- Some other I/O error occurred.
Parameters
path- The directory path to search in
Returns
- A list of files & directories found
writeFile
Section titled “writeFile”function fs.writeFile(path: string, contents: buffer | string) endWrites to a file at path.
An error will be thrown in the following situations:
- The file’s parent directory does not exist.
- The current process lacks permissions to write to the file.
- Some other I/O error occurred.
Parameters
path- The path of the filecontents- The contents of the file
writeDir
Section titled “writeDir”function fs.writeDir(path: string) endCreates a directory and its parent directories if they are missing.
An error will be thrown in the following situations:
pathalready 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.
Parameters
path- The directory to create
removeFile
Section titled “removeFile”function fs.removeFile(path: string) endRemoves a file.
An error will be thrown in the following situations:
pathdoes not point to an existing file.- The current process lacks permissions to remove the file.
- Some other I/O error occurred.
Parameters
path- The file to remove
removeDir
Section titled “removeDir”function fs.removeDir(path: string) endRemoves a directory and all of its contents.
An error will be thrown in the following situations:
pathis not an existing and empty directory.- The current process lacks permissions to remove the directory.
- Some other I/O error occurred.
Parameters
path- The directory to remove
metadata
Section titled “metadata”function fs.metadata(path: string): Metadatamust_use
Gets metadata for the given path.
An error will be thrown in the following situations:
- The current process lacks permissions to read at
path. - Some other I/O error occurred.
Parameters
path- The path to get metadata for
Returns
- Metadata for the path
isFile
Section titled “isFile”function fs.isFile(path: string): booleanmust_use
Checks if a given path is a file.
An error will be thrown in the following situations:
- The current process lacks permissions to read at
path. - Some other I/O error occurred.
Parameters
path- The file path to check
Returns
- If the path is a file or not
function fs.isDir(path: string): booleanmust_use
Checks if a given path is a directory.
An error will be thrown in the following situations:
- The current process lacks permissions to read at
path. - Some other I/O error occurred.
Parameters
path- The directory path to check
Returns
- If the path is a directory or not
function fs.move(from: string, to: string, overwriteOrOptions: (boolean | WriteOptions)?) endMoves 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
fromor write atto. - The new path exists on a different mount point.
- Some other I/O error occurred.
Parameters
from- The path to move fromto- The path to move tooverwriteOrOptions- 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)?) endCopies 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
fromor write atto. - Some other I/O error occurred.
Parameters
from- The path to copy fromto- The path to copy tooverwriteOrOptions- Options for the target path, such as if should be overwritten if it already exists
MetadataPermissions
Section titled “MetadataPermissions”export type MetadataPermissions = { readOnly: boolean,}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
Metadata
Section titled “Metadata”export type Metadata = { kind: "file", exists: true, size: number, createdAt: DateTime, modifiedAt: DateTime, accessedAt: DateTime, permissions: MetadataPermissions, } | { kind: "dir", exists: true, size: nil, createdAt: DateTime, modifiedAt: DateTime, accessedAt: DateTime, permissions: MetadataPermissions, } | { kind: nil, exists: false, size: nil, createdAt: nil, modifiedAt: nil, accessedAt: nil, permissions: nil, }Metadata for the given file or directory.
This is a dictionary that will contain the following values:
kind- If the target path is afile,dirorsymlinkexists- If the target path existscreatedAt- The timestamp represented as aDateTimeobject at which the file or directory was createdmodifiedAt- The timestamp represented as aDateTimeobject at which the file or directory was last modifiedaccessedAt- The timestamp represented as aDateTimeobject at which the file or directory was last accessedpermissions- 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.
WriteOptions
Section titled “WriteOptions”export type WriteOptions = { overwrite: boolean?,}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
