scripts/.lune/lib/exec.luau

124 lines
2.4 KiB
Text
Raw Normal View History

2024-12-08 17:42:05 +00:00
--> lib: Builder pattern class to spawn child processes
local process = require("@lune/process")
local stdio = require("@lune/stdio")
local CommandBuilder = {}
2024-12-08 18:30:38 +00:00
export type CommandBuilder = typeof(setmetatable(
{} :: CommandBuilderFields,
{ __index = CommandBuilder }
))
2024-12-08 17:42:05 +00:00
type CommandBuilderFields = {
2024-12-08 18:30:38 +00:00
program: string,
args: { string },
stdioStrategy: IoStrategyMapping?,
2024-12-08 17:42:05 +00:00
}
export type StdioStrategy = "pipe" | "forward" | "none"
export type IoStrategyMapping = {
2024-12-08 18:30:38 +00:00
stdout: StdioStrategy?,
stderr: StdioStrategy?,
2024-12-08 17:42:05 +00:00
}
local DEFAULT_STDIO_STRATEGY: IoStrategyMapping = {
2024-12-08 18:30:38 +00:00
stdout = "pipe",
stderr = "pipe",
2024-12-08 17:42:05 +00:00
}
function CommandBuilder.new(program: string)
2024-12-08 18:30:38 +00:00
return setmetatable(
{
program = program,
args = {},
stdioStrategy = nil,
} :: CommandBuilderFields,
{
__index = CommandBuilder,
}
)
2024-12-08 17:42:05 +00:00
end
2024-12-08 18:30:38 +00:00
function CommandBuilder.withArg(
self: CommandBuilder,
arg: string
): CommandBuilder
table.insert(self.args, arg)
return self
2024-12-08 17:42:05 +00:00
end
2024-12-08 18:30:38 +00:00
function CommandBuilder.withArgs(
self: CommandBuilder,
args: { string }
): CommandBuilder
for _, arg in args do
self:withArg(arg)
end
2024-12-08 17:42:05 +00:00
2024-12-08 18:30:38 +00:00
return self
2024-12-08 17:42:05 +00:00
end
function CommandBuilder.withStdioStrategy(
2024-12-08 18:30:38 +00:00
self: CommandBuilder,
strategy: StdioStrategy | IoStrategyMapping
2024-12-08 17:42:05 +00:00
): CommandBuilder
2024-12-08 18:30:38 +00:00
self.stdioStrategy = if typeof(strategy) == "string"
then {
stdout = strategy,
stderr = strategy,
}
else strategy
return self
2024-12-08 17:42:05 +00:00
end
2024-12-08 18:30:38 +00:00
local function intoSpawnOptionsStdioKind(
strategy: StdioStrategy
): process.SpawnOptionsStdioKind
if strategy == "pipe" then
return "default"
end
2024-12-08 17:42:05 +00:00
2024-12-08 18:30:38 +00:00
if strategy == "forward" then
return "forward"
end
2024-12-08 17:42:05 +00:00
2024-12-08 18:30:38 +00:00
if strategy == "none" then
return "none"
end
2024-12-08 17:42:05 +00:00
2024-12-08 18:30:38 +00:00
error(`Non-strategy provided: {strategy}`)
2024-12-08 17:42:05 +00:00
end
function CommandBuilder.exec(self: CommandBuilder): process.SpawnResult
2024-12-08 18:30:38 +00:00
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
2024-12-08 17:42:05 +00:00
end
return CommandBuilder