|
| 1 | +const crypto = require('crypto'); |
1 | 2 | const fs = require('fs'); |
2 | 3 | const path = require('path'); |
3 | 4 |
|
4 | | -const prePopulateEnv = (apps, folderBasePath, exampleEnvFilePath = 'src/.example.env', envFilePath = 'src/.env') => { |
| 5 | +const ENCRYPTION_KEY_PLACEHOLDER = '<ENCRYPTION_KEY_MUST_BE_32_LONG>'; |
| 6 | + |
| 7 | +const generateEncryptionKey = () => crypto.randomBytes(16).toString('hex'); |
| 8 | + |
| 9 | +const prePopulateEnv = (apps, folderBasePath, exampleEnvFilePath = 'src/.example.env', envFilePath = 'src/.env', sharedEncryptionKey) => { |
5 | 10 | console.log(`Pre-populating .env files from .example.env for [${apps.join(',')}]`); |
6 | 11 | for (const folder of apps) { |
7 | | - const exists = fs.existsSync(path.resolve(`${folderBasePath}/${folder}/${envFilePath}`)); |
| 12 | + const destPath = path.resolve(`${folderBasePath}/${folder}/${envFilePath}`); |
| 13 | + const exists = fs.existsSync(destPath); |
8 | 14 | if (!exists) { |
9 | 15 | console.log(`Populating ${folderBasePath}/${folder} with .env file`); |
10 | | - fs.copyFileSync( |
11 | | - path.resolve(`${folderBasePath}/${folder}/${exampleEnvFilePath}`), |
12 | | - path.resolve(`${folderBasePath}/${folder}/${envFilePath}`) |
13 | | - ); |
| 16 | + const sourcePath = path.resolve(`${folderBasePath}/${folder}/${exampleEnvFilePath}`); |
| 17 | + let content = fs.readFileSync(sourcePath, 'utf8'); |
| 18 | + if (content.includes(ENCRYPTION_KEY_PLACEHOLDER) && sharedEncryptionKey) { |
| 19 | + content = content.replace(ENCRYPTION_KEY_PLACEHOLDER, sharedEncryptionKey); |
| 20 | + console.log(` Generated STORE_ENCRYPTION_KEY for ${folder}`); |
| 21 | + } |
| 22 | + fs.writeFileSync(destPath, content); |
14 | 23 | } |
15 | 24 | } |
16 | 25 | }; |
17 | 26 |
|
18 | 27 | (async () => { |
19 | 28 | const appsBasePath = `${__dirname}/../apps`; |
| 29 | + const sharedEncryptionKey = generateEncryptionKey(); |
20 | 30 | console.log('----------------------------------------'); |
21 | | - prePopulateEnv(['api', 'ws', 'worker'], appsBasePath); |
22 | | - prePopulateEnv(['dashboard'], appsBasePath, '.example.env', '.env'); |
| 31 | + prePopulateEnv(['api', 'ws', 'worker'], appsBasePath, 'src/.example.env', 'src/.env', sharedEncryptionKey); |
| 32 | + prePopulateEnv(['dashboard'], appsBasePath, '.example.env', '.env', sharedEncryptionKey); |
23 | 33 | console.log('Finished populating .env files'); |
24 | 34 | console.log('----------------------------------------'); |
25 | 35 | })(); |
0 commit comments