Skip to content

Commit 41e5758

Browse files
ok
1 parent 4a26967 commit 41e5758

15 files changed

Lines changed: 20183 additions & 19570 deletions

File tree

‎.gitignore‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ yarn-error.log*
4040

4141
.smoke
4242

43-
.idea
43+
.idea
44+
45+
old-cli/

‎apps/cli/src/helpers/database-providers/d1-setup.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from "node:path";
33
import type { ProjectConfig } from "../../types";
44

55
import { addPackageDependency } from "../../utils/add-package-deps";
6-
import { addEnvVariablesToFile, type EnvVariable } from "../core/env-setup";
6+
import { addEnvVariablesToFile, type EnvVariable } from "../../utils/env-utils";
77

88
export async function setupCloudflareD1(config: ProjectConfig) {
99
const { projectDir, serverDeploy, orm, backend } = config;

‎apps/cli/src/helpers/database-providers/docker-compose-setup.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from "node:path";
22

33
import type { Database, ProjectConfig } from "../../types";
44

5-
import { addEnvVariablesToFile, type EnvVariable } from "../core/env-setup";
5+
import { addEnvVariablesToFile, type EnvVariable } from "../../utils/env-utils";
66

77
export async function setupDockerCompose(config: ProjectConfig) {
88
const { database, projectDir, projectName, backend } = config;

‎apps/cli/src/helpers/database-providers/mongodb-atlas-setup.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import pc from "picocolors";
88
import type { ProjectConfig } from "../../types";
99

1010
import { commandExists } from "../../utils/command-exists";
11+
import { addEnvVariablesToFile, type EnvVariable } from "../../utils/env-utils";
1112
import { exitCancelled } from "../../utils/errors";
12-
import { addEnvVariablesToFile, type EnvVariable } from "../core/env-setup";
1313

1414
type MongoDBConfig = {
1515
connectionString: string;

‎apps/cli/src/helpers/database-providers/neon-setup.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import pc from "picocolors";
77

88
import type { PackageManager, ProjectConfig } from "../../types";
99

10+
import { addEnvVariablesToFile, type EnvVariable } from "../../utils/env-utils";
1011
import { exitCancelled } from "../../utils/errors";
1112
import { getPackageExecutionArgs } from "../../utils/package-runner";
12-
import { addEnvVariablesToFile, type EnvVariable } from "../core/env-setup";
1313

1414
type NeonConfig = {
1515
connectionString: string;

‎apps/cli/src/helpers/database-providers/planetscale-setup.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from "node:path";
33

44
import type { ProjectConfig } from "../../types";
55

6-
import { addEnvVariablesToFile, type EnvVariable } from "../core/env-setup";
6+
import { addEnvVariablesToFile, type EnvVariable } from "../../utils/env-utils";
77

88
export async function setupPlanetScale(config: ProjectConfig) {
99
const { projectDir, database, orm, backend } = config;

‎apps/cli/src/helpers/database-providers/prisma-postgres-setup.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import pc from "picocolors";
77

88
import type { PackageManager, ProjectConfig } from "../../types";
99

10+
import { addEnvVariablesToFile, type EnvVariable } from "../../utils/env-utils";
1011
import { getPackageExecutionArgs } from "../../utils/package-runner";
11-
import { addEnvVariablesToFile, type EnvVariable } from "../core/env-setup";
1212

1313
type PrismaConfig = {
1414
databaseUrl: string;

‎apps/cli/src/helpers/database-providers/supabase-setup.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import pc from "picocolors";
77

88
import type { PackageManager, ProjectConfig } from "../../types";
99

10+
import { addEnvVariablesToFile, type EnvVariable } from "../../utils/env-utils";
1011
import { exitCancelled } from "../../utils/errors";
1112
import { getPackageExecutionArgs } from "../../utils/package-runner";
12-
import { addEnvVariablesToFile, type EnvVariable } from "../core/env-setup";
1313

1414
async function writeSupabaseEnvFile(
1515
projectDir: string,

‎apps/cli/src/helpers/database-providers/turso-setup.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import pc from "picocolors";
88
import type { ProjectConfig } from "../../types";
99

1010
import { commandExists } from "../../utils/command-exists";
11+
import { addEnvVariablesToFile, type EnvVariable } from "../../utils/env-utils";
1112
import { exitCancelled } from "../../utils/errors";
12-
import { addEnvVariablesToFile, type EnvVariable } from "../core/env-setup";
1313

1414
type TursoConfig = {
1515
dbUrl: string;

‎apps/cli/src/utils/env-utils.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import fs from "fs-extra";
2+
3+
export interface EnvVariable {
4+
key: string;
5+
value: string;
6+
condition?: boolean;
7+
}
8+
9+
export async function addEnvVariablesToFile(
10+
envPath: string,
11+
variables: EnvVariable[],
12+
): Promise<void> {
13+
let content = "";
14+
15+
if (fs.existsSync(envPath)) {
16+
content = await fs.readFile(envPath, "utf-8");
17+
} else {
18+
// If file doesn't exist, create it
19+
await fs.ensureFile(envPath);
20+
}
21+
22+
const existingLines = content.split("\n");
23+
const newLines: string[] = [];
24+
const keysToAdd = new Map<string, string>();
25+
26+
for (const variable of variables) {
27+
if (variable.condition === false || !variable.key) {
28+
continue;
29+
}
30+
keysToAdd.set(variable.key, variable.value);
31+
}
32+
33+
let foundKeys = new Set<string>();
34+
35+
for (const line of existingLines) {
36+
const trimmedLine = line.trim();
37+
let lineProcessed = false;
38+
39+
for (const [key, value] of keysToAdd) {
40+
if (trimmedLine.startsWith(`${key}=`)) {
41+
newLines.push(`${key}=${value}`);
42+
foundKeys.add(key);
43+
lineProcessed = true;
44+
break;
45+
}
46+
}
47+
48+
if (!lineProcessed) {
49+
newLines.push(line);
50+
}
51+
}
52+
53+
for (const [key, value] of keysToAdd) {
54+
if (!foundKeys.has(key)) {
55+
newLines.push(`${key}=${value}`);
56+
}
57+
}
58+
59+
// Remove empty line at the end if it exists
60+
if (newLines.length > 0 && newLines[newLines.length - 1] === "") {
61+
newLines.pop();
62+
}
63+
64+
const hasChanges = foundKeys.size > 0 || keysToAdd.size > foundKeys.size;
65+
66+
if (hasChanges) {
67+
await fs.writeFile(envPath, newLines.join("\n") + "\n");
68+
}
69+
}

0 commit comments

Comments
 (0)