Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for parsing environment variables #207

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/git-extractor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 17 additions & 3 deletions packages/import-utils/src/create-sandbox/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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[];
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -160,6 +173,7 @@ export default async function createSandbox(
modules,
directories,
externalResources: [],
environmentVariables: getEnvironmentVariables(directory),
template,
entry: mainFile,
v2: isCloudTemplate(template),
Expand Down
1 change: 1 addition & 0 deletions packages/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export interface ISandbox {
externalResources: string[];
template: ITemplate;
entry: string;
environmentVariables: Record<string, string>;
v2?: boolean;
templateParams?: {
iconUrl?: string;
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading