@@ -3,7 +3,11 @@ use clap::crate_version;
33use std:: str:: FromStr ;
44
55/// Runs a Lua script.
6- pub async fn run_command ( file_path : String , extra_args : Option < Vec < String > > ) {
6+ pub async fn run_command (
7+ file_path : String ,
8+ stdlib_path : Option < String > ,
9+ extra_args : Option < Vec < String > > ,
10+ ) {
711 let lua = & LUA ;
812
913 // ! Move VM preparation into a separate function
@@ -19,7 +23,7 @@ pub async fn run_command(file_path: String, extra_args: Option<Vec<String>>) {
1923 . expect ( "Could not set the script path to OnceCell" ) ;
2024
2125 // Register Lua components.
22- registration ( lua) . await ;
26+ registration ( lua, stdlib_path ) . await ;
2327
2428 // Handle extra arguments.
2529 if let Some ( extra_args) = extra_args {
@@ -196,22 +200,48 @@ pub async fn upgrade_command(user_agent: Option<String>) -> Result<(), Box<dyn s
196200}
197201
198202/// Registers Lua components.
199- async fn registration ( lua : & mlua:: Lua ) {
203+ async fn registration ( lua : & mlua:: Lua , stdlib_path : Option < String > ) {
200204 let mut lua_lib: Vec < ( String , String ) > = Vec :: new ( ) ;
201205
202- let folder_path = if let Ok ( file) = tokio:: fs:: read_to_string ( ".luarc.json" ) . await
203- && let Ok ( parsed) = serde_json:: from_str :: < serde_json:: Value > ( & file)
204- && let Some ( parsed) = parsed. as_object ( )
205- && let Some ( parsed) = parsed. get ( "workspace.library" )
206- && let Some ( parsed) = parsed. as_array ( )
207- && let Some ( folder_path) = parsed. first ( )
208- && let Some ( folder_path) = folder_path. as_str ( )
209- {
210- folder_path. to_string ( )
206+ let folder_path = stdlib_path. unwrap_or (
207+ // get the folder path from .luarc.json
208+ // { "workspace.library": ["./folder_path"] }
209+ if let Ok ( file) = tokio:: fs:: read_to_string ( ".luarc.json" ) . await
210+ && let Ok ( parsed) = serde_json:: from_str :: < serde_json:: Value > ( & file)
211+ && let Some ( parsed) = parsed. as_object ( )
212+ && let Some ( parsed) = parsed. get ( "workspace.library" )
213+ && let Some ( parsed) = parsed. as_array ( )
214+ && let Some ( folder_path) = parsed. first ( )
215+ && let Some ( folder_path) = folder_path. as_str ( )
216+ {
217+ folder_path. to_string ( )
218+ } else {
219+ ".astra" . to_string ( )
220+ } ,
221+ ) ;
222+ if let Ok ( mut files) = tokio:: fs:: read_dir ( folder_path) . await {
223+ // add them to the lua_lib for being sent to interpretation
224+ #[ allow( for_loops_over_fallibles) ]
225+ for file in files. next_entry ( ) . await {
226+ if let Some ( file) = file
227+ && let Ok ( content) = tokio:: fs:: read_to_string ( file. path ( ) ) . await
228+ {
229+ lua_lib. push ( ( file. path ( ) . to_string_lossy ( ) . to_string ( ) , content) ) ;
230+ }
231+ }
211232 } else {
212- ".astra" . to_string ( )
213- } ;
214- if let Ok ( files) = tokio:: fs:: read_dir ( folder_path) . await { }
233+ // if the folder couldn't be opened or issues existed
234+ lua_lib = prepare_prelude ( )
235+ }
236+
237+ // Try to make astra.lua the first to get interpreted
238+ if let Some ( index) = lua_lib. iter ( ) . position ( |entry| {
239+ let name = entry. 0 . to_ascii_lowercase ( ) ;
240+ name == "astra.lua"
241+ } ) {
242+ let value = lua_lib. remove ( index) ;
243+ lua_lib. insert ( 0 , value) ;
244+ }
215245
216246 for ( file_name, content) in lua_lib {
217247 if let Err ( e) = lua
0 commit comments