61 lines
1.6 KiB
JavaScript
61 lines
1.6 KiB
JavaScript
const fs = require('fs');
|
|
const webPush = require('web-push');
|
|
const crypto = require('crypto');
|
|
const readline = require('readline');
|
|
|
|
// Create a readline interface
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout,
|
|
});
|
|
|
|
// Wrap the question function in a Promise, so we can use async/await
|
|
const question = (prompt, defaultValue) =>
|
|
new Promise((resolve) => {
|
|
rl.question(`${prompt} (default: ${defaultValue}): `, (answer) => {
|
|
// Use the default value if no answer was provided
|
|
resolve(answer || defaultValue);
|
|
});
|
|
});
|
|
|
|
const setup = async () => {
|
|
const devOrigin = await question(
|
|
'Enter the dev origin',
|
|
'http://localhost:5137',
|
|
);
|
|
const prodOrigin = await question(
|
|
'Enter the production origin',
|
|
'https://prod.site.this.api.connects.to.com',
|
|
);
|
|
|
|
const vapidEmail = await question(
|
|
'Enter email address for VAPID keys',
|
|
'example@gmail.com',
|
|
);
|
|
|
|
// Generate secret
|
|
const secret = crypto.randomBytes(32).toString('hex');
|
|
|
|
// Generate VAPID keys
|
|
const vapidKeys = webPush.generateVAPIDKeys();
|
|
|
|
// Create .env content
|
|
const envContent =
|
|
`SECRET=${secret}\n` +
|
|
`VAPID_EMAIL=${vapidEmail}\n` +
|
|
`VAPID_PUBLIC_KEY=${vapidKeys.publicKey}\n` +
|
|
`VAPID_PRIVATE_KEY=${vapidKeys.privateKey}\n` +
|
|
`DEV_ORIGIN=${devOrigin}\n` +
|
|
`PROD_ORIGIN=${prodOrigin}\n`;
|
|
|
|
// Write to .env
|
|
fs.writeFileSync('.env', envContent);
|
|
|
|
// Close the readline interface
|
|
rl.close();
|
|
|
|
console.log('Secrets generated and written to .env');
|
|
};
|
|
|
|
setup();
|