diff --git a/lib/init.luau b/lib/init.luau index 0034841..3ac397f 100644 --- a/lib/init.luau +++ b/lib/init.luau @@ -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 diff --git a/stylua.toml b/stylua.toml new file mode 100644 index 0000000..88b2e63 --- /dev/null +++ b/stylua.toml @@ -0,0 +1,3 @@ +indent_type = "Spaces" +indent_width = 2 + diff --git a/tests/load.test.luau b/tests/load.test.luau index da68204..7e78243 100644 --- a/tests/load.test.luau +++ b/tests/load.test.luau @@ -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) diff --git a/tests/parse.test.luau b/tests/parse.test.luau index 48ddf5e..7c95262 100644 --- a/tests/parse.test.luau +++ b/tests/parse.test.luau @@ -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)