Creating a Gulp automation task to handle ng-constant across various environments

I've been attempting to make this work, but it seems like I might be overlooking something. I'm utilizing ng-constant and configuring different environment endpoints as outlined in the ng-constants issue

However, my setup involves using gulp and the configuration appears like this:

gulp.task('environmentsapi', function () {
return ngConstant({
    stream: true,
    development: {
        constants: {
            "ENV": {"api": "http://1.1.1.1:8082/"}
        }
    },
    production: {
        constants: {
            "ENV": {"api": "https://productionapplink/"}
        }
    }
})
// Writes config.js to dist/ folder
.pipe(gulp.dest('dist/scripts/config'));
});

I am struggling to understand how to access the different endpoints in the various gulp tasks like the example in the link ngconstant:development etc. How can I execute this within the environmentsapi task, considering that this task is utilized in all environment builds? Please advise on how to accomplish this.

gulp.task('build', function () {
runSequence('clean', ['sass', 'scripts', 'bower_components', 'environmentsapi' //How can I run ngconstant:development here? ], 'wiredep')
});

Answer №1

To simplify the process, create new tasks to set flags!
Here, I am utilizing the development flag with a default setting of true.

var development = true;

gulp.task('prod', function () {
  development = false;
});

gulp.task('environmentsapi', function () {
    const apiEndpoint = development ? 'http://1.1.1.1:8082/' : 'https://productionapplink/';
    return ngConstant({
        stream: true,
        constants: {
            'ENV': {api: apiEndpoint}
        }
    });
});

By running gulp build, your application will be built with the ENV.api pointing to 'http://1.1.1.1:8082/', the development endpoint.

Executing gulp prod build will change the output to utilize an ENV.api set as 'https://productionapplink/'.


The solution above works well for two environments, but it can become complex with more. An alternative is to use yargs.

Below is an updated version of your gulpfile.js:

const argv = require('yargs').argv;

const endpoints = {
    'dev': 'http://1.1.1.1:8082/',
    'prod-org': 'https://productionapplink.org/',
    'prod-com': 'https://productionapplink.com/',
    'prod-gov': 'https://productionapplink.gov/'
};

gulp.task('enviornmentsapi', function () {
    const apiEnpdoint = typeof argv.env === 'undefined' ? endpoints.dev : endpoints[argv.env];
    return ngConstant({
        stream: true,
        constants: {
            ENV: { api: apiEnpdoint }
        }
    }).pipe(gulp.dest('dist/scripts/config'));
});

Usage:

  • gulp build uses the default api URL: 'http://1.1.1.1:8082/'
  • gulp build --env=prod-org uses 'https://productionapplink.org/'
  • gulp build --env=prod-com uses 'https://productionapplink.com/'

This flexible approach should help manage multiple environments effectively!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discord.js Error: Unable to access undefined properties for 'username'

victim is a variable containing the user's ID input. Despite confirming it as a valid user ID on the server, I encounter the error 'cannot read properties of undefined' This is my first time using client.users.cache.get(victim).username in ...

Run code once all ajax requests have been completed

Here's the code snippet I'm working with: function updateCharts() { for (var i = 0; i < charts.length; i++) { updateChart(charts[i]); } sortQueues(); } function updateChart(chart) { $.ajax({ type: "POST", ...

AngularJS gathers user database information from Firebase using the `$rootScope`

My goal is to retrieve user information from a database and make it accessible globally using $rootScope. Upon user sign-in, I have access to the user's email and uid. However, in order to retrieve additional user info from the database, I need to que ...

The variable "$" cannot be found within the current context - encountering TypeScript and jQuery within a module

Whenever I attempt to utilize jQuery within a class or module, I encounter an error: /// <reference path="../jquery.d.ts" /> element: jQuery; // all is good elementou: $; // all is fine class buggers{ private element: jQuery; // The nam ...

Load HighCharts dynamically in a sequential manner

Currently, I am working on dynamically loading a variety of series based on user-selected projects. My tech stack includes Laravel 5.2, PHP 5.6, and HighCharts. I've successfully loaded one JSON file generated upon project selection. However, I aim to ...

What is the method for retrieving a variable from a Q Node promise?

I am currently developing a learning game utilizing Angular, Express, MongoDB, and Node. As part of my learning process, I have been exploring the use of promises to manage asynchronous operations effectively. One particular challenge I am facing involves ...

Error: The function styles$1.makeStyles is not defined

Currently, I am experimenting with rollup for bundling. However, when I run the code, I encounter the following problem: https://i.stack.imgur.com/8xLT3.png import { makeStyles } from '@material-ui/styles'; const styles = makeStyles({ subHead ...

Display search results in Rails without needing to refresh the entire tab

I have a search functionality incorporated within a Bootstrap tab, and I aim to display the search results dynamically without reloading the entire page, specifically within the tab itself. Despite adding 'remote: true' to the form_tag and incor ...

Steps for including a map in a property file for JavaScript parsing

When a checkbox is clicked, a new series of checkboxes will be displayed. The details for these checkboxes are fetched from a database. However, I now need to have certain checkboxes pre-checked based on the user's selection. Since I can't store ...

Adjusting background size using jQuery as the window is resized

Exploring ways to dynamically resize the background image of my website based on the current window dimensions using jQuery. At the moment, I have tried the following approach: $(window).resize(function() { $("body").css({"background ...

Get the report from jsreport and angularjs now

Is there a way I can enable users to download a report from my AngularJS Application? The report format is in .xlsx. I am able to send the data, but the response received appears like this: https://i.stack.imgur.com/3OPra.png What I want is to provide a ...

What is the best configuration to use for successful MapReduce queries in Riak?

While working on a nodejs application with riak / riak-js, I encountered the following issue: Executing this request db.mapreduce .add('logs') .run(); successfully retrieves all 155.000 items stored in the bucket logs along with their IDs ...

What is the best way to retrieve the name of a dynamic form in a Controller

How can I retrieve the dynamic form name in my controller? See below for the code snippet: HTML <form name="{{myForm}}" novalidate> <input type="text" ng-model="username" name="username" required/> <span ng-show="(submit & ...

Tips for identifying a scroll down gesture on a touch device with jQuery

I have a method for infinite scroll that is triggered like this: $(window).on('scroll resize load', function() { // execute the infinite scroll method }); It is triggered by these events: scroll resize load However, it does not seem to ...

I'm looking to use JavaScript to dynamically generate multiple tabs based on the selected option in a dropdown menu

I'm reaching out with this question because my search for a clear answer or method has come up empty. Here's what I need help with: I've set up a dropdown titled 'Number of Chassis'. Depending on the selection made in this dropdown ...

animation of leaping to a specific element

I am currently working on a sidebar with links that have a hover effect to add a bullet. However, I want the bullet to smoothly follow the cursor's movement along the y-axis within the sidebar instead of jumping between the links. How can I achieve th ...

What's the deal with $routeProvider in angularJS?

I'm having some trouble with my simple web app that I'm trying to update using route provider. Everything works fine when I click on the list items on the page, but when I refresh the page, I get a 404 error. It seems like it's trying to acc ...

The Route.get() function in Node.js is expecting a callback function, but instead received an unexpected object of type

Recently, I started coding with nodejs and express. In my file test.js located in the routes folder, I have written the following code: const express = require('express'); const router = new express.Router(); router.get('/test', (req ...

"AngularJS Bi-Directional Data Binding Issue: Unexpected 'Undefined

Recently, I've been tackling a project that involves using a directive. Initially, everything was smooth sailing as I had my directive set up to bind to ngModel. However, I hit a roadblock when I needed to bind multiple values. Upon making the necessa ...

Utilize Vue.js to easily upload images alongside form input fields

I have recently started a small project with Vue Js. I am trying to incorporate an upload file option in my contact form. Due to the numerous input text fields, I am using serialize for the form. However, I am facing issues with the append function. How ca ...