2023-02-06 18:02:13 +00:00
|
|
|
-- Lune v0.3.0
|
2023-01-19 19:57:39 +00:00
|
|
|
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@class fs
|
|
|
|
|
|
|
|
Filesystem
|
|
|
|
]=]
|
2023-01-19 01:47:14 +00:00
|
|
|
declare fs: {
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within fs
|
|
|
|
|
|
|
|
Reads a file at `path`.
|
|
|
|
|
|
|
|
An error will be thrown in the following situations:
|
|
|
|
|
|
|
|
* `path` does not point to an existing file.
|
|
|
|
* The current process lacks permissions to read the file.
|
|
|
|
* The contents of the file cannot be read as a UTF-8 string.
|
|
|
|
* Some other I/O error occurred.
|
2023-01-27 00:56:50 +00:00
|
|
|
|
|
|
|
@param path The path to the file to read
|
|
|
|
@return The contents of the file
|
2023-01-27 00:36:06 +00:00
|
|
|
]=]
|
2023-01-19 01:47:14 +00:00
|
|
|
readFile: (path: string) -> string,
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within fs
|
|
|
|
|
|
|
|
Reads entries in a directory at `path`.
|
|
|
|
|
|
|
|
An error will be thrown in the following situations:
|
|
|
|
|
|
|
|
* `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-01-27 00:56:50 +00:00
|
|
|
|
|
|
|
@param path The directory path to search in
|
|
|
|
@return A list of files & directories found
|
2023-01-27 00:36:06 +00:00
|
|
|
]=]
|
2023-01-19 01:47:14 +00:00
|
|
|
readDir: (path: string) -> { string },
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within fs
|
|
|
|
|
|
|
|
Writes 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.
|
2023-01-27 00:56:50 +00:00
|
|
|
|
|
|
|
@param path The path of the file
|
|
|
|
@param contents The contents of the file
|
2023-01-27 00:36:06 +00:00
|
|
|
]=]
|
2023-01-19 01:47:14 +00:00
|
|
|
writeFile: (path: string, contents: string) -> (),
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within fs
|
|
|
|
|
|
|
|
Creates a directory and its parent directories if they are missing.
|
|
|
|
|
|
|
|
An error will be thrown in the following situations:
|
|
|
|
|
|
|
|
* `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-01-27 00:56:50 +00:00
|
|
|
|
|
|
|
@param path The directory to create
|
2023-01-27 00:36:06 +00:00
|
|
|
]=]
|
2023-01-19 01:47:14 +00:00
|
|
|
writeDir: (path: string) -> (),
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within fs
|
|
|
|
|
|
|
|
Removes a file.
|
|
|
|
|
|
|
|
An error will be thrown in the following situations:
|
|
|
|
|
|
|
|
* `path` does not point to an existing file.
|
|
|
|
* The current process lacks permissions to remove the file.
|
|
|
|
* Some other I/O error occurred.
|
2023-01-27 00:56:50 +00:00
|
|
|
|
|
|
|
@param path The file to remove
|
2023-01-27 00:36:06 +00:00
|
|
|
]=]
|
2023-01-19 01:47:14 +00:00
|
|
|
removeFile: (path: string) -> (),
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within fs
|
|
|
|
|
|
|
|
Removes a directory and all of its contents.
|
|
|
|
|
|
|
|
An error will be thrown in the following situations:
|
|
|
|
|
|
|
|
* `path` is not an existing and empty directory.
|
|
|
|
* The current process lacks permissions to remove the directory.
|
|
|
|
* Some other I/O error occurred.
|
2023-01-27 00:56:50 +00:00
|
|
|
|
|
|
|
@param path The directory to remove
|
2023-01-27 00:36:06 +00:00
|
|
|
]=]
|
2023-01-19 01:47:14 +00:00
|
|
|
removeDir: (path: string) -> (),
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within fs
|
|
|
|
|
|
|
|
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.
|
2023-01-27 00:56:50 +00:00
|
|
|
|
|
|
|
@param path The file path to check
|
|
|
|
@return If the path is a file or not
|
2023-01-27 00:36:06 +00:00
|
|
|
]=]
|
2023-01-19 01:47:14 +00:00
|
|
|
isFile: (path: string) -> boolean,
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within fs
|
|
|
|
|
|
|
|
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.
|
2023-01-27 00:56:50 +00:00
|
|
|
|
|
|
|
@param path The directory path to check
|
|
|
|
@return If the path is a directory or not
|
2023-01-27 00:36:06 +00:00
|
|
|
]=]
|
2023-01-19 01:47:14 +00:00
|
|
|
isDir: (path: string) -> boolean,
|
|
|
|
}
|
|
|
|
|
2023-02-04 04:40:27 +00:00
|
|
|
type NetMethod = "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | "PATCH"
|
|
|
|
|
|
|
|
export type NetFetchParams = {
|
|
|
|
url: string,
|
|
|
|
method: NetMethod?,
|
|
|
|
headers: { [string]: string }?,
|
|
|
|
body: string?,
|
|
|
|
}
|
|
|
|
export type NetFetchResponse = {
|
|
|
|
ok: boolean,
|
|
|
|
statusCode: number,
|
|
|
|
statusMessage: string,
|
|
|
|
headers: { [string]: string },
|
|
|
|
body: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
export type NetRequest = {
|
|
|
|
path: string,
|
|
|
|
query: string,
|
|
|
|
method: NetMethod,
|
|
|
|
headers: { [string]: string },
|
|
|
|
body: string,
|
|
|
|
}
|
|
|
|
export type NetResponse = {
|
|
|
|
status: number?,
|
|
|
|
headers: { [string]: string }?,
|
|
|
|
body: string?,
|
|
|
|
}
|
|
|
|
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@class net
|
|
|
|
|
|
|
|
Networking
|
|
|
|
]=]
|
2023-01-19 22:56:12 +00:00
|
|
|
declare net: {
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within net
|
|
|
|
|
|
|
|
Sends an HTTP request using the given url and / or parameters, and returns a dictionary that describes the response received.
|
|
|
|
|
|
|
|
Only throws an error if a miscellaneous network or I/O error occurs, never for unsuccessful status codes.
|
2023-01-27 00:56:50 +00:00
|
|
|
|
|
|
|
@param config The URL or request config to use
|
|
|
|
@return A dictionary representing the response for the request
|
2023-01-27 00:36:06 +00:00
|
|
|
]=]
|
2023-02-04 04:40:27 +00:00
|
|
|
request: (config: string | NetFetchParams) -> NetFetchResponse,
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within net
|
|
|
|
|
2023-02-04 00:27:56 +00:00
|
|
|
Creates an HTTP server that listens on the given `port`.
|
|
|
|
|
|
|
|
The call to this function will block indefinitely and if
|
|
|
|
put inside `task.spawn` will not be cancellable using `task.cancel`, to
|
|
|
|
stop the script from running it must be terminated manually or using `process.exit`.
|
|
|
|
|
|
|
|
@param port The port to use for the server
|
|
|
|
@param handler The handler function to use for the server
|
|
|
|
]=]
|
2023-02-04 04:40:27 +00:00
|
|
|
serve: (port: number, handler: (request: NetRequest) -> (string | NetResponse)) -> (),
|
2023-02-04 00:27:56 +00:00
|
|
|
--[=[
|
|
|
|
@within net
|
|
|
|
|
2023-01-27 00:36:06 +00:00
|
|
|
Encodes the given value as JSON.
|
2023-01-27 00:56:50 +00:00
|
|
|
|
|
|
|
@param value The value to encode as JSON
|
|
|
|
@param pretty If the encoded JSON string should include newlines and spaces. Defaults to false
|
|
|
|
@return The encoded JSON string
|
2023-01-27 00:36:06 +00:00
|
|
|
]=]
|
2023-01-19 22:56:12 +00:00
|
|
|
jsonEncode: (value: any, pretty: boolean?) -> string,
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within net
|
|
|
|
|
|
|
|
Decodes the given JSON string into a lua value.
|
2023-01-27 00:56:50 +00:00
|
|
|
|
|
|
|
@param encoded The JSON string to decode
|
|
|
|
@return The decoded lua value
|
2023-01-27 00:36:06 +00:00
|
|
|
]=]
|
2023-01-19 22:56:12 +00:00
|
|
|
jsonDecode: (encoded: string) -> any,
|
2023-01-19 01:47:14 +00:00
|
|
|
}
|
|
|
|
|
2023-02-04 04:40:27 +00:00
|
|
|
type ProcessSpawnOptionsStdio = "inherit" | "default"
|
|
|
|
|
|
|
|
export type ProcessSpawnOptions = {
|
|
|
|
cwd: string?,
|
|
|
|
env: { [string]: string }?,
|
|
|
|
shell: (boolean | string)?,
|
|
|
|
stdio: ProcessSpawnOptionsStdio?,
|
|
|
|
}
|
|
|
|
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@class process
|
|
|
|
|
|
|
|
Current process & child processes
|
|
|
|
]=]
|
2023-01-19 01:47:14 +00:00
|
|
|
declare process: {
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within process
|
|
|
|
|
|
|
|
The arguments given when running the Lune script.
|
|
|
|
]=]
|
2023-01-20 20:21:20 +00:00
|
|
|
args: { string },
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within process
|
|
|
|
|
2023-01-28 02:32:08 +00:00
|
|
|
The current working directory in which the Lune script is running.
|
|
|
|
]=]
|
|
|
|
cwd: string,
|
|
|
|
--[=[
|
|
|
|
@within process
|
|
|
|
|
2023-01-27 00:36:06 +00:00
|
|
|
Current environment variables for this process.
|
|
|
|
|
|
|
|
Setting a value on this table will set the corresponding environment variable.
|
|
|
|
]=]
|
2023-01-20 20:21:20 +00:00
|
|
|
env: { [string]: string? },
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within process
|
|
|
|
|
|
|
|
Exits the currently running script as soon as possible with the given exit code.
|
|
|
|
|
|
|
|
Exit code 0 is treated as a successful exit, any other value is treated as an error.
|
|
|
|
|
|
|
|
Setting the exit code using this function will override any otherwise automatic exit code.
|
2023-01-27 00:56:50 +00:00
|
|
|
|
|
|
|
@param code The exit code to set
|
2023-01-27 00:36:06 +00:00
|
|
|
]=]
|
2023-01-19 01:47:14 +00:00
|
|
|
exit: (code: number?) -> (),
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within process
|
|
|
|
|
2023-01-28 02:32:08 +00:00
|
|
|
Spawns a child process that will run the program `program`, and returns a dictionary that describes the final status and ouput of the child process.
|
|
|
|
|
|
|
|
The second argument, `params`, can be passed as a list of string parameters to give to the program.
|
|
|
|
|
|
|
|
The third argument, `options`, can be passed as a dictionary of options to give to the child process.
|
|
|
|
The available options inside of the `options` dictionary are:
|
|
|
|
* `cwd` - The current working directory for the process
|
|
|
|
* `env` - Extra environment variables to give to the process
|
|
|
|
* `shell` - Whether to run in a shell or not - set to `true` to run using the default shell, or a string to run using a specific shell
|
|
|
|
* `stdio` - How to treat output and error streams from the child process - set to "inherit" to pass output and error streams to the current process
|
2023-01-27 00:56:50 +00:00
|
|
|
|
|
|
|
@param program The program to spawn as a child process
|
|
|
|
@param params Additional parameters to pass to the program
|
2023-01-28 02:32:08 +00:00
|
|
|
@param options A dictionary of options for the child process
|
2023-01-27 00:56:50 +00:00
|
|
|
@return A dictionary representing the result of the child process
|
2023-01-27 00:36:06 +00:00
|
|
|
]=]
|
2023-01-28 02:32:08 +00:00
|
|
|
spawn: (
|
|
|
|
program: string,
|
|
|
|
params: { string }?,
|
2023-02-04 04:40:27 +00:00
|
|
|
options: ProcessSpawnOptions?
|
2023-01-28 02:32:08 +00:00
|
|
|
) -> {
|
2023-01-19 01:47:14 +00:00
|
|
|
ok: boolean,
|
|
|
|
code: number,
|
|
|
|
stdout: string,
|
|
|
|
stderr: string,
|
|
|
|
},
|
|
|
|
}
|
2023-01-21 18:33:33 +00:00
|
|
|
|
2023-02-06 05:13:12 +00:00
|
|
|
--[=[
|
|
|
|
@class stdio
|
|
|
|
|
|
|
|
Standard input / output & utility functions
|
|
|
|
]=]
|
|
|
|
declare stdio: {
|
|
|
|
--[=[
|
|
|
|
@within stdio
|
|
|
|
|
|
|
|
Return an ANSI string that can be used to modify the persistent output color.
|
|
|
|
|
|
|
|
Pass `"reset"` to get a string that can reset the persistent output color.
|
|
|
|
|
|
|
|
### Example usage
|
|
|
|
|
|
|
|
```lua
|
|
|
|
stdio.write(stdio.color("red"))
|
|
|
|
print("This text will be red")
|
|
|
|
stdio.write(stdio.color("reset"))
|
|
|
|
print("This text will be normal")
|
|
|
|
```
|
|
|
|
|
|
|
|
@param color The color to use
|
2023-02-06 18:10:52 +00:00
|
|
|
@return A printable ANSI string
|
2023-02-06 05:13:12 +00:00
|
|
|
]=]
|
2023-02-06 18:10:52 +00:00
|
|
|
color: (color: "reset" | "black" | "red" | "green" | "yellow" | "blue" | "purple" | "cyan" | "white") -> string,
|
2023-02-06 05:13:12 +00:00
|
|
|
--[=[
|
|
|
|
@within stdio
|
|
|
|
|
|
|
|
Return an ANSI string that can be used to modify the persistent output style.
|
|
|
|
|
|
|
|
Pass `"reset"` to get a string that can reset the persistent output style.
|
|
|
|
|
|
|
|
### Example usage
|
|
|
|
|
|
|
|
```lua
|
|
|
|
stdio.write(stdio.style("bold"))
|
|
|
|
print("This text will be bold")
|
|
|
|
stdio.write(stdio.style("reset"))
|
|
|
|
print("This text will be normal")
|
|
|
|
```
|
|
|
|
|
|
|
|
@param style The style to use
|
2023-02-06 18:10:52 +00:00
|
|
|
@return A printable ANSI string
|
2023-02-06 05:13:12 +00:00
|
|
|
]=]
|
2023-02-06 18:10:52 +00:00
|
|
|
style: (style: "reset" | "bold" | "dim") -> string,
|
2023-02-06 05:13:12 +00:00
|
|
|
--[=[
|
|
|
|
@within stdio
|
|
|
|
|
|
|
|
Formats arguments into a human-readable string with syntax highlighting for tables.
|
|
|
|
|
|
|
|
@param ... The values to format
|
|
|
|
@return The formatted string
|
|
|
|
]=]
|
|
|
|
format: (...any) -> (string),
|
|
|
|
--[=[
|
|
|
|
@within stdio
|
|
|
|
|
|
|
|
Writes a string directly to stdout, without any newline.
|
|
|
|
|
|
|
|
@param s The string to write to stdout
|
|
|
|
]=]
|
|
|
|
write: (s: string) -> (),
|
|
|
|
--[=[
|
|
|
|
@within stdio
|
|
|
|
|
|
|
|
Writes a string directly to stderr, without any newline.
|
|
|
|
|
|
|
|
@param s The string to write to stderr
|
|
|
|
]=]
|
|
|
|
ewrite: (s: string) -> (),
|
2023-02-06 17:59:48 +00:00
|
|
|
--[=[
|
|
|
|
@within stdio
|
|
|
|
|
|
|
|
Prompts for user input using the wanted kind of prompt:
|
|
|
|
|
|
|
|
* `"text"` - Prompts for a plain text string from the user
|
|
|
|
* `"confirm"` - Prompts the user to confirm with y / n
|
|
|
|
* `"select"` - Prompts the user to select *one* value from a list
|
|
|
|
* `"multiselect"` - Prompts the user to select *one or more* values from a list
|
|
|
|
* `nil` - Equivalent to `"text"` with no extra arguments
|
|
|
|
|
|
|
|
@param kind The kind of prompt to use
|
|
|
|
@param message The message to show the user
|
|
|
|
@param defaultOrOptions The default value for the prompt, or options to choose from for selection prompts
|
|
|
|
]=]
|
|
|
|
prompt: (
|
|
|
|
(() -> string)
|
|
|
|
& ((kind: "text", message: string?, defaultOrOptions: string?) -> string)
|
|
|
|
& ((kind: "confirm", message: string, defaultOrOptions: boolean?) -> boolean)
|
|
|
|
& ((kind: "select", message: string?, defaultOrOptions: { string }) -> number?)
|
|
|
|
& ((kind: "multiselect", message: string?, defaultOrOptions: { string }) -> { number }?)
|
|
|
|
),
|
2023-02-06 05:13:12 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@class task
|
|
|
|
|
|
|
|
Task scheduler & thread spawning
|
|
|
|
]=]
|
2023-01-23 04:00:09 +00:00
|
|
|
declare task: {
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within task
|
|
|
|
|
|
|
|
Stops a currently scheduled thread from resuming.
|
|
|
|
|
|
|
|
@param thread The thread to cancel
|
|
|
|
]=]
|
2023-01-24 00:13:18 +00:00
|
|
|
cancel: (thread: thread) -> (),
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within task
|
|
|
|
|
|
|
|
Defers a thread or function to run at the end of the current task queue.
|
|
|
|
|
|
|
|
@param functionOrThread The function or thread to defer
|
|
|
|
@return The thread that will be deferred
|
|
|
|
]=]
|
2023-01-24 00:13:18 +00:00
|
|
|
defer: <T...>(functionOrThread: thread | (T...) -> (...any), T...) -> thread,
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within task
|
|
|
|
|
|
|
|
Delays a thread or function to run after `duration` seconds.
|
|
|
|
|
|
|
|
@param functionOrThread The function or thread to delay
|
|
|
|
@return The thread that will be delayed
|
|
|
|
]=]
|
2023-01-24 00:13:18 +00:00
|
|
|
delay: <T...>(duration: number?, functionOrThread: thread | (T...) -> (...any), T...) -> thread,
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within task
|
|
|
|
|
|
|
|
Instantly runs a thread or function.
|
|
|
|
|
|
|
|
If the spawned task yields, the thread that spawned the task
|
|
|
|
will resume, letting the spawned task run in the background.
|
|
|
|
|
|
|
|
@param functionOrThread The function or thread to spawn
|
|
|
|
@return The thread that was spawned
|
|
|
|
]=]
|
2023-01-24 00:13:18 +00:00
|
|
|
spawn: <T...>(functionOrThread: thread | (T...) -> (...any), T...) -> thread,
|
2023-01-27 00:36:06 +00:00
|
|
|
--[=[
|
|
|
|
@within task
|
|
|
|
|
|
|
|
Waits for the given duration, with a minimum wait time of 10 milliseconds.
|
|
|
|
|
|
|
|
@param duration The amount of time to wait
|
|
|
|
@return The exact amount of time waited
|
|
|
|
]=]
|
2023-01-23 04:00:09 +00:00
|
|
|
wait: (duration: number?) -> (number),
|
|
|
|
}
|
2023-02-06 03:25:36 +00:00
|
|
|
|
|
|
|
-- TODO: Write docs for these and include them in docs gen
|
|
|
|
|
|
|
|
declare print: <T...>(T...) -> ()
|
|
|
|
declare info: <T...>(T...) -> ()
|
|
|
|
declare warn: <T...>(T...) -> ()
|
|
|
|
declare error: <T>(message: T, level: number?) -> never
|