hparra / gulp-rename

Rename files easily
MIT License
692 stars 73 forks source link

Set extname by file content value #86

Closed nathanwoulfe closed 5 years ago

nathanwoulfe commented 5 years ago

I'm needing to set the extname property based on a value in the file:

.pipe(rename(function(path, file) { 
    path.dirname = path.dirname;
    path.basename = path.basename;
    path.extname = file.contents.toString().indexOf('foo') > -1 ? '.this' : '.that';                        
}))

The ternary operation always falls through to the false branch, regardless of the file contents. Is there a better way to achieve this?

nathanwoulfe commented 5 years ago

And of course as I type that, I solve the issue - the example works fine. I was trying to set like this:

path.extname = (file.contents.toString().indexOf('foo') > -1 ? '_this' : '_that') + path.extname;

to essentially set a suffix on the file rather than changing the extension. Correct syntax is:

path.extname = file.contents.toString().indexOf('foo') > -1 ? '_this' + path.extname : '_that' + path.extname;