outaTiME / grunt-replace

Replace text patterns with applause.
MIT License
411 stars 47 forks source link

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

Closed cyfyifanchen closed 7 years ago

cyfyifanchen commented 8 years ago

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'}
        ]
      }
    },
cyfyifanchen commented 7 years ago

Figured out why.

outaTiME commented 7 years ago

Hi pal, it works?

cyfyifanchen commented 7 years ago

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?

cyfyifanchen commented 7 years ago

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

outaTiME commented 7 years ago

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.

cyfyifanchen commented 7 years ago

@outaTiME cool, thanks.

outaTiME commented 7 years ago

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 ...

cyfyifanchen commented 7 years ago

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'}
    ]
  }
}
outaTiME commented 7 years ago

mmm, can you share your code ?

gruntfile and index.hbs file

cyfyifanchen commented 7 years ago

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>
outaTiME commented 7 years ago

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 ...

cyfyifanchen commented 7 years ago

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

cyfyifanchen commented 7 years ago

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'}
    ]
  }
}
outaTiME commented 7 years ago

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

cyfyifanchen commented 7 years ago

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?

cyfyifanchen commented 7 years ago

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