1

I have an app folder where I want to replace http://localhost:8000 for http://fomoapp-melbourne.rhcloud.com in two files: companies-list.component.ts and events-list.component.ts. I am trying to use grunt-replace-string plugin and it seemingly runs successfully with green Done result and no errors, but no replacement happens.

Here is how Gruntfile.js looks like:

module.exports = function(grunt){

[
    'grunt-string-replace',
].forEach(function(task){
    grunt.loadNpmTasks(task);
});

// configure plugins
grunt.initConfig({
    'string-replace': {
      dist: {
        files: {
          './app/': ['companies-list.component.ts','events-list.component.ts'],
        },
        options: {
          replacements: [{
            pattern: 'http://localhost:8000',
            replacement: 'http://fomoapp-melbourne.rhcloud.com',
          }]
        }
      }
    },
});

// register tasks
    grunt.registerTask('default', ['string-replace']);
};
Nikita Vlasenko
  • 4,004
  • 7
  • 47
  • 87

1 Answers1

3

The Grunt Files object is for specifying a mapping of source files to destination files. The purpose of this mapping is to tell Grunt to get the contents of the files in source, do something to the contents, and then write the response to a new file in the destination folder.

It looks to me from your configuration that you want Grunt to rewrite two files in the app/ directory. This is not going to work. I will bet that if you run grunt with the verbose option, grunt --verbose, your output will contain the following:

Files: [no src] -> ./app/

This is because Grunt cannot find the source files because you need to specify their relative paths.

It's up to you how you want to structure your app, but you might want to have a src/ folder and a dist/ folder under app/. If you choose to build your files objects dynamically, your config might look something like this:

files: [{
    expand: true,
    cwd: './app/src/',
    dest: './app/dest/',
    src: ['companies-list.component.ts', 'events-list.component.ts']
}]

Additionally, the documentation for grunt-string-replace states:

If the pattern is a string, only the first occurrence will be replaced, as stated on String.prototype.replace.

This means that if you want multiple instances of your string to be replaced, you must provide a Regular Expression literal. For example:

replacements: [{
    pattern: /http:\/\/localhost:8000/g,
    replacement: 'http://fomoapp-melbourne.rhcloud.com'
}]
76484
  • 8,498
  • 3
  • 19
  • 30