Skip to content

Commit

Permalink
chore(local-setup): setup .env file for the ui folder (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
enudler committed Oct 7, 2020
1 parent 0fe396e commit 481c1da
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions setup-env.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { readFileSync, writeFileSync } = require('fs');
const { networkInterfaces } = require('os');
const { networkInterfaces, EOL } = require('os');
const shell = require('shelljs');
const path = require('path');
const envFileName = '.env';
const defaultPort = 3000;
let port, ipAddress;
Expand All @@ -9,11 +10,12 @@ console.log('\x1b[36m%s\x1b[0m', 'Starting Predator local setup, good luck!'); /

npmInstallAndBuild();
extractIpAndPort();
setupEnvFile();
setupBackendEnvFile();
setupFrontendEnvFile();

console.log('\x1b[36m%s\x1b[0m', 'To start predator backend + frontend:\nnpm run start-local');
console.log('\x1b[36m%s\x1b[0m', `To start predator backend + frontend:${EOL}npm run start-local`);
console.log();
console.log('\x1b[36m%s\x1b[0m', `To develop frontend with hot reload:\ncd ui\nPREDATOR_URL=http//${ipAddress}:${port}/v1 npm start`);
console.log('\x1b[36m%s\x1b[0m', `To develop frontend with hot reload:${EOL}cd ui${EOL}npm start`);

function npmInstallAndBuild() {
if (shell.exec('npm install').code !== 0) {
Expand Down Expand Up @@ -51,7 +53,22 @@ function extractIpAndPort() {
port = process.env.PORT;
}
}
function setupEnvFile() {
function setupFrontendEnvFile() {
const envFilePath = path.join('ui', envFileName);
let envFile = '';
try {
envFile = readFileSync(envFilePath, 'utf8');
} catch (error) {
console.log(`${envFileName} does not exists, will create one`);
}
let newEnv = envFile;
newEnv = newEnv.replace(/^PREDATOR_URL.*\n?/m, '');

newEnv += `PREDATOR_URL=http://${ipAddress}:${port}/v1\n`;
console.log(`Updating ${envFileName} file with:\n${newEnv}`);
writeFileSync(envFilePath, newEnv);
}
function setupBackendEnvFile() {
let envFile = '';
try {
envFile = readFileSync(envFileName, 'utf8');
Expand Down

0 comments on commit 481c1da

Please sign in to comment.