Skip to content

Instantly share code, notes, and snippets.

@Sleitnick
Created June 8, 2025 16:07
Show Gist options
  • Save Sleitnick/b238500d973dfeb0d2ea765f9a286284 to your computer and use it in GitHub Desktop.
Save Sleitnick/b238500d973dfeb0d2ea765f9a286284 to your computer and use it in GitHub Desktop.
Take instance-require lines and convert them to string-require lines
-- Add lines to here & run script
local lines = [[
local Roact = require(script.Parent.Parent.Parent.Packages.Roact)
local RoactRodux = require(script.Parent.Parent.Parent.Packages.RoactRodux)
local Constants = require(script.Parent.Parent.Plugin.Constants)
local ThemeContext = require(script.Parent.ThemeContext)
]]
-- e.g. output from above:
--[[
local Roact = require("../../Packages/Roact")
local RoactRodux = require("../../Packages/RoactRodux")
local Constants = require("../Plugin/Constants")
local ThemeContext = require("./ThemeContext")
]]
local function transform(line: string)
local name, path = string.match(line, "local%s*(%w+)%s*=%s*require%((.+)%)$")
if (not name) or (not path) then
return line
end
local pathParts = string.split(path, ".")
local newPathParts = {}
if pathParts[1] == "script" then
local numParents = 0
for i = 2, #pathParts do
if pathParts[i] == "Parent" then
if numParents > 0 then
table.insert(newPathParts, "..")
end
numParents += 1
else
numParents = 0
table.insert(newPathParts, pathParts[i])
end
end
if newPathParts[1] ~= ".." then
table.insert(newPathParts, 1, ".")
end
local newPath = table.concat(newPathParts, "/")
return `local {name} = require("{newPath}")`
end
return line
end
local function transformAll(all: string)
local lines = string.split(all, "\n")
for i, line in lines do
if string.sub(line, #line) == "\r" then
line = string.sub(line, #line - 1)
end
lines[i] = transform(line)
end
return `\n\n{table.concat(lines, "\n")}\n`
end
print(transformAll(lines))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment