Add stricter type checking to newly created modules

This commit is contained in:
2025-09-23 22:33:36 -04:00
parent 7f21bbc27d
commit 10d2d6f027
4 changed files with 38 additions and 9 deletions
+3 -2
View File
@@ -2,10 +2,11 @@
local process = require("@lune/process") local process = require("@lune/process")
local stdio = require("@lune/stdio") local stdio = require("@lune/stdio")
local fs = require("@lune/fs") local fs = require("@lune/fs")
local types = require("./types")
local cli = {} local cli = {}
function cli.parseArgs() function cli.parseArgs(): types.opts
local args = process.args local args = process.args
local opts = { local opts = {
outputFile = nil, outputFile = nil,
@@ -76,7 +77,7 @@ function cli.printUsage()
stdio.write(stdio.color("reset")) stdio.write(stdio.color("reset"))
end end
function cli.checkOutputFile(outputFile) function cli.checkOutputFile(outputFile: string?)
if outputFile and fs.isFile(outputFile) then if outputFile and fs.isFile(outputFile) then
stdio.write(stdio.color("red")) stdio.write(stdio.color("red"))
stdio.write(`Error: Output file {outputFile} already exists. Will not overwrite.\n`) stdio.write(`Error: Output file {outputFile} already exists. Will not overwrite.\n`)
+5 -4
View File
@@ -2,11 +2,12 @@
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 core = {} local core = {}
-- recursively searches a model for a workspace instance -- recursively searches a model for a workspace instance
function core.scanForWorkspace(model, printInstanceNames) function core.scanForWorkspace(model: types.model, printInstanceNames: boolean): 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())
@@ -19,13 +20,13 @@ function core.scanForWorkspace(model, printInstanceNames)
end end
-- checks if file has valid extension -- checks if file has valid extension
function core.isValidModelFile(fileName) function core.isValidModelFile(fileName: string): boolean
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, opts) function core.fileContainsWorkspace(fileContents: string, opts: types.opts): boolean
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
@@ -41,7 +42,7 @@ function core.fileContainsWorkspace(fileContents, opts)
return core.scanForWorkspace(instances, opts.printInstanceNames) return core.scanForWorkspace(instances, opts.printInstanceNames)
end end
function core.formatResult(result, fileName) function core.formatResult(result: boolean, fileName: string): 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
+15 -3
View File
@@ -2,14 +2,16 @@
local fs = require("@lune/fs") local fs = require("@lune/fs")
local stdio = require("@lune/stdio") local stdio = require("@lune/stdio")
local process = require("@lune/process") local process = require("@lune/process")
local serde = require("@lune/serde")
local core = require("./core") local core = require("./core")
local types = require("./types")
local fileproc = {} local fileproc = {}
function fileproc.collectFiles(opts) function fileproc.collectFiles(opts: types.opts): {string}
local filesToProcess = opts.filesToProcess or {} local filesToProcess = opts.filesToProcess or {}
if opts.directoryMode then if opts.directoryMode then
if not fs.isDir(opts.directoryPath) then if not opts.directoryPath or not fs.isDir(opts.directoryPath) then
stdio.write(stdio.color("red")) stdio.write(stdio.color("red"))
stdio.write(`Error: Directory {opts.directoryPath} does not exist.\n`) stdio.write(`Error: Directory {opts.directoryPath} does not exist.\n`)
stdio.write(stdio.color("reset")) stdio.write(stdio.color("reset"))
@@ -44,7 +46,7 @@ function fileproc.collectFiles(opts)
return filesToProcess return filesToProcess
end end
function fileproc.processFiles(filesToProcess, opts) function fileproc.processFiles(filesToProcess: {string}, opts: types.opts): {string}
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
@@ -77,4 +79,14 @@ function fileproc.processFiles(filesToProcess, opts)
return filesWithWorkspace return filesWithWorkspace
end end
-- take in file contents and zlib decompress them
-- if recursive param is true repeatedly zlib decompress the file until it fails
-- (modelscrape files are often double/triple zlib decompressed)
function fileproc.zlibDecompress(contents: string, recursive: boolean?): string | boolean
local success: boolean, decompressedContents: string = pcall(function()
return serde.decompress("zlib", contents)
end)
return success and decompressedContents or success
end
return fileproc return fileproc
+15
View File
@@ -0,0 +1,15 @@
-- custom type definitions
local types = {}
export type opts = {outputFile: string?,
directoryMode: boolean,
directoryPath: string?,
forceBinaryRead: boolean,
printInstanceNames: boolean,
zlibDecompressFiles: boolean,
filesToProcess: {string?}}
export type model = {Instance}
return types