Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions scripts/setup-env-files.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');

const prePopulateEnv = (apps, folderBasePath, exampleEnvFilePath = 'src/.example.env', envFilePath = 'src/.env') => {
const ENCRYPTION_KEY_PLACEHOLDER = '<ENCRYPTION_KEY_MUST_BE_32_LONG>';

const generateEncryptionKey = () => crypto.randomBytes(16).toString('hex');

const prePopulateEnv = (apps, folderBasePath, exampleEnvFilePath = 'src/.example.env', envFilePath = 'src/.env', sharedEncryptionKey) => {
console.log(`Pre-populating .env files from .example.env for [${apps.join(',')}]`);
for (const folder of apps) {
const exists = fs.existsSync(path.resolve(`${folderBasePath}/${folder}/${envFilePath}`));
const destPath = path.resolve(`${folderBasePath}/${folder}/${envFilePath}`);
const exists = fs.existsSync(destPath);
if (!exists) {
console.log(`Populating ${folderBasePath}/${folder} with .env file`);
fs.copyFileSync(
path.resolve(`${folderBasePath}/${folder}/${exampleEnvFilePath}`),
path.resolve(`${folderBasePath}/${folder}/${envFilePath}`)
);
const sourcePath = path.resolve(`${folderBasePath}/${folder}/${exampleEnvFilePath}`);
let content = fs.readFileSync(sourcePath, 'utf8');
if (content.includes(ENCRYPTION_KEY_PLACEHOLDER) && sharedEncryptionKey) {
content = content.replace(ENCRYPTION_KEY_PLACEHOLDER, sharedEncryptionKey);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace only substitutes first placeholder occurrence

Low Severity

content.replace(ENCRYPTION_KEY_PLACEHOLDER, sharedEncryptionKey) only replaces the first occurrence of the placeholder. If an .example.env file ever contains the <ENCRYPTION_KEY_MUST_BE_32_LONG> placeholder on more than one line, subsequent occurrences will be left as literal placeholder text, silently breaking those env vars. Using replaceAll (or split/join) would match the intent of replacing every occurrence.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 1acbf11. Configure here.

console.log(` Generated STORE_ENCRYPTION_KEY for ${folder}`);
}
fs.writeFileSync(destPath, content);
}
}
};

(async () => {
const appsBasePath = `${__dirname}/../apps`;
const sharedEncryptionKey = generateEncryptionKey();
console.log('----------------------------------------');
prePopulateEnv(['api', 'ws', 'worker'], appsBasePath);
prePopulateEnv(['dashboard'], appsBasePath, '.example.env', '.env');
prePopulateEnv(['api', 'ws', 'worker'], appsBasePath, 'src/.example.env', 'src/.env', sharedEncryptionKey);
prePopulateEnv(['dashboard'], appsBasePath, '.example.env', '.env', sharedEncryptionKey);
console.log('Finished populating .env files');
console.log('----------------------------------------');
})();
Loading