setup.mjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { readFileSync, writeFileSync, existsSync } from "fs";
  2. import { resolve, dirname } from "path";
  3. import { fileURLToPath } from "url";
  4. const __dirname = dirname(fileURLToPath(import.meta.url));
  5. const root = resolve(__dirname, "..");
  6. function loadEnv() {
  7. const envPath = resolve(root, ".env");
  8. if (!existsSync(envPath)) return {};
  9. const env = {};
  10. for (const line of readFileSync(envPath, "utf-8").split("\n")) {
  11. const trimmed = line.trim();
  12. if (!trimmed || trimmed.startsWith("#")) continue;
  13. const idx = trimmed.indexOf("=");
  14. if (idx === -1) continue;
  15. const key = trimmed.slice(0, idx).trim();
  16. const val = trimmed.slice(idx + 1).trim();
  17. env[key] = val;
  18. }
  19. return env;
  20. }
  21. const env = loadEnv();
  22. const templatePath = resolve(root, "stripe-app.template.json");
  23. const outputPath = resolve(root, "stripe-app.json");
  24. const template = JSON.parse(readFileSync(templatePath, "utf-8"));
  25. if (env.STRIPE_APP_ID) {
  26. template.id = env.STRIPE_APP_ID;
  27. console.log(`Using app ID from .env: ${env.STRIPE_APP_ID}`);
  28. } else {
  29. console.log(`No STRIPE_APP_ID in .env, using template default: ${template.id}`);
  30. }
  31. if (env.STRIPE_APP_NAME) {
  32. template.name = env.STRIPE_APP_NAME;
  33. }
  34. writeFileSync(outputPath, JSON.stringify(template, null, 4) + "\n");
  35. console.log("Generated stripe-app.json");