jecs-nightly/src/shared.luau

47 lines
1.3 KiB
Text

--!strict
local datetime = require("@lune/datetime")
local fs = require("@lune/fs")
local net = require("@lune/net")
local result = require("./util/result")
--- Fetches the given file raw from the jecs github
local function fetch_raw(file: string): result.Identity<string>
local res = net.request(`https://raw.githubusercontent.com/Ukendio/jecs/refs/heads/main/{file}`)
if not res.ok then
return result(false, `Not ok: {res.statusMessage} @ {res.statusCode}`)
end
return result(true, res.body)
end
local function save_if_diff(filepath: string, contents: string): result.Identity<nil>
if not fs.metadata(filepath).exists then
fs.writeFile(filepath, contents)
return result(true, nil)
end
local existing = fs.readFile(filepath)
if existing == contents then
return result(false, "Contents are the same.")
end
fs.writeFile(filepath, contents)
return result(true, nil)
end
local function round_to(n: number, places: number)
local x = 10 ^ (places or 0)
return math.round(n * x) / x
end
-- Returns an ISO 8601 date (YYYYmmddThhmmssZ)
local function iso_date_light(date: datetime.DateTime): string
return date:formatUniversalTime("%Y%m%dT%H%M%SZ")
end
return {
fetch_raw = fetch_raw,
save_if_diff = save_if_diff,
round_to = round_to,
iso_date_light = iso_date_light,
}