Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

refactor: update manageEnvFiles function to handle multiple app directories and improve .env file management

vholikc6541c48b2cf
diff --git a/packages/cli/src/utils/manage-env-files.ts b/packages/cli/src/utils/manage-env-files.ts
index 4b356c49e..371726382 100644
--- a/packages/cli/src/utils/manage-env-files.ts
+++ b/packages/cli/src/utils/manage-env-files.ts
@@ -52,7 +52,12 @@ const sanitizeEnv = ({
return updatedEnv;
};
-// todo: comeback to this because we would have multiple folders for env files
+const APP_PATHS = [
+ { path: "apps/api", isApi: true },
+ { path: "apps/vendor", isApi: false },
+ { path: "apps/admin", isApi: false },
+];
+
export async function manageEnvFiles({
projectDir,
databaseUri,
@@ -60,41 +65,52 @@ export async function manageEnvFiles({
projectDir: string;
databaseUri?: string;
}): Promise<void> {
- const envExamplePath = path.join(projectDir, ".env.example");
- const envPath = path.join(projectDir, ".env");
-
- let exampleEnv = "";
-
try {
- // If there's a .env.example file, use it to create or update the .env file
- if (fs.existsSync(envExamplePath)) {
- const envExampleContents = await fs.readFile(envExamplePath, "utf8");
-
- exampleEnv = sanitizeEnv({
- contents: envExampleContents,
- databaseUri,
- });
- }
-
- // If there's no .env file, create it using the .env.example content (if it exists)
- if (!fs.existsSync(envPath)) {
- const envContent = sanitizeEnv({
- contents: exampleEnv,
- databaseUri,
- });
-
- await fs.writeFile(envPath, envContent);
- } else {
- // If the .env file already exists, sanitize it as-is
- const envContents = await fs.readFile(envPath, "utf8");
-
- const updatedEnvContents = sanitizeEnv({
- contents: envContents,
- databaseUri,
- });
-
- await fs.writeFile(envPath, updatedEnvContents);
- }
+ await Promise.all(
+ APP_PATHS.map(async ({ path: appPath, isApi }) => {
+ const appDir = path.join(projectDir, appPath);
+
+ if (!fs.existsSync(appDir)) {
+ return;
+ }
+
+ const envExamplePath = path.join(appDir, ".env.example");
+ const envPath = path.join(appDir, ".env");
+
+ // if not API, just copy the .env.example file
+ if (!isApi) {
+ if (fs.existsSync(envExamplePath) && !fs.existsSync(envPath)) {
+ await fs.copy(envExamplePath, envPath);
+ }
+ return;
+ }
+
+ let exampleEnv = "";
+
+ if (fs.existsSync(envExamplePath)) {
+ const envExampleContents = await fs.readFile(envExamplePath, "utf8");
+ exampleEnv = sanitizeEnv({
+ contents: envExampleContents,
+ databaseUri,
+ });
+ }
+
+ if (!fs.existsSync(envPath)) {
+ const envContent = sanitizeEnv({
+ contents: exampleEnv,
+ databaseUri,
+ });
+ await fs.writeFile(envPath, envContent);
+ } else {
+ const envContents = await fs.readFile(envPath, "utf8");
+ const updatedEnvContents = sanitizeEnv({
+ contents: envContents,
+ databaseUri,
+ });
+ await fs.writeFile(envPath, updatedEnvContents);
+ }
+ })
+ );
} catch (err: unknown) {
logger.error("Unable to manage environment files");
if (err instanceof Error) {