mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 04:50:36 +00:00
Add support for init files
This commit is contained in:
parent
594e773236
commit
65f2319a64
7 changed files with 49 additions and 5 deletions
|
@ -8,6 +8,13 @@ All notable changes to this project will be documented in this file.
|
|||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
## Unreleased
|
||||
|
||||
### Added
|
||||
|
||||
- Added support for `init` files in directories, similar to Rojo, or `index.js` or `mod.rs` in JavaScript / Rust. <br/>
|
||||
This means that placing a file named `init.luau` or `init.luau` in a directory will now let you `require` that directory.
|
||||
|
||||
## `0.7.1` - June 17th, 2023
|
||||
|
||||
### Added
|
||||
|
|
|
@ -133,11 +133,20 @@ impl<'lua> RequireContext<'lua> {
|
|||
) {
|
||||
(Ok(luau), _) => luau,
|
||||
(_, Ok(lua)) => lua,
|
||||
_ => {
|
||||
return Err(LuaError::RuntimeError(format!(
|
||||
"File does not exist at path '{require_path}'"
|
||||
)))
|
||||
}
|
||||
// If we did not find a luau/lua file at the wanted path,
|
||||
// we should also look for "init" files in directories
|
||||
_ => match (
|
||||
canonicalize(path_relative_to_pwd.join("init").with_extension("luau")),
|
||||
canonicalize(path_relative_to_pwd.join("init").with_extension("lua")),
|
||||
) {
|
||||
(Ok(luau), _) => luau,
|
||||
(_, Ok(lua)) => lua,
|
||||
_ => {
|
||||
return Err(LuaError::RuntimeError(format!(
|
||||
"File does not exist at path '{require_path}'"
|
||||
)))
|
||||
}
|
||||
},
|
||||
};
|
||||
let absolute = file_path.to_string_lossy().to_string();
|
||||
let relative = absolute.trim_start_matches(&self.pwd).to_string();
|
||||
|
|
|
@ -70,6 +70,7 @@ create_tests! {
|
|||
require_async_sequential: "require/tests/async_sequential",
|
||||
require_builtins: "require/tests/builtins",
|
||||
require_children: "require/tests/children",
|
||||
require_directories: "require/tests/directories",
|
||||
require_invalid: "require/tests/invalid",
|
||||
require_nested: "require/tests/nested",
|
||||
require_parents: "require/tests/parents",
|
||||
|
|
4
tests/require/modules/init.luau
Normal file
4
tests/require/modules/init.luau
Normal file
|
@ -0,0 +1,4 @@
|
|||
return {
|
||||
Foo = "Bar",
|
||||
Hello = "World",
|
||||
}
|
15
tests/require/tests/directories.luau
Normal file
15
tests/require/tests/directories.luau
Normal file
|
@ -0,0 +1,15 @@
|
|||
local module = require("./modules")
|
||||
|
||||
assert(type(module) == "table", "Required module did not return a table")
|
||||
assert(module.Foo == "Bar", "Required module did not contain correct values")
|
||||
assert(module.Hello == "World", "Required module did not contain correct values")
|
||||
|
||||
module = require("modules")
|
||||
assert(module.Foo == "Bar", "Required module did not contain correct values")
|
||||
assert(module.Hello == "World", "Required module did not contain correct values")
|
||||
|
||||
module = require("modules/modules")
|
||||
assert(module.Foo == "Bar", "Required module did not contain correct values")
|
||||
assert(module.Hello == "World", "Required module did not contain correct values")
|
||||
|
||||
return true
|
4
tests/require/tests/modules/init.luau
Normal file
4
tests/require/tests/modules/init.luau
Normal file
|
@ -0,0 +1,4 @@
|
|||
return {
|
||||
Foo = "Bar",
|
||||
Hello = "World",
|
||||
}
|
4
tests/require/tests/modules/modules/init.luau
Normal file
4
tests/require/tests/modules/modules/init.luau
Normal file
|
@ -0,0 +1,4 @@
|
|||
return {
|
||||
Foo = "Bar",
|
||||
Hello = "World",
|
||||
}
|
Loading…
Reference in a new issue