MCPcopy
hub / github.com/opactorai/Claudable / syncDbToEnvFile

Function syncDbToEnvFile

lib/services/env.ts:159–199  ·  view source on GitHub ↗
(projectId: string)

Source from the content-addressed store, hash-verified

157}
158
159export async function syncDbToEnvFile(projectId: string): Promise<number> {
160 const project = await ensureProject(projectId);
161 const repoEnvPath = envFilePath(project);
162
163 const envVars = await prisma.envVar.findMany({
164 where: { projectId },
165 orderBy: { key: 'asc' },
166 });
167
168 const entries = envVars.reduce<{ key: string; value: string }[]>((acc, envVar) => {
169 try {
170 acc.push({ key: envVar.key, value: decrypt(envVar.valueEncrypted) });
171 } catch (error) {
172 console.warn(`[EnvService] Failed to decrypt env var ${envVar.key}:`, error);
173 }
174 return acc;
175 }, []);
176
177 const header =
178 '# Environment Variables\n# This file is automatically synchronized with Project Settings\n\n';
179
180 const contents =
181 header +
182 entries
183 .map(({ key, value }) => {
184 if (value === undefined || value === null) {
185 return `${key}=`;
186 }
187 if (/[ \t#"$']/u.test(value)) {
188 return `${key}="${value.replace(/"/g, '\\"')}"`;
189 }
190 return `${key}=${value}`;
191 })
192 .join('\n') +
193 (entries.length > 0 ? '\n' : '');
194
195 await fs.mkdir(path.dirname(repoEnvPath), { recursive: true });
196 await fs.writeFile(repoEnvPath, contents, 'utf8');
197
198 return entries.length;
199}
200
201function parseEnvFile(contents: string): Record<string, string> {
202 const result: Record<string, string> = {};

Callers 5

POSTFunction · 0.90
createEnvVarFunction · 0.85
updateEnvVarFunction · 0.85
deleteEnvVarFunction · 0.85
upsertEnvVarFunction · 0.85

Calls 3

decryptFunction · 0.90
ensureProjectFunction · 0.85
envFilePathFunction · 0.85

Tested by

no test coverage detected