chore(lune): restructure util libraries and include dev scripts

* Moved all util libs like `channel` into a `lib/` directory within
  `.lune`. Also includes a builder-pattern exec lib.
* Added dev scripts for fmt (stylua) and typecheck (luau-lsp).
This commit is contained in:
Erica Marigold 2024-12-04 05:49:01 +00:00
parent 9fc9e44522
commit ca5f2d53c8
Signed by: DevComp
GPG key ID: 429EF1C337871656
7 changed files with 131 additions and 1 deletions

7
.lune/fmt.luau Normal file
View file

@ -0,0 +1,7 @@
--> Run stylua to check for formatting errors
local process = require("@lune/process")
local CommandBuilder = require("./lib/exec")
process.exit(CommandBuilder.new("stylua"):withArg("."):withArgs(process.args):withStdioStrategy("forward"):exec().code)

103
.lune/lib/exec.luau Normal file
View file

@ -0,0 +1,103 @@
--> lib: Builder pattern class to spawn child processes
local process = require("@lune/process")
local stdio = require("@lune/stdio")
local CommandBuilder = {}
export type CommandBuilder = typeof(setmetatable({} :: CommandBuilderFields, { __index = CommandBuilder }))
type CommandBuilderFields = {
program: string,
args: { string },
stdioStrategy: IoStrategyMapping?,
}
export type StdioStrategy = "pipe" | "forward" | "none"
export type IoStrategyMapping = {
stdout: StdioStrategy?,
stderr: StdioStrategy?,
}
local DEFAULT_STDIO_STRATEGY: IoStrategyMapping = {
stdout = "pipe",
stderr = "pipe",
}
function CommandBuilder.new(program: string)
return setmetatable(
{
program = program,
args = {},
stdioStrategy = nil,
} :: CommandBuilderFields,
{
__index = CommandBuilder,
}
)
end
function CommandBuilder.withArg(self: CommandBuilder, arg: string): CommandBuilder
table.insert(self.args, arg)
return self
end
function CommandBuilder.withArgs(self: CommandBuilder, args: { string }): CommandBuilder
for _, arg in args do
self:withArg(arg)
end
return self
end
function CommandBuilder.withStdioStrategy(
self: CommandBuilder,
strategy: StdioStrategy | IoStrategyMapping
): CommandBuilder
self.stdioStrategy = if typeof(strategy) == "string"
then {
stdout = strategy,
stderr = strategy,
}
else strategy
return self
end
local function intoSpawnOptionsStdioKind(strategy: StdioStrategy): process.SpawnOptionsStdioKind
if strategy == "pipe" then
return "default"
end
if strategy == "forward" then
return "forward"
end
if strategy == "none" then
return "none"
end
error(`Non-strategy provided: {strategy}`)
end
function CommandBuilder.exec(self: CommandBuilder): process.SpawnResult
print("$", stdio.style("dim") .. self.program, table.concat(self.args, " ") .. stdio.style("reset"))
local function translateIoStrategyMappings(mappings: IoStrategyMapping)
local translatedMappings: process.SpawnOptionsStdio = {}
for field, value in mappings do
translatedMappings[field] = intoSpawnOptionsStdioKind(value)
end
return translatedMappings
end
local child = process.spawn(self.program, self.args, {
shell = true,
stdio = translateIoStrategyMappings(self.stdioStrategy or DEFAULT_STDIO_STRATEGY),
})
if not child.ok then
print(`\n{stdio.color("red")}[luau-lsp]{stdio.color("reset")} Exited with code`, child.code)
end
return child
end
return CommandBuilder

View file

@ -1,3 +1,5 @@
--> Generates a Rojo sync config from a list of input files
local function enter(fn: (args: { string }) -> number?): never
local process = require("@lune/process")
local stdio = require("@lune/stdio")

View file

@ -1,3 +1,5 @@
--> Generates a Rojo sourcemap for a provided project directory
local function enter(fn: (args: { string }) -> number?): never
local process = require("@lune/process")
local stdio = require("@lune/stdio")

View file

@ -3,7 +3,7 @@ local stdio = require("@lune/stdio")
local frktest = require("../../lune_packages/frktest")
local Reporter = frktest._reporters.lune_console_reporter
local watch = require("./channel")
local watch = require("../lib/channel")
local STYLE = table.freeze({
suite = function(name: string)

16
.lune/typecheck.luau Normal file
View file

@ -0,0 +1,16 @@
--> Run luau-lsp analysis to check for type errors
local process = require("@lune/process")
local CommandBuilder = require("./lib/exec")
process.exit(
CommandBuilder.new("~/.rokit/bin/luau-lsp")
:withArg("analyze")
:withArgs({ "--settings", ".vscode/settings.json" })
:withArgs({ "--ignore", "'**/.pesde/**'" })
:withArgs({ "--ignore", "'./test-files/**'" })
:withArg(".")
:withStdioStrategy("forward")
:exec().code
)