The following gulpfile should give you the results you want.
I will go over the process flow for using streams of data.
We will create a mini gulp-plugin. colors.js
var map = require('map-stream'); // require the map-stream module
var gutil = require('gulp-util');
module.exports = function() { // we are actually creating a embedded gulp plugin
var buildColors = function(file, cb) {
// file is the gulp vinyl
// file.contents is the contents
reRGBa = /^rgba?\(\s*(\d{1,3}(?:\.\d+)?\%?)\s*,\s*(\d{1,3}(?:\.\d+)?\%?)\s*,\s*(\d{1,3}(?:\.\d+)?\%?)\s*(?:\s*,\s*(\d+(?:\.\d+)?)\s*)?\)$/;
reHSLa = /^hsla?\(\s*(\d{1,3})\s*,\s*(\d{1,3}\%)\s*,\s*(\d{1,3}\%)\s*(?:\s*,\s*(\d+(?:\.\d+)?)\s*)?\)$/;
reHex = /^#?([0-9a-f]{6}|[0-9a-f]{3})$/i;
var hexColors;
var lines = file.contents.toString("utf8").split('\n'); // convert file.contents from a Buffer to a string
for (var line in lines) {
var _line = lines[line];
gutil.log(_line);
var matches = reRGBa.exec(_line) || reHSLa.exec(_line) || reHex.exec(_line);
while(matches != null && matches != undefined && matches != ''){
gutil.log(matches[0]);
}
if (line == lines.length){
// the loop has finished, return the file
file.contents = new Buffer(hexColors); // make the new file.contents the contents of the color hexes.
cb(null, file); // return the error (null) and the file.
}
}
};
return map(buildColors); // finally return the map stream of the colors
};
And the gulpfile gulpfile.js
var gulp = require('gulp');
var colors = require('./colors');
gulp.task('default', function () {
// Here's where the node streaming magic via gulp happens, show me the way!
gulp.src('./css/main.less')
.pipe(colors())
.on('data', function(data){
console.log(String(data));
});
});
Your regex does not seem to be working correctly, I would confirm that they indeed get the results.
Using streams, you would use through2
to get the contents from several files, concat them, and then run the regex.
Example: https://github.com/plus3network/gulp-less/blob/master/index.js