Backend API: Tauri Commands
Relevant source files
The following files were used as context for generating this wiki page:The backend of the Scripty application is a Rust binary powered by the Tauri framework. It exposes a set of commands that can be invoked from the JavaScript frontend. These commands provide the core functionality of the application, primarily focused on interacting with the local file system and executing PowerShell scripts with varying privilege levels. This architecture allows the web-based UI to perform powerful system-level operations securely.
The main logic for the backend, including command definitions and application setup, is located in src-tauri/src/lib.rs. The frontend components, such as DetailView.jsx and Form.jsx, use the @tauri-apps/api library to call these backend commands, forming the bridge between the user interface and the native capabilities of the operating system.
Application Initialization
The backend application starts from the main function in src-tauri/src/main.rs, which simply calls the run function from the scripttool_lib library crate.
The run function in src-tauri/src/lib.rs is responsible for building and running the Tauri application. It initializes necessary plugins and registers the backend commands that will be exposed to the frontend.
The following diagram illustrates the application startup and command registration flow.
This diagram shows the sequence from the main entry point to the running Tauri application with registered commands.
Sources: src-tauri/src/main.rs:4-6, src-tauri/src/lib.rs:65-77
The invoke_handler macro gathers all functions marked with #[tauri::command] and makes them available to the frontend.
Sources: src-tauri/src/lib.rs:71-76
Core Commands
The backend exposes several commands for file handling and script execution. These are defined as Rust functions and decorated with the #[tauri::command] attribute.
Command Summary
| Command | Description | Parameters | Returns |
|---|---|---|---|
read_ps1 | Reads the content of a .ps1 or .txt file from a given path. | path: String | Result<(String, String), String> |
open_terminal | Executes a PowerShell script with standard user privileges. | content: String | () |
open_admin_terminal | Executes a PowerShell script with administrator privileges. | content: String | () |
run_sys_terminal | Executes a PowerShell script as the SYSTEM user via an embedded utility. | content: Option<String> | Result<(), String> |
Sources: src-tauri/src/lib.rs:30-68
read_ps1
This command is used to read the contents of a script file selected by the user. It takes a file path as input and returns a tuple containing the original path and the file's string content.
#[tauri::command]
fn read_ps1(path: String) -> Result<(String, String), String> {
match fs::read_to_string(&path) {
Ok(content) => Ok((path, content)),
Err(_e) => Err(format!("Failed to resolve the .ps1 file")),
}
}
Sources: src-tauri/src/lib.rs:63-68
The frontend calls this command when a user selects a file to add to their script list.
The following sequence diagram shows the interaction:
This diagram shows the flow of reading a .ps1 file, from user action to backend processing and response.
Sources: src/components/Form/Form.jsx:8-30, src-tauri/src/lib.rs:63-68
Script Execution Commands
The application provides three distinct commands for running PowerShell scripts, each with a different level of privilege.
open_terminal (User Level)
This is the simplest execution command. It spawns a new powershell process and passes the script content to it, running with the current user's permissions.
#[tauri::command]
fn open_terminal(content: String) {
Command::new("powershell").arg(&content).spawn().unwrap();
}
Sources: src-tauri/src/lib.rs:30-33
open_admin_terminal (Administrator Level)
To execute a script with elevated privileges, this command uses PowerShell's Start-Process cmdlet with the -Verb RunAs argument. This prompts the user for administrator credentials via the standard Windows UAC dialog.
#[tauri::command]
fn open_admin_terminal(content: String) {
Command::new("powershell")
.args(&[
"-Command",
&format!(
"Start-Process powershell -ArgumentList '-NoExit','-Command','{}' -Verb RunAs",
content.replace("'", "''")
),
])
.spawn()
.expect("Failed to start elevated PowerShell");
}
Sources: src-tauri/src/lib.rs:36-46
run_sys_terminal (SYSTEM Level)
This command executes a script with SYSTEM-level privileges. It relies on a bundled executable, syspwsh.exe, which is embedded directly into the application binary.
The execution flow is as follows:
- The
extract_syspwshhelper function is called to write the embeddedsyspwsh.exeto the user's temporary directory if it doesn't already exist. - The extracted
syspwsh.exeis then executed as a new process, passing the script content to it via command-line arguments.
This diagram details the logic for extracting and running the SYSTEM-level PowerShell utility.
Sources: src-tauri/src/lib.rs:8-23, 49-61
Frontend Invocation
The React frontend, specifically in the DetailView.jsx component, invokes these execution commands based on the user's selection from a dropdown menu.
// src/components/DetailView/DetailView.jsx
async function chruntime(val) {
switch (val) {
case "1":
// The SYSTEM option is commented out in the UI, but the backend support exists.
await invoke("run_sys_terminal", { content: selectedItem.content });
break;
case "2":
await invoke("open_admin_terminal", { content: selectedItem.content });
break;
case "3":
await invoke("open_terminal", { content: selectedItem.content });
break;
}
}
Sources: src/components/DetailView/DetailView.jsx:12-26
Permissions and Capabilities
The ability for these commands to interact with the file system and spawn processes is granted through Tauri's capabilities configuration. The default.json capability file lists permissions that allow the application to perform these actions, such as fs:default and fs:allow-write-text-file. While process-spawning permissions are not explicitly listed, they are enabled by the inclusion of the tauri-plugin-process and the general security model.
Sources: src-tauri/capabilities/default.json:9-68, src-tauri/Cargo.toml:27
Conclusion
The Tauri command system is the backbone of Scripty, providing a secure and robust bridge between the React frontend and native OS functions. By defining specific, auditable commands in Rust, the application can safely execute powerful operations like reading local files and running PowerShell scripts with different privilege levels. This clear separation of concerns between the UI and the backend logic is a key architectural strength of the project.