Comment/Uncomment code based on condition - javascript

I have been using the gulp-remove-code plugin to remove the specific code from the source files, when I need to change the environment from staging to production or vice-versa. Most of the changed code is API keys and boolean flags. This is for JS, Python, and Yaml files.
The problem with the approach is that, I need to keep the original file at a separate place, since it removes the other environment code.
Instead of removing the code based on a variable, I want to comment or uncomment code, so that I can do an in-place gulp.dest() without having a separate file.
Is there any way to do it via 'gulp' or using the 'gulp-remove-code' plugin or any other plugin?

I use npm module config for that purposes.
All you have to do is:
set NODE_ENV env variable
add config files with the same names as NODE_ENV variable
set NODE_CONFIG_DIR env variable
This approach is very convenient.

Related

Sails object not available in log.js

Im building a custom logger using Winston in Sails. I would like to set a log level variable in all of my various environment configs and reference that variable in the log.js file. This seems to work in my controllers with a reference of sails.config.variableName but the same reference in log.js throws: Details:ReferenceError: sails is not defined Can someone tell me how to reference this variable from the config? Is there some sort of require statement that I can add?
I would rather not set the level with some sort of switch/conditional that references the env variable used to start up the application in the log.js (ie. process.env.myEnvironment)
The best way to handle this, even though you may not like it, would be to pass it in as an ENVIRONMENT VARIABLE to Sails at startup instead of using the env/{name}.js files. log.js loads before the full Sails object and you can't reference it. I recommend using:
process.env.{your variable name}
ex: process.env.LOG_LEVEL
Where logLevel is passed to the sails.js app at startup using:
>> LOG_LEVEL=info NODE_ENV=dev node app.js
In this scenario both LOG_LEVEL and NODE_ENV are available to you in the log.js file.
But it's not a "switch" as much as it's an environment specific setting. Externalizing environment specific entities is actually a common practice and recommended for cloud environments where you can alter the values without having to redeploy code.
When running in containers you can easily change the log level from DEBUG to INFO and cycle your containers without redeploying your code. You then switch it back the same way without any downtime.

Conditional javascript source code

Background:
I have 3 different URLs, one per environment (dev, test, prod), and I don't want to expose all the URLs in the client (source code).
How can I expose in the client code, just the one corresponding to the environment in context?
Note: As I understand, I need to do something in the build process using environment variables (I'm using node.js). However, I don't want to touch anything related with webpack, as what I'm trying to do is a standalone package that can be imported in any application regardless of the framework they are using. Webpack plugins/configuration are not an option, but I can use any npm package if required.
During your build process, you can check the environment variable and then copy over a config file. For example, you could keep your URIs in /config/<env>.js, and then copy/rename it to /settings.js during the build. Your URL could be exported from that.
The following npm package fits my requirements completely https://www.npmjs.com/package/config , you can load conditional files based on the node environment variable NODE_ENV, so when NODE_ENV=development, the file /config/development.js is used to create the build. you can use different extensions for the config files, also you can customize the config folder path by changing the environment variable $NODE_CONFIG_DIR heres an example:
const config = require('config');
process.env.$NODE_CONFIG_DIR = './' // relative path ./config
const url = config.get('url');
//if NODE_ENV is development will load the file config/development.js
console.log(url);

Update configuration file with Ant

I'm not looking for a full working solution for my question, but for a recommendation on how to achieve something.
I have a web application with a javascript file which contains some constants and default values.
I'm using ant to build and package the application, so I have 2 targets, one for development and another one for production. The thing is, in my configuration file some constants have a default value for production but another one in development.
Right now, if I'm developing and I want to prepare a delivery for production I have to:
- manually edit configuration file to set right values (comment some lines and uncomment some others)
- run ant production target
I'd like to automate this, so when I run my ant target for production, the correct default values are used in my configuration javascript file. Can ant achieve this (search for some lines in a javascript file and edit that file)? is this a bad practice? any other way to do this?
As a suggestion, I would have two configuration files -dev and -prod and then use ant to move the right file to the build dir, renaming it if needed
You can write to a file using the echo task
https://ant.apache.org/manual/Tasks/echo.html
Replace Task is a directory based task for replacing the occurrence of a given string with another string in selected file.
https://ant.apache.org/manual/Tasks/replace.html

Managing JavaScript config files for different build environments (like QA, UAT, Production)

So you are building a stand alone Single Page Application in JavaScript that is served statically. It connects to an API.
Your application has a JSON object in its main.js like this:
var config = {
apiPath: 'http://dev.api.example.com',
appTitle: 'hello'
};
You use Grunt to create a release build, this transpiles all your SASS, ES6, AMD Modules etc code into one or two JavaScript files, along with this config object.
Your production API url is http://api.example.com and when you do a release build, you want just this value in the config to change. (Maybe you would want to change other config parameters too). You have a release config
var config = {
apiPath: 'http://api.example.com'
};
How can a JavaScript config file be altered depending on different types of release environments during a build. Notice that I want to merge the release config into the default, not maintain totally seperate files with all the settings. While there are grunt plugins for setting a build mode, I've not found any that have the ability to update config files, only hacks that change which file is pointed to.
ASP.NET has web.config transforms that do exactly this, any property in the release transform for example, will overwrite the 'default' config on packaging. Does such a workflow exist using grunt or something?
In the end I solved the question myself by using a combination of environment variables and an amazing Node called Confidence which where you place all your different envs config settings in a single file, and they are resolved to a single configuration based on filters (such as an environment variable) - these can then be injected into the build process using tools like WebPack.

not to minify the js when degugging the code minify-maven-plugin samaxes

I am using minify-maven-plugin of com.samaxes.maven to minify my js files and to make package into a single file. But during the development/debugging i need to set a configuration so that it does not minify the js but still packages the js in a single file. Please help me.
According to the documentation as of 1.5.2 you can specify
<skipMinify>true</skipMinify>
If you use a property then you can provide a default value in the pom and override it on the command line. Or you could use a clever combination of profiles and their activations to automatically set it depending on your environment (for example, CI vs development).

Categories

Resources