47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import * as webpush from 'web-push';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
|
|
const envPath = path.join(__dirname, '..', '.env');
|
|
|
|
function generateVapidKeys(): void {
|
|
console.log('Generating VAPID keys...');
|
|
|
|
const vapidKeys = webpush.generateVAPIDKeys();
|
|
|
|
let envContent = '';
|
|
|
|
if (fs.existsSync(envPath)) {
|
|
envContent = fs.readFileSync(envPath, 'utf-8');
|
|
}
|
|
|
|
const lines = envContent.split('\n');
|
|
|
|
const vapidSubject = `VAPID_SUBJECT=mailto:contact@placebo.mk`;
|
|
const vapidPublicKey = `VAPID_PUBLIC_KEY=${vapidKeys.publicKey}`;
|
|
const vapidPrivateKey = `VAPID_PRIVATE_KEY=${vapidKeys.privateKey}`;
|
|
|
|
const updatedLines = lines.filter(
|
|
(line) =>
|
|
!line.startsWith('VAPID_SUBJECT=') &&
|
|
!line.startsWith('VAPID_PUBLIC_KEY=') &&
|
|
!line.startsWith('VAPID_PRIVATE_KEY='),
|
|
);
|
|
|
|
const nonEmptyLines = updatedLines.filter((line) => line.trim() !== '');
|
|
|
|
const newEnvContent =
|
|
nonEmptyLines.join('\n') +
|
|
(nonEmptyLines.length > 0 ? '\n' : '') +
|
|
`${vapidSubject}\n${vapidPublicKey}\n${vapidPrivateKey}\n`;
|
|
|
|
fs.writeFileSync(envPath, newEnvContent);
|
|
|
|
console.log('VAPID keys generated and added to .env file:');
|
|
console.log(` Public Key: ${vapidKeys.publicKey}`);
|
|
console.log(` Private Key: ${vapidKeys.privateKey}`);
|
|
console.log('\nKeep your private key secret!');
|
|
}
|
|
|
|
generateVapidKeys();
|