2018年4月9日 星期一

[Gulp] Multiple environments - system variables


 C#   ExpandoObject  


Scenario


In the last part of [Gulp] Multiple environment - gulp-util, we could use the following command to run the task with specified environment,

 $ gulp myTaskName --env=dev 

Now our team needed to bind the gulp tasks while building the project like this,


However, too bad there was no way to send arguments in Task Runner on Visual Studio 2015, 2017.
We came out to use the system variables in Windows and let Gulp did the tasks which depended on the value of it.


Environment


Visual Studio 2017 community
gulp 3.9.1


Implement


Create system environment

Choose to set a User Variable or System variable here.

Set user variable

$ setx ASPNET_ENV "dev"

Set user variable

$ setx ASPNET_ENV "dev" /M


Gulp

Then in gulpfile.js, we can check the environment by…

const DEVELOPMENT = "dev";
const TESTING = "test";
const PRODUCTION = "prod";

if (process.env.ASPNET_ENV === DEVELOPMENT) {
        // Do something for “Development” environment
}



Integrate the system variables with gulp-util, we can use both of them like this…

var util = require('gulp-util');

if ((!util.env.env && process.env.ASPNET_ENV === DEVELOPMENT) || util.env.env === DEVELOPMENT) {
        // Do something for “Development” environment
}



Reference






沒有留言:

張貼留言