From c820b7ff1403b7a42cacb32bbd10d94d2e669019 Mon Sep 17 00:00:00 2001 From: Ives van Hoorne Date: Thu, 2 May 2024 17:14:57 +0000 Subject: [PATCH] feat: add support for parsing environment variables --- packages/git-extractor/package.json | 1 + .../import-utils/src/create-sandbox/index.ts | 20 ++++++++++++++++--- packages/types/index.d.ts | 1 + yarn.lock | 5 +++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/git-extractor/package.json b/packages/git-extractor/package.json index 26196ee8..23db5a4e 100644 --- a/packages/git-extractor/package.json +++ b/packages/git-extractor/package.json @@ -61,6 +61,7 @@ "babylon": "^6.18.0", "base-64": "^0.1.0", "debug": "^2.6.8", + "envfile": "^7.1.0", "gitignore-parser": "^0.0.2", "humps": "CompuIves/humps", "istextorbinary": "^6.0.0", diff --git a/packages/import-utils/src/create-sandbox/index.ts b/packages/import-utils/src/create-sandbox/index.ts index ac014024..3eed4f5b 100644 --- a/packages/import-utils/src/create-sandbox/index.ts +++ b/packages/import-utils/src/create-sandbox/index.ts @@ -5,6 +5,7 @@ import { ITemplate, } from "codesandbox-import-util-types"; import denormalize from "../utils/files/denormalize"; +import { parse as parseEnv } from "envfile"; import parseHTML from "./html-parser"; import { getMainFile, getTemplate } from "./templates"; @@ -71,9 +72,7 @@ function isCloudTemplate(template: ITemplate): boolean { return CLOUD_TEMPLATES.indexOf(template) > -1; } -function getSandboxMetadata( - directory: INormalizedModules -): { +function getSandboxMetadata(directory: INormalizedModules): { title: string; description: string; tags: string[]; @@ -108,6 +107,20 @@ function getSandboxMetadata( return packageJsonInfo; } +/** + * Gets the prefilled environment variables by parsing either /.env.example + * or /.env. + */ +function getEnvironmentVariables(directory: INormalizedModules) { + const envFile = directory[".env"] || directory[".env.example"]; + + if (!envFile || envFile.type !== "file") { + return {}; + } + + return parseEnv(envFile.content); +} + /** * Creates all relevant data for create a sandbox, like dependencies and which * files are in a sandbox @@ -160,6 +173,7 @@ export default async function createSandbox( modules, directories, externalResources: [], + environmentVariables: getEnvironmentVariables(directory), template, entry: mainFile, v2: isCloudTemplate(template), diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index b33e8414..7f229727 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -73,6 +73,7 @@ export interface ISandbox { externalResources: string[]; template: ITemplate; entry: string; + environmentVariables: Record; v2?: boolean; templateParams?: { iconUrl?: string; diff --git a/yarn.lock b/yarn.lock index 78df908f..7e6440f4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3719,6 +3719,11 @@ env-paths@^2.2.0: resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-2.2.1.tgz#420399d416ce1fbe9bc0a07c62fa68d67fd0f8f2" integrity sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A== +envfile@^7.1.0: + version "7.1.0" + resolved "https://registry.yarnpkg.com/envfile/-/envfile-7.1.0.tgz#c0b101279dc710c25546602d5d17cfb9ab132e48" + integrity sha512-dyH4QnnZsArCLhPASr29eqBWDvKpq0GggQFTmysTT/S9TTmt1JrEKNvTBc09Cd7ujVZQful2HBGRMe2agu7Krg== + envinfo@7.8.1: version "7.8.1" resolved "https://registry.yarnpkg.com/envinfo/-/envinfo-7.8.1.tgz#06377e3e5f4d379fea7ac592d5ad8927e0c4d475"