Skip to content

Commit

Permalink
Read local env file
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBarba committed May 1, 2023
1 parent 5a911e3 commit 535029b
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion Plugins/VercelPackager/VercelOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,13 @@ public struct VercelOutput {
print("")

Task {
var env = localEnvironment()
env["LOCAL_LAMBDA_SERVER_ENABLED"] = "true"

try Shell.execute(
executable: context.tool(named: "swift").path,
arguments: ["run", "--package-path", projectDirectory.string],
environment: ["LOCAL_LAMBDA_SERVER_ENABLED": "true"]
environment: env
)
}

Expand Down Expand Up @@ -151,6 +154,25 @@ extension VercelOutput {
}
return arguments[index + 1]
}

public func localEnvironment() -> [String: String] {
guard let data = fs.contents(atPath: projectDirectory.appending(".env").string) else {
return [:]
}
guard let text = String(data: data, encoding: .utf8) else {
return [:]
}
let lines = text.split(separator: "\n").map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
var env: [String: String] = [:]
for line in lines {
guard line.starts(with: "#") == false else { continue }
let parts = line.split(separator: "=", maxSplits: 1)
let key = String(parts[0]).trimmingCharacters(in: .whitespacesAndNewlines)
let value = parts[1].trimmingCharacters(in: .whitespacesAndNewlines).dropFirst().dropLast()
env[key] = String(value)
}
return env
}
}

// MARK: - Products
Expand Down

0 comments on commit 535029b

Please sign in to comment.