outaTiME / grunt-replace

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

Regex file replacement at group. #84

Closed mchambaud closed 9 years ago

mchambaud commented 9 years ago

Hi,

I'm trying to replace a certain script with another one but I can't figure out out to insert the file at $1.

Here's my code, works but script tag goes away.

patterns: [{
    match: /<script id="config" \b[^>]*>([\s\S]*?)<\/script>/,
    replacement: '<%= grunt.file.read(directory.build + "/"+environment+"/config.json") %>'
}]

I already have a solution but I'm not happy with it, I though maybe there was a more elegant way to achieve this.

patterns: [{
    match: /(<script id="config" \b[^>]*>)([\s\S]*?)(<\/script>)/,
    replacement: '$1<%= grunt.file.read(directory.build + "/"+environment+"/config.json") %>$3'
}]
outaTiME commented 9 years ago

Hi pal, you could try:

patterns: [{
    match: /<script id="config" \b[^>]*>([\s\S]*?)<\/script>/,
    replacement: function(match, p1) {
       return grunt.file.read(directory.build + "/" + p1 + "/config.json");
    }
}]  
mchambaud commented 9 years ago

HI @outaTiME,

Actually, I don't care about the content of the script, I want to replace it with the content of the config.json.

mchambaud commented 9 years ago

@outaTiME

this will not work.

replacement: function(match, p1) {
   return grunt.file.read(directory.build + "/" + p1 + "/config.json");
}

When ever I call grunt or variables from replacement function it fails with "Warning: Error while processing "..." file

I can do

{
    match: 'config',
    replacement: '<%= environment %>'
},

but this will not work.

{
    match: 'config',
    replacement: function(){
        return environment;
    }
},
outaTiME commented 9 years ago

i again pal, i don't know how environment variable was define but it your grunt is defined like that it might be work:

{
    match: 'config',
    replacement: function() {
        return grunt.config.get('environment');
    }
},

checkout: http://gruntjs.com/api/grunt.config

outaTiME commented 9 years ago

it works ?

mchambaud commented 9 years ago

Unfortunately, no.

outaTiME commented 9 years ago

Mmm, probably something wrong in your gruntfile, do you want to share with us your fully file ?

El 23 sept 2015, a las 13:10, Michael Chambaud notifications@github.com escribió:

Unfortunately, no.

— Reply to this email directly or view it on GitHub.

mchambaud commented 9 years ago

My grunt file is massive.. Do you have a working example with? Otherwise I will start a fresh project to see if it works.

outaTiME commented 9 years ago

Here basic example:

Gruntfile.js

module.exports = function (grunt) {

  // define the configuration for all the tasks
  grunt.initConfig({

    // environment
    environment: 'dev',

    // replace task
    replace: {
      config: {
        options: {
          patterns: [
            {
              match: 'config',
              replacement: function() {
                return grunt.config.get('environment');
              }
            }
          ]
        },
        files: [
          {expand: true, flatten: true, src: ['fixtures/environment.txt'], dest: 'tmp/'}
        ]
      }
    }

  });

  // load tasks
  grunt.loadNpmTasks('grunt-replace');

  // default task
  grunt.registerTask('default', [
    'replace'
  ]);

};

fixtures/environment.txt:

@@config

tmp/environment.txt:

dev
outaTiME commented 9 years ago

Here the complete working example:

https://cloudup.com/cnjwEtEKSlF

outaTiME commented 9 years ago

Updates ??

mchambaud commented 9 years ago

Sorry haven't had a chance to try until just now, it works =)

Thanks!