Srini Srini - 2 months ago 18
AngularJS Question

Pass parameters to processhtml grunt task for updating index.html file

I am very new to grunt.I have two grunt tasks , one for deploying to dev and one for deploying to test. I wanted to pass the respective deployment url as pararmeter when running the tasks. Also i would like to use this passed parameter value to be updated in my index.html by using

processhtml


My deployment tasks:

grunt.registerTask('deploy-dev', ['msdeploy:target1', 'msdeploy:target2']);
grunt.registerTask('deploy-test', ['msdeploy:target3', 'msdeploy:target4']);
//I would like to pass my respective url parameters here , how ?


//how can i use the passed parameters above here to process the html ?

processhtml: {
options: {
data: {
message: grunt.template.today(),
}
},
dist: {
files: {
'dist/index.html': ['index.html']
}
}
}


Index.html:
//i wanted to use the dynamic url value from processhtml here to be updated

<!-- build:template !-->
<script>
var deployUrl = '<%= url%>'
</script>
<!-- /build -->


Is there a better way of doing this ? If i am thinking the right way, how to do it ? Kindly help !

Answer
processhtml: {
                options: {
                    data: {
                        message: grunt.template.today(),
                        //this will be replaced dynamically based on
                        //deployment regions
                        serviceUrl: ''
                    }
                },
                dist: {
                    files: {
                        'dist/index.html': ['index.html']
                    }
                }
            }

 grunt.registerTask('deploy-dev', function () {
        grunt.config.set('processhtml.options.data.serviceUrl', "http://server1/api");
        //run the processhtml with the new parameter
        grunt.task.run('processhtml');
        //call the deployment task
        grunt.task.run('msdeploy:server1');
    });

Index.html file changes:

<!-- build:template !-->
    <script>
        var buildDateTime = '<%= message %>'
        var serviceBase = '<%= serviceUrl %>'
    </script>
<!-- /build -->
Comments