webhook-config.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const fs = require('fs');
  2. const path = require('path');
  3. const { outputsRoot } = require('./output-paths');
  4. function webhookDir() {
  5. return path.join(outputsRoot(), 'webhook');
  6. }
  7. function ensureWebhookDir() {
  8. fs.mkdirSync(webhookDir(), { recursive: true });
  9. }
  10. function configPath() {
  11. ensureWebhookDir();
  12. return path.join(webhookDir(), 'webhook-config.json');
  13. }
  14. function readConfig() {
  15. const filePath = configPath();
  16. if (!fs.existsSync(filePath)) return { callbackUrl: null, secret: null, autoCreateGroup: true };
  17. try {
  18. return { callbackUrl: null, secret: null, autoCreateGroup: true, ...JSON.parse(fs.readFileSync(filePath, 'utf8')) };
  19. } catch {
  20. return { callbackUrl: null, secret: null, autoCreateGroup: true };
  21. }
  22. }
  23. function writeConfig(config) {
  24. ensureWebhookDir();
  25. fs.writeFileSync(configPath(), JSON.stringify({ ...readConfig(), ...config, updatedAt: new Date().toISOString() }, null, 2), 'utf8');
  26. }
  27. function relayConfigPath() {
  28. ensureWebhookDir();
  29. return path.join(webhookDir(), 'relay-config.json');
  30. }
  31. function readRelayConfig() {
  32. const filePath = relayConfigPath();
  33. if (!fs.existsSync(filePath)) return {};
  34. try {
  35. return JSON.parse(fs.readFileSync(filePath, 'utf8'));
  36. } catch {
  37. return {};
  38. }
  39. }
  40. function writeRelayConfig(config) {
  41. ensureWebhookDir();
  42. fs.writeFileSync(relayConfigPath(), JSON.stringify({ ...readRelayConfig(), ...config, updatedAt: new Date().toISOString() }, null, 2), 'utf8');
  43. }
  44. module.exports = {
  45. readConfig,
  46. writeConfig,
  47. readRelayConfig,
  48. writeRelayConfig
  49. };