Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Commit

Permalink
fix: styling
Browse files Browse the repository at this point in the history
  • Loading branch information
Eggflaw committed May 31, 2024
1 parent 6bd5043 commit ef8633e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 86 deletions.
114 changes: 57 additions & 57 deletions lib/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -4,76 +4,76 @@ local fs = require("@lune/fs")
local process = require("@lune/process")

function dotenv:parse(str: string): { [string?]: string? }
-- TODO: Parse dotenv and convert it into a table
local env: { [string?]: string? } = {}
-- TODO: Parse dotenv and convert it into a table
local env: { [string?]: string? } = {}

local multiline = false
local multilineKey = ""
local multilineValue = ""
for line in str:gmatch("([^\n]+)\n") do
local key, value = line:match("(.+)=(.+)")
local multiline = false
local multilineKey = ""
local multilineValue = ""
for line in str:gmatch("([^\n]+)\n") do
local key, value = line:match("(.+)=(.+)")

if key and value then
local firstChar = value:sub(1, 1)
local lastChar = value:sub(#value, #value)
if firstChar == '"' then
-- [[
-- #value == 1 is for keys that start with this
-- KEY="
-- FOO
-- BAR
-- "
-- otherwise it will return "\""
-- ]]
if lastChar ~= '"' or #value == 1 then
multiline = true
end
end
if key and value then
local firstChar = value:sub(1, 1)
local lastChar = value:sub(#value, #value)
if firstChar == '"' then
-- [[
-- #value == 1 is for keys that start with this
-- KEY="
-- FOO
-- BAR
-- "
-- otherwise it will return "\""
-- ]]
if lastChar ~= '"' or #value == 1 then
multiline = true
end
end

if not multiline then
value = value:gsub('^"(.*)"$', "%1")
value = value:gsub("^'(.*)'$", "%1")
if not multiline then
value = value:gsub('^"(.*)"$', "%1")
value = value:gsub("^'(.*)'$", "%1")

env[key] = value
else
value = value:gsub('^"(.*)', "%1")
multilineKey = key
multilineValue = value
end
elseif multiline then
local lastChar = line:sub(#line, #line)
local escapeChar = line:sub(#line - 1, #line - 1)
if lastChar == '"' and escapeChar ~= "\\" then
line = line:gsub('(.*)"$', "%1")
multilineValue = multilineValue .. "\n" .. line
env[key] = value
else
value = value:gsub('^"(.*)', "%1")
multilineKey = key
multilineValue = value
end
elseif multiline then
local lastChar = line:sub(#line, #line)
local escapeChar = line:sub(#line - 1, #line - 1)
if lastChar == '"' and escapeChar ~= "\\" then
line = line:gsub('(.*)"$', "%1")
multilineValue = multilineValue .. "\n" .. line

env[multilineKey] = multilineValue
env[multilineKey] = multilineValue

multiline = false
multilineKey = ""
multilineValue = ""
continue
end
multilineValue = multilineValue .. "\n" .. line
end
end
multiline = false
multilineKey = ""
multilineValue = ""
continue
end
multilineValue = multilineValue .. "\n" .. line
end
end

return env
return env
end

function dotenv:load(overwrite: boolean?, path: string?)
-- TODO: call parse() and add it into process.env
local target = path or ".env"
-- TODO: call parse() and add it into process.env
local target = path or ".env"

local file = fs.readFile(target)
local file = fs.readFile(target)

local env = dotenv:parse(file)
local env = dotenv:parse(file)

for key, value in env do
if not process.env[key] or overwrite then
process.env[key] = value
end
end
for key, value in env do
if not process.env[key] or overwrite then
process.env[key] = value
end
end
end

return dotenv
3 changes: 3 additions & 0 deletions stylua.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
indent_type = "Spaces"
indent_width = 2

40 changes: 20 additions & 20 deletions tests/load.test.luau
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ local process = require("@lune/process")
local dotenv = require("../lib")

describe("Populate process.env", function()
test("Normal", function()
dotenv:load()
assertEq(process.env.SIMPLE, "ABC")
assertEq(process.env.DOUBLE, "DEF")
assertEq(process.env.SINGLE, "GHI")
end)
test("Normal", function()
dotenv:load()
assertEq(process.env.SIMPLE, "ABC")
assertEq(process.env.DOUBLE, "DEF")
assertEq(process.env.SINGLE, "GHI")
end)

test("Overwrite TRUE", function()
process.env.OVERWRITE = "FOO"
dotenv:load(true)
assertEq(process.env.OVERWRITE, "BAR")
end)
test("Overwrite TRUE", function()
process.env.OVERWRITE = "FOO"
dotenv:load(true)
assertEq(process.env.OVERWRITE, "BAR")
end)

test("Overwrite FALSE", function()
process.env.OVERWRITE = "FOO"
dotenv:load(false)
assertEq(process.env.OVERWRITE, "FOO")
end)
test("Overwrite FALSE", function()
process.env.OVERWRITE = "FOO"
dotenv:load(false)
assertEq(process.env.OVERWRITE, "FOO")
end)

test("Custom Path", function()
dotenv:load(false, "tests/envs/path.env")
assertEq(process.env.CUSTOM_PATH, "THIS IS A CUSTOM PATH")
end)
test("Custom Path", function()
dotenv:load(false, "tests/envs/path.env")
assertEq(process.env.CUSTOM_PATH, "THIS IS A CUSTOM PATH")
end)
end)
18 changes: 9 additions & 9 deletions tests/parse.test.luau
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ local fs = require("@lune/fs")
local dotenv = require("../lib")

local function readEnv(path)
local content = fs.readFile(path)
local env = dotenv:parse(content)
return env
local content = fs.readFile(path)
local env = dotenv:parse(content)
return env
end

describe("Multiline", function()
test("Normal", function()
local env = readEnv("tests/envs/multiline.env")
test("Normal", function()
local env = readEnv("tests/envs/multiline.env")

assertEq(env.NORMAL, "BAR\nFOO\nBAZ\n")
assertEq(env.TEST_2, "\nBAR\nFOO\nBAZ\n")
assertEq(env.WITH_ESC, 'FOO\nBAR\\"\nBAZ\n')
end)
assertEq(env.NORMAL, "BAR\nFOO\nBAZ\n")
assertEq(env.TEST_2, "\nBAR\nFOO\nBAZ\n")
assertEq(env.WITH_ESC, 'FOO\nBAR\\"\nBAZ\n')
end)
end)

0 comments on commit ef8633e

Please sign in to comment.