grunt-sico-string-replace v0.1.1
grunt-sico-string-replace
Grunt plugin to replace strings in given files. You can use normal string replace and also regexp search and replace.
Getting Started
This plugin requires Grunt ^1.0.0
If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
npm install grunt-sico-string-replace --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-sico-string-replace');
The "sico_string_replace" task
Overview
In your project's Gruntfile, add a section named sico_string_replace
to the data object passed into grunt.initConfig()
.
grunt.initConfig({
sico_string_replace: {
options: {
pattern: {
max: 0,
match: "foo",
replace: "bar"
},
patterns: [{
max: 0,
match: "a",
replace: "x"
},{
max: 0,
match: "b",
replace: "y"
}]
},
files: [{
"tmp/dest-file": ["tmp/src-file"]
}],
},
});
Options
Default options
{
options: {
encoding: grunt.file.defaultEncoding,
timestamp: false,
pattern: {},
patterns: [],
}
}
// default pattern options
{
max: 0,
match: '',
replace: '',
}
options.encoding
Type: String
Default: grunt.file.defaultEncoding
The file encoding to copy files with.
options.timestamp
Type: Boolean
Default: false
Whether to preserve the timestamp attributes(atime
and mtime
) when copying files. Set to true
to preserve files timestamp. But timestamp will not be preserved when the file contents or name are changed during copying.
options.pattern
Type: Pattern Object
Default value: null
Pattern configuration object. Use this for a single replace. If you plan to replace more patterns then use options.patterns.
options.patterns
Type: Array<Pattern Object>
Default value: null
An array of pattern configuration objects.
pattern.match
Type: String | RegExp
Default value:
A string value to replace if found in a file. It is also possible to use regexp for matching.
pattern.replace
Type: String
Default value:
A string value that replaces any matches. If you use regexp you can also use Backreferences variables like $1
pattern.max
Type: Numeric
Default value: 0
Max numbers of matches, which will be replaced. Set 0
to replace all matches.
Note: If max != 0 then the global g
regexp flag is ignored.
Usage Examples
Simple Replace
In this example, all 'foo' matches in the file src/foo
are replaced with 'bar' the result is saved to dest/bar
.
grunt.initConfig({
sico_string_replace: {
options: {
pattern: {
max: 0,
match: "foo",
replace: "bar"
}
},
files: {
"dest/bar": ["src/foo"],
},
},
});
RegExp Replace
grunt.initConfig({
sico_string_replace: {
options: {
pattern: {
max: 0,
match: new RegExp("<(.*)>"),
replace: "<TAG-$1>"
}
},
files: {
"dest/bar": ["src/foo"],
},
},
});
Multi-Replace and Multi-File
In this example, all matches of 'a' are replaced with 'x' and the first two matched of 'b' are replaced with 'y' in all HTML files.
Note: You need to pass an array to the files parameter.
grunt.initConfig({
sico_string_replace: {
options: {
patterns: [{
max: 0,
match: "a",
replace: "x"
},{
max: 2,
match: "b",
replace: "y"
}]
},
files: [{
src: "src/*.html"
dest: "dest/"
}],
},
});
This task supports all the file mapping format Grunt supports. Please read Globbing patterns and Building the files object dynamically for additional details.
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
Release History
- 2016-10-10 v0.1.1 Add some more tests
- 2016-10-10 v0.1.0 Initial release
8 years ago