Skip to content

Commit

Permalink
Merge pull request #24 from scottyeck/master
Browse files Browse the repository at this point in the history
Adds replace functionality / tests
  • Loading branch information
mattstyles committed Sep 29, 2015
2 parents 143b173 + fec66cf commit 663e59d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 8 deletions.
11 changes: 11 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ module.exports = function ( grunt ) {
}
},

bannerReplace: {
options: {
position: 'replace',
replaceContent: '// replace-this-comment',
banner: '// the banner'
},
files: {
src: [ 'test/tmp/someReplace.js']
}
},

bannerNoLineBreak: {
options: {
banner: 'console.log("loaded"); ',
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ grunt.initConfig({
#### options.position
Type: `String`
Default: `'top'`
Value range: `'top'` or `'bottom'`
Value range: `'top'` or `'bottom'` or `'replace'`

The position to place the banner - *either* the top or bottom (other values will default to top).
The position to place the banner - *either* the top or bottom or in place of the contents in the desired file specified by `'replaceContent'`.

#### options.replaceContent
Type: `String` or `RegExp`

The text in the specified file that the banner should replace. Valid only when ```position``` is set to `'replace'`.

This comment has been minimized.

Copy link
@vsn4ik

vsn4ik Oct 8, 2015

Contributor

position is short position

This comment has been minimized.

Copy link
@mattstyles

mattstyles Oct 8, 2015

Author Owner

Great spot, think I've caught them all. Although I'm curious, I thought gfm didnt mind? I dont think I saw any issues in the rendering on github at least


#### options.banner
Type: `String`
Expand Down
30 changes: 24 additions & 6 deletions tasks/usebanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,20 @@ module.exports = function ( grunt ) {
process: false
});

if ( options.position !== 'top' && options.position !== 'bottom' ) {
if ( options.position !== 'top' && options.position !== 'bottom' && options.position !== 'replace') {
options.position = 'top';
}

// Verify that if user wishes to replace content with a banner, that they have correctly
// supplied the content they wish to replace.
if ( options.position === 'replace' ) {
if ( ! (('replaceContent' in options)) ) {
grunt.util.error('Detected option `replace` without accompanying option `replaceContent`.');
} else if ( typeof options.replaceContent !== 'string' || ! (options.replaceContent instanceof RegExp) ) {
grunt.util.error('Detected option `replaceContent` with invalid type - type must be String or RegExp.');
}
}

var re = null;

if ( options.pattern ) {
Expand All @@ -51,11 +61,19 @@ module.exports = function ( grunt ) {
options.banner = options.process( src );
}

grunt.file.write( src,
options.position === 'top' ?
options.banner + linebreak + fileContents :
fileContents + linebreak + options.banner
);
if ( options.position === 'replace' ) {
if ( ! (options.replaceContent instanceof RegExp) ) {
options.replaceContent = new RegExp(options.replaceContent);
}
fileContents = fileContents.replace(options.replaceContent, options.banner);
grunt.file.write( src, fileContents );
} else {
grunt.file.write( src,
options.position === 'top' ?
options.banner + linebreak + fileContents :
fileContents + linebreak + options.banner
);
}

grunt.verbose.writeln( 'Banner added to file ' + chalk.cyan( src ) );
}
Expand Down
11 changes: 11 additions & 0 deletions test/banner_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ exports.banner = {
test.done();
},

bannerReplace: function ( test ) {
test.expect( 1 );

var actual = grunt.file.read( 'test/tmp/someReplace.js' );
var expected = grunt.file.read( 'test/expected/someReplace.js' );

test.equal( actual, expected, 'should add a banner to replace content in the middle of a file' );

test.done();
},

bannerNoLineBreak: function ( test ) {
test.expect( 1 );

Expand Down
3 changes: 3 additions & 0 deletions test/expected/someReplace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var variable1 = "this is a variable";
// the banner
var variable2 = "this is another variable";
3 changes: 3 additions & 0 deletions test/fixtures/someReplace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var variable1 = "this is a variable";
// replace-this-comment
var variable2 = "this is another variable";

0 comments on commit 663e59d

Please sign in to comment.