0

I have a service where I manage my requests to a webservice and actually I have defined the webservice url on this service:

(function ()
{
    angular.module("MyProject")

    .factory("Webservice", ["$http", function ($http)
    {
        var URLbase = function ()
        {
            //return "http://myproject.link.pt/Service/Service.svc/";
            return "http://localhost/MyProject/Service.svc/";
        }

        var login = function (param) {
            var serviceURL = URLbase() + "ValidateLogin";
            return $http.post(serviceURL, JSON.stringify({ username: param.Username, password: param.Password }));
          }
        var loginResult = function (response) {
            return exists(response) && exists(response.data) && exists(response.data.ValidateLoginResult)
                ? response.data.ValidateLoginResult
                : null;
        }

        return {
            Login: login,
            LoginResult: loginResult
        }
    }])
})();

But when I need to publish my project I frequently forget to comment and uncomment the paths on the URLbase().

There is any way to do this on the gulpfile.js?

On my gulpfile.js I have two different tasks to build for development or for production and I thought that was nice if I could done that management there:

gulp.task('build-development', function (done)
{
    config.minify = false;

    var tasks = ['html', 'resources', 'assets', 'sass', 'js', 'vendors'];
    seq('clean', tasks, done);
});

gulp.task('build-production', function (done)
{
    config.minify = true;

    var tasks = ['html', 'resources', 'assets', 'sass', 'js', 'vendors'];
    seq('clean', tasks, function() {
        notifier.notify({ title: 'Project publishment', message: 'Done' });
    });
});

I already tried to create a variable on my gulpfile.js an access it on a AngularJS file but I got an error saying that variable isn't defined.

1 Answer 1

1

Use a placeholder in your webservice:

var URLbase = function ()
{
    return "URL_BASE_FOR_WEBSERVICE";
}

Add both URLs to your config object:

gulp.task('build-development', function (done)
{
    config.minify = false;
    config.urlBaseForWebService = "http://localhost/MyProject/Service.svc/";

    var tasks = ['html', 'resources', 'assets', 'sass', 'js', 'vendors'];
    seq('clean', tasks, done);
});

gulp.task('build-production', function (done)
{
    config.minify = true;
    config.urlBaseForWebService = "http://myproject.link.pt/Service/Service.svc/";

    var tasks = ['html', 'resources', 'assets', 'sass', 'js', 'vendors'];
    seq('clean', tasks, function() {
        notifier.notify({ title: 'Project publishment', message: 'Done' });
    });
});

Finally in your js task use gulp-replace to replace the URL_BASE_FOR_WEBSERVICE placeholder with the value of config.urlBaseForWebService:

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

 gulp.task('js', function() {
   return gulp.src('src/js/**/*.js')
     .pipe(replace(/URL_BASE_FOR_WEBSERVICE/, config.urlBaseForWebService))
     .pipe(gulp.dest('dist/js'));
 });
Sign up to request clarification or add additional context in comments.

1 Comment

Really nice solution. Works perfectly! Thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.