UNPKG

@vxrn/takeout-cli

Version:

CLI tools for Takeout starter kit - interactive onboarding and project setup

91 lines (89 loc) 2.7 kB
import { randomBytes } from "node:crypto"; import { copyFileSync, existsSync, readFileSync, writeFileSync } from "node:fs"; import { join } from "node:path"; function generateSecret(length = 32) { return randomBytes(length).toString("hex"); } function envFileExists(cwd, filename = ".env") { return existsSync(join(cwd, filename)); } function copyEnvFile(cwd, source, target) { const sourcePath = join(cwd, source), targetPath = join(cwd, target); if (!existsSync(sourcePath)) return { success: !1, error: `Source file ${source} not found` }; if (existsSync(targetPath)) return { success: !1, error: `Target file ${target} already exists` }; try { return copyFileSync(sourcePath, targetPath), { success: !0 }; } catch (error) { return { success: !1, error: error instanceof Error ? error.message : "Unknown error" }; } } function updateEnvVariable(cwd, key, value, filename = ".env") { const envPath = join(cwd, filename); if (!existsSync(envPath)) return { success: !1, error: `${filename} not found` }; try { let content = readFileSync(envPath, "utf-8"); const keyRegex = new RegExp(`^${key}=.*$`, "m"); return keyRegex.test(content) ? content = content.replace(keyRegex, `${key}=${value}`) : content = content.trimEnd() + ` ${key}=${value} `, writeFileSync(envPath, content, "utf-8"), { success: !0 }; } catch (error) { return { success: !1, error: error instanceof Error ? error.message : "Unknown error" }; } } function createEnvLocal(cwd) { const envLocalPath = join(cwd, ".env.local"); if (existsSync(envLocalPath)) return { success: !0 }; const template = `# Local environment overrides # This file is gitignored and never committed # Add your personal secrets and local configuration here # These values override .env # Example: # BETTER_AUTH_SECRET=your-secret-here # AWS_ACCESS_KEY_ID=your-key-here `; try { return writeFileSync(envLocalPath, template, "utf-8"), { success: !0 }; } catch (error) { return { success: !1, error: error instanceof Error ? error.message : "Unknown error" }; } } function readEnvVariable(cwd, key, filename = ".env") { const envPath = join(cwd, filename); if (!existsSync(envPath)) return null; try { const content = readFileSync(envPath, "utf-8"), keyRegex = new RegExp(`^${key}=(.*)$`, "m"); return content.match(keyRegex)?.[1]?.trim().replace(/^["']|["']$/g, "") || null; } catch { return null; } } export { copyEnvFile, createEnvLocal, envFileExists, generateSecret, readEnvVariable, updateEnvVariable }; //# sourceMappingURL=env.mjs.map