Minkyu Kim
Minkyu Kim

Reputation: 1154

How can I set properties conditionally in Node.js via Atom?

I am developing a Node.js app with Electron via Atom.

I want to set some properties conditionally(or automatically), for instance, the url should be http://some.url on production level.

Currently, I use like this.

//  win.loadURL('http://app.url/webchat'); //uncomment when production
win.loadURL('http://test.app.url/webchat'); // uncomment when development

This is very annoying me, and it can be a problem when I miss changing comments.

How can I change my properties conditionally with production/development level?

Upvotes: 0

Views: 153

Answers (2)

shashi
shashi

Reputation: 4696

I have a config directory with different config files for different environments: dev, test, prod. Then in my package.json I have added environment specific build commands. e.g. For prod:

"build-prod-config": "config/buildProdConfig.sh",
"build-renderer-prod": "webpack --config=webpack.config.renderer.prod.js",
"build-main-prod": "webpack --config=webpack.config.main.prod.js",
"build-prod": "npm run build-prod-config && npm run build-main-prod & npm run build-renderer-prod",

buildProdConfig.sh
#!/usr/bin/env bash

cp config/app.config.prod.js config/app.config.js
echo "Copied ProdConfig to Config"

//This is what a config file looks like
const Config = {
  suppDataDirectoryPath: '/suppData/',
  builtFor: 'prod',
}

module.exports = Config;

I then require Config whereever I need in my application and use the values. This is a simple thing for now, but it works.

Upvotes: 1

Datsik
Datsik

Reputation: 14824

if (process.env.DEV === "PROD") {
    win.loadURL('http://app.url/webchat');
} else {
    win.loadURL('http://test.app.url/webchat');
}

then when launching your app just do

DEV="PROD" node app.js or whatever

Upvotes: 1

Related Questions