Created
April 10, 2016 08:44
-
-
Save MadLittleMods/0452ef473e854be83724bba77a153dae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gulp = require('gulp'); | |
var through = require('through2'); | |
gulp.task('replace-dep', function() { | |
var replaceDep = function() { | |
return through.obj(function(chunk, enc, callback) { | |
if(chunk.isBuffer()) { | |
var contents = String(chunk.contents); | |
// Replace the usage to the new syntax and reference to new package | |
var foundMatch = false; | |
contents = contents.replace(/context.env\(('|")(.*?)(?:'|")\)/g, function(match, quote, property) { | |
foundMatch = true; | |
return 'clientEnv[' + quote + property + quote + ']'; | |
}); | |
if(foundMatch) { | |
console.log('Found usage in', chunk.path); | |
} | |
// If we found a match in here, we need to add the `clientEnv` dep | |
// And check to make sure `context` is still being used | |
if(foundMatch) { | |
var foundContextUsage = false; | |
contents.replace(/context\./, function(match, property) { | |
foundContextUsage = true; | |
}); | |
contents = contents.replace(/var\scontext(\s*?)=\s?require\(('|")utils\/context(?:'|")\);/, function(match, spacing, quote) { | |
var packageNameLengthDifference = 'clientEnv'.length - 'context'.length; | |
var clientEnvRequireSpacingLength = spacing.length - packageNameLengthDifference; | |
clientEnvRequireSpacingLength = clientEnvRequireSpacingLength >= 0 ? clientEnvRequireSpacingLength : 1; | |
//console.log('|' + spacing + '|', spacing.length, packageNameLengthDifference); | |
var replacement = 'var clientEnv' + new Array(clientEnvRequireSpacingLength+1).join(' ') + '= require(' + quote + 'gitter-client-env' + quote + ');'; | |
if(foundContextUsage) { | |
replacement = 'var context' + spacing + '= require(' + quote + 'utils/context' + quote + ');' + '\n' + replacement; | |
} | |
console.log(replacement); | |
return replacement; | |
}); | |
} | |
chunk.contents = new Buffer(String(contents)); | |
this.push(chunk); | |
} | |
callback(); | |
}); | |
}; | |
return gulp.src([ | |
//'./test/fixtures/**/*.js', | |
'/Users/eric/Documents/github/gitter-webapp/**/*.js', | |
'!/Users/eric/Documents/github/gitter-webapp/node_modules/' | |
]) | |
.pipe(replaceDep()) | |
//.pipe(gulp.dest('./test/output/')); | |
.pipe(gulp.dest('/Users/eric/Documents/github/gitter-webapp/')); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment