outaTiME/grunt-replace

Unable to match the file name with `@@`.

cyfyifanchen opened this issue · 16 comments

Hi,

I'd like to use this grunt task to update the file name with package.json version number i.e <%= pkg.version %>. The following is the code, however I couldn't run the task, the error is saying Unable to match 1 pattern, remember for simple matches (String) we are using the prefix @@ for replaceme. Any idea what's going on?

File that I want to replace:

  <script src="/dist/js/main.@@test123.js"></script>

replace task in Gruntfile:

    replace: {
      dist: {
        options: {
          patterns: [
            {
              match: 'test123',
              replacement: '<%= pkg.version %>'
            }
          ]
        },
        files: [
          {expand: true,
          flatten: true,
          src: ['./index.hbs'], dest: './build'}
        ]
      }
    },

Figured out why.

Hi pal, it works?

It does, however I ran into another problem seeking advices. For example, I have a grunt task is called grunt-bump which bumps a version up every time I run it. So I set a @@pkgJsonVersion variable in the template where I want to replace every time after I run grunt-bump. The problem is that I am limited to only replace it once because after the first time @@pkgJsonVersion gets replaced by package.json version number. Any ideas how to fix this?

@outaTiME I might need to open a separate issue only for this problem for better publicity?

This is not an issue from grunt-replace you need to resolve expression after bump runs, im away right now, in few minutes I send you and advise

El 9 nov. 2016, a las 20:15, Yifan Chen notifications@github.com escribió:

@outaTiME I might need to open a separate issue only for this problem for better publicity?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

@outaTiME cool, thanks.

You could try the following:

replace: {
  dist: {
    options: {
      patterns: [
        {
          match: 'test123',
          replacement: function () {
            // read again the package file
            var new_pkg = grunt.file.readJSON('package.json');
            return new_pkg.version;
          }
        }
      ]
    },
    files: [
      {expand: true,
      flatten: true,
      src: ['./index.hbs'], dest: './build'}
    ]
  }
}

the pkg in expression points to initial read of package.json file, tell me if it works ...

I saw your intention using the function as the replacement, that's smart. However, it doesn't work. the error says unable to match 1 pattern.. I think it might be a syntax error. So, I changed grunt.file.readJSON('package.json') to Gruntfile.readJSON('package.json'). Still doesn't work.

replace: {
  dist: {
    options: {
      patterns: [
        {
          match: 'test123',
          replacement: function () {
            // read again the package file
            var new_pkg = Gruntfile.readJSON('package.json');
            return new_pkg.version;
          }
        }
      ]
    },
    files: [
      {expand: true,
      flatten: true,
      src: ['./index.hbs'], dest: './build'}
    ]
  }
}

mmm, can you share your code ?

gruntfile and index.hbs file

Gruntfile:

    replace : {
      dist : {
        options : {
          patterns : [
            {
              match: 'packageJsonVersion',
              // the following line throws errors
              replacement: function() {
                var new_pkg = grunt.file.readJSON('package.json');
                return new_pkg.version;
              }
              // the following line works fine, but throws errors after grunt-bump.
              replacement: '<%= pkg.version %>'
            }
          ]
        },
        files : [
          {
            expand : true,
            flatten : true,
            src : [ './home/index.hbs' ],
            dest : './home/'
          },
        ]
      }
    },

index.hbs:

  <script src="/main.@@packageJsonVersion.min.js"></script>

I need the full gruntfile to help you ...

try it .. if you create a empty gulpfile with ONLY replace task and the index.hbs as look like it works, but in your complex workflow something are wrong ...

It's very possible that my build process is too complicated, I will try to do a simple one and keep you posted.

I tested it with an empty Gruntfile.js, still doesn't work as I expected. It only works for the first time, when the @@packageJsonVersion get replaced my new_pkg.version(from package.json). After that, the variable @@pkgJsonVersion is gone, so next time to run grunt-replace, there is no way to find a match pattern.

Link has the all the files:
https://gist.github.com/yifanchen/494c204ca5e6b843e79deb15d64fdee6

However, I straighten some logics up by looking at your code: It doesn't work, maybe because match doesn't take function as argument?

replace: {
  dist: {
    options: {
      patterns: [
        {
          match: function () {
            // find the pre-replaced version by -1.
            var new_pkg = grunt.file.readJSON('package.json');
            return new_pkg.version - 0.0.1;
          },

          replacement: function () {
            // then replace it with the current version. 
            var new_pkg = grunt.file.readJSON('package.json');
            return new_pkg.version;
          }
        }
      ]
    },
    files: [
      {expand: true,
      flatten: true,
      src: ['./index.hbs'], dest: './build'}
    ]
  }
}
<script src="https://gist.github.com/yifanchen/494c204ca5e6b843e79deb15d64fdee6.js"></script>

Hi, first of all the match must be an expression or pattern. Second I don't understand how replace runs again or next run

https://gist.github.com/yifanchen/494c204ca5e6b843e79deb15d64fdee6
Using the above link as an example:

In package.json, the version is 0.0.1. I'd like to make the index.js to index.0.0.1.js inside the index.html. It is doable by running grunt replace. However, once it changes to index.0.0.1.js. How can you replace it again?

Found the solution, don't worry about it anymore. Thanks for your advice anyway.