Strengthened typechecking and assertions. Added types for modules

This commit is contained in:
2025-09-23 23:22:41 -04:00
parent 10d2d6f027
commit 95ff0c5164
4 changed files with 46 additions and 18 deletions
+7 -1
View File
@@ -4,7 +4,13 @@ local stdio = require("@lune/stdio")
local fs = require("@lune/fs") local fs = require("@lune/fs")
local types = require("./types") local types = require("./types")
local cli = {} export type CLI = {
parseArgs: () -> types.opts,
printUsage: () -> (),
checkOutputFile: (outputFile: string?) -> ()
}
local cli = {} :: CLI
function cli.parseArgs(): types.opts function cli.parseArgs(): types.opts
local args = process.args local args = process.args
+19 -5
View File
@@ -1,13 +1,22 @@
-- handles core detection logic for workspace in models --[[ handles core detection logic for workspace in models ]]
local roblox = require("@lune/roblox") local roblox = require("@lune/roblox")
local stdio = require("@lune/stdio") local stdio = require("@lune/stdio")
local serde = require("@lune/serde") local serde = require("@lune/serde")
local types = require("./types") local types = require("./types")
local core = {} export type Core = {
scanForWorkspace: (model: {Instance}, printInstanceNames: boolean) -> boolean,
isValidModelFile: (fileName: string) -> boolean,
fileContainsWorkspace: (fileContents: string, opts: types.opts) -> boolean,
formatResult: (result: boolean, fileName: string) -> string
}
local core = {} :: Core
-- recursively searches a model for a workspace instance -- recursively searches a model for a workspace instance
function core.scanForWorkspace(model: types.model, printInstanceNames: boolean): boolean function core.scanForWorkspace(model: types.model, printInstanceNames: boolean): boolean
assert(typeof(model) == "table", "Expected model to be of type 'table''")
assert(typeof(printInstanceNames) == "boolean", "Expected printInstanceNames to be of type 'boolean'")
for _, child in pairs(model) do for _, child in pairs(model) do
if printInstanceNames then if printInstanceNames then
print(child:GetFullName()) print(child:GetFullName())
@@ -21,12 +30,15 @@ end
-- checks if file has valid extension -- checks if file has valid extension
function core.isValidModelFile(fileName: string): boolean function core.isValidModelFile(fileName: string): boolean
assert(typeof(fileName) == "string", "Expected fileName to be of type 'string'")
local ext = string.match(fileName, "%.([^%.]+)$") local ext = string.match(fileName, "%.([^%.]+)$")
return ext == "rbxm" or ext == "rbxmx" return ext == "rbxm" or ext == "rbxmx"
end end
-- takes in fileContents as a string and deserializes them returning the results of scanForWorkspace() on the deserialized model. if it can't be deserialized, it will return the results of a naive search through the xml -- takes in fileContents as a string and deserializes them returning the results of scanForWorkspace() on the deserialized model. if it can't be deserialized, it will return the results of a naive search through the xml
function core.fileContainsWorkspace(fileContents: string, opts: types.opts): boolean function core.fileContainsWorkspace(fileContents: string, opts: types.opts): boolean
assert(typeof(fileContents) == "string", "Expected fileContents to be of type 'string'")
assert(typeof(opts) == "table", "Expected opts to be of type 'table'")
if opts.zlibDecompressFiles then if opts.zlibDecompressFiles then
local success = pcall(function() fileContents = serde.decompress("zlib", fileContents) end) local success = pcall(function() fileContents = serde.decompress("zlib", fileContents) end)
if not success then if not success then
@@ -43,6 +55,8 @@ function core.fileContainsWorkspace(fileContents: string, opts: types.opts): boo
end end
function core.formatResult(result: boolean, fileName: string): string function core.formatResult(result: boolean, fileName: string): string
assert(typeof(result) == "boolean", "Expected result to be of type 'boolean'")
assert(typeof(fileName) == "string", "Expected fileName to be of type 'string'")
if result then if result then
return stdio.color("green") .. `File {fileName} contains a Workspace instance.` .. stdio.color("reset") return stdio.color("green") .. `File {fileName} contains a Workspace instance.` .. stdio.color("reset")
else else
+11 -5
View File
@@ -6,9 +6,16 @@ local serde = require("@lune/serde")
local core = require("./core") local core = require("./core")
local types = require("./types") local types = require("./types")
local fileproc = {} export type FileProc = {
collectFiles: (opts: types.opts) -> {string},
processFiles: (filesToProcess: {string}, opts: types.opts) -> {string},
zlibDecompress: (contents: string, recursive: boolean?) -> string | boolean
}
local fileproc = {} :: FileProc
function fileproc.collectFiles(opts: types.opts): {string} function fileproc.collectFiles(opts: types.opts): {string}
assert(typeof(opts) == "table", "Expected opts to be of type 'table'")
local filesToProcess = opts.filesToProcess or {} local filesToProcess = opts.filesToProcess or {}
if opts.directoryMode then if opts.directoryMode then
if not opts.directoryPath or not fs.isDir(opts.directoryPath) then if not opts.directoryPath or not fs.isDir(opts.directoryPath) then
@@ -47,6 +54,8 @@ function fileproc.collectFiles(opts: types.opts): {string}
end end
function fileproc.processFiles(filesToProcess: {string}, opts: types.opts): {string} function fileproc.processFiles(filesToProcess: {string}, opts: types.opts): {string}
assert(typeof(filesToProcess) == "table", "Expected filesToProcess to be of type 'table'")
assert(typeof(opts) == "table", "Expected opts to be of type 'table'")
local filesWithWorkspace = {} local filesWithWorkspace = {}
for _, filePath in pairs(filesToProcess) do for _, filePath in pairs(filesToProcess) do
if fs.isFile(filePath) then if fs.isFile(filePath) then
@@ -83,10 +92,7 @@ end
-- if recursive param is true repeatedly zlib decompress the file until it fails -- if recursive param is true repeatedly zlib decompress the file until it fails
-- (modelscrape files are often double/triple zlib decompressed) -- (modelscrape files are often double/triple zlib decompressed)
function fileproc.zlibDecompress(contents: string, recursive: boolean?): string | boolean function fileproc.zlibDecompress(contents: string, recursive: boolean?): string | boolean
local success: boolean, decompressedContents: string = pcall(function() error("fileproc.zlibDecompress is not implemented.")
return serde.decompress("zlib", contents)
end)
return success and decompressedContents or success
end end
return fileproc return fileproc
+8 -6
View File
@@ -2,13 +2,15 @@
local types = {} local types = {}
export type opts = {outputFile: string?, export type opts = {
directoryMode: boolean, outputFile: string?,
directoryPath: string?, directoryMode: boolean,
forceBinaryRead: boolean, directoryPath: string?,
printInstanceNames: boolean, forceBinaryRead: boolean,
printInstanceNames: boolean,
zlibDecompressFiles: boolean, zlibDecompressFiles: boolean,
filesToProcess: {string?}} filesToProcess: {string?}
}
export type model = {Instance} export type model = {Instance}