Using Grunt to Replace Text in a File

mtpultz

I'm trying to get Grunt to replace a path reference and I'm not sure what I'm doing wrong. This looks like it should work. Essentially, I'm copying a Bootstrap file up a directory and changing the @import paths, so I'm just trying to replace 'bootstrap/' with the new destination path 'MY/NEW/DEST/PATH/bootstrap'. I don't want to use a module for something as straight forward as this, seems needless. Everything works but the replace.

var destFilePath = path.join(basePath, file);

// Does the file already exist?
if (!grunt.file.exists(destFilePath)) {

    // Copy Bootstrap source @import file to destination
    grunt.file.copy(

        // Node API join to keep this cross-platform
        path.join(basePath, 'bootstrap/_bootstrap.scss'),
        destFilePath
    );

    // Require node filesystem module, since not a global
    var fs = require('fs');

    // Replace @import paths to be relative to new destination                                
    fs.readFile(destFilePath, 'utf8', function(err, data) {

        // Check for any errs during read
        if (err) {
            return grunt.log.write(err);
        }

        var result = data.replace('/bootstrap\//g', 'bootstrap/bootstrap/');

        fs.writeFile(destFilePath, result, 'utf8', function(err) {
            return grunt.log.write(err);
        });
    });
}
Robert Levy

You wrapped your regex in quotes - don't do that and it should work fine:

var result = data.replace(/bootstrap\//g, 'bootstrap/bootstrap/');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using Grunt to Replace Text in a File

From Dev

grunt: how to replace paths in html file using grunt task

From Dev

grunt: how to replace paths in html file using grunt task

From Dev

Replace text in text file using VBS

From Dev

Grunt replace HTML with file contents

From Dev

Grunt uglify - replace original file

From Dev

How to replace a column in a text file using a list?

From Dev

Unable to replace text in a file using perl

From Dev

Find and replace words in a text file using java

From Dev

Using sed in terminal to replace text in file

From Dev

Find and replace text within a file using commands

From Dev

PHP Replace string in text file using

From Dev

Replace text inside a PDF file using iText

From Dev

Find and replace text within a file using commands

From Dev

Using sed in terminal to replace text in file

From Dev

Replace text in file with variable using sed

From Dev

Select Text in a File and replace it using C#

From Dev

Replace all text in text file using regular expression

From Dev

Replace text between 2 particular lines in a text file using sed

From Dev

Grunt - Replace blank lines in a file - regex

From Dev

Grunt less won't replace css file

From Dev

Batch File to Find and Replace in text file using whole word only?

From Dev

Grunt not using right JSON file

From Dev

Replace multiline text in a file using Powershell without using Regex

From Dev

Replacing strings using grunt-replace

From Dev

Using Grunt to preprocess and replace environment variables

From Dev

Using sed to find-and-replace in a text file using strings from another text file

From Dev

How to minify a JSON text file with grunt?

From Dev

replace text in middle of the file

Related Related

HotTag

Archive