/ / Cómo determinar el entorno principal de ASP.NET en mi Gulpfile.js - gulp, asp.net-core, production-environment, visual-studio-2015, asp.net-core-mvc

Cómo determinar el entorno principal ASP.NET en mi Gulpfile.js - gulp, asp.net-core, production-environment, visual-studio-2015, asp.net-core-mvc

Estoy usando ASP.NET Core MVC 6 con Visual Studio 2015. En mi script gulpfile.js, quiero saber si el entorno de alojamiento es Desarrollo, Puesta en escena o Producción para poder agregar o eliminar mapas de origen (archivos .map) y hacer otras cosas. es posible?

ACTUALIZAR

Cuestión relevante en GitHub.

Respuestas

5 para la respuesta № 1

Puedes usar el ASPNETCORE_ENVIRONMENT (Fue anteriormente ASPNET_ENV en RC1) variable de entorno para obtener el entorno. Esto se puede hacer en su gulpfile usando process.env.ASPNETCORE_ENVIRONMENT.

Si la variable de entorno no existe, puede volver a leer el launchSettings.json Archivo que utiliza Visual Studio para iniciar su aplicación. Si eso tampoco existe, entonces recurra al uso del entorno de desarrollo.

Escribí el siguiente objeto de JavaScript para facilitar el manejo del entorno en gulpfile.js. Puede encontrar el código fuente completo de gulpfile.js aquí.

// Read the launchSettings.json file into the launch variable.
var launch = require("./Properties/launchSettings.json");

// Holds information about the hosting environment.
var environment = {
// The names of the different environments.
development: "Development",
staging: "Staging",
production: "Production",
// Gets the current hosting environment the application is running under.
current: function () {
return process.env.ASPNETCORE_ENVIRONMENT ||
(launch && launch.profiles["IIS Express"].environmentVariables.ASPNETCORE_ENVIRONMENT) ||
this.development;
},
// Are we running under the development environment.
isDevelopment: function () { return this.current() === this.development; },
// Are we running under the staging environment.
isStaging: function () { return this.current() === this.staging; },
// Are we running under the production environment.
isProduction: function () { return this.current() === this.production; }
};

Ver esta responder por cómo configurar la variable de entorno.


1 para la respuesta № 2

Necesitaría establecer la variable de entorno NODE_ENV en cada entorno y luego en su archivo gulp, leerlo usando process.env.NODE_ENV.

Mira esto https://stackoverflow.com/a/16979503/672859 para detalles adicionales.