feat(cmd): reduce init boilerplate in minify & bundle wrappers

This commit is contained in:
Erica Marigold 2023-09-26 19:38:10 +05:30
parent 0ff7bcd36c
commit 9b34155267
No known key found for this signature in database
GPG key ID: 7843994FD1386E35

View file

@ -54,31 +54,7 @@ local function ProcessWhich(binName: string): { path: string?, warnings: { strin
}
end
Darklua.Type = "Darklua"
Darklua.Interface = {}
Darklua.Prototype = {}
function Darklua.Prototype.ToString(self: DarkluaExtended): string
return string.format("%s<%s>", Darklua.Type, "Global")
end
function Darklua.Prototype.IsOk(self: DarkluaExtended): boolean
if self.darkluaPath and Fs.isFile(self.darkluaPath) then
local isExecutable = Process.spawn(self.darkluaPath, { "--help" }, { stdio = "default" }).ok
return isExecutable
end
return false
end
function Darklua.Prototype.Process(
self: DarkluaExtended,
sourceKind: "code" | "path",
source: string,
configPath: string?
): { error: string?, processed: string? }
function __DarkluaInit(sourceKind: "code" | "path", source: string): { res: { string }?, error: string? }
local sourcePath: string
if sourceKind == "code" then
@ -109,6 +85,50 @@ function Darklua.Prototype.Process(
}
end
-- [1] => sourcePath
-- [2] => outFilePath
return {
res = { sourcePath, outFilePath },
}
end
Darklua.Type = "Darklua"
Darklua.Interface = {}
Darklua.Prototype = {}
function Darklua.Prototype.ToString(self: DarkluaExtended): string
return string.format("%s<%s>", Darklua.Type, "Global")
end
function Darklua.Prototype.IsOk(self: DarkluaExtended): boolean
if self.darkluaPath and Fs.isFile(self.darkluaPath) then
local isExecutable = Process.spawn(self.darkluaPath, { "--help" }, { stdio = "default" }).ok
return isExecutable
end
return false
end
function Darklua.Prototype.Process(
self: DarkluaExtended,
sourceKind: "code" | "path",
source: string,
configPath: string?
): { error: string?, processed: string? }
local initOut = __DarkluaInit(sourceKind, source)
if initOut.error then
return {
error = initOut.error,
}
end
local paths = initOut.res :: { string }
local sourcePath = paths[1]
local outFilePath = paths[2]
local args = { "--config" }
if configPath and configPath ~= "" then
@ -137,36 +157,17 @@ function Darklua.Prototype.Minify(
sourceKind: "code" | "path",
source: string
): { error: string?, minified: string? }
local sourcePath: string
if sourceKind == "code" then
local ok, tmpFilePath = FsUtils.MakeTemp()
if not ok then
return {
error = "failed to create temporary file for darklua minification",
}
end
Fs.writeFile(tmpFilePath, source)
sourcePath = tmpFilePath
elseif sourceKind == "path" then
sourcePath = source
else
local initOut = __DarkluaInit(sourceKind, source)
if initOut.error then
return {
error = "invalid sourceKind provided",
error = initOut.error,
}
end
local ok, outFilePath = FsUtils.MakeTemp()
if not ok then
return {
error = "failed to create temporary file for darklua minification",
}
end
local paths = initOut.res :: { string }
local sourcePath = paths[1]
local outFilePath = paths[2]
local darkluaChild = Process.spawn(self.darkluaPath, { "minify", sourcePath, outFilePath })
if not darkluaChild.ok then