Skip to content

Commit

Permalink
Merge pull request #5 from VitorLuizC/master
Browse files Browse the repository at this point in the history
Fix #4 very good <3
  • Loading branch information
giowe committed Jun 26, 2017
2 parents 52b6ef4 + 22a5f2d commit d75435e
Show file tree
Hide file tree
Showing 3 changed files with 377 additions and 23 deletions.
50 changes: 30 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
'use strict';

const { log, colors: { green }, PluginError } = require('gulp-util');
const header = require('gulp-header');
const Stream = require('readable-stream');
const StreamQueue = require('streamqueue');
const parse = require('parse-sass-value');
const pkg = require('./package.json');

/**
* @typedef Options
* @type {Object}
* @property {boolean} verbose
* @property {('single'|'double')} quotes
* @property {('comma'|'space')} separator
*/

/**
* Default plugin options.
* @type {Options}
*/
const defaultOptions = {
verbose: true,
quotes: 'single',
separator: 'comma'
};

/**
* Parse object properties as SASS variables and inject them on streams.
* @param {Object} variables
* @param {Options} options
* @returns {Stream}
*/
function getStreamFromBuffer(string) {
const stream = new Stream.Readable();
stream._read = function() {
stream.push(new Buffer(string));
stream._read = stream.push.bind(stream, null);
};
return stream;
}

module.exports = (variables, options) => {
options = Object.assign({}, defaultOptions, options);

Expand All @@ -53,5 +45,23 @@ module.exports = (variables, options) => {
+ `\t${statements.join('\n\t')}`);
}

return header(statements.join('\n'));
const stream = new Stream.Transform({objectMode: true});

stream._transform = function(file, enc, cb) {
if(file.isNull()) {
return cb(null, file);
}

const prependedBuffer = new Buffer(statements.join('\n'));
if(file.isStream()) {
file.contents = new StreamQueue( getStreamFromBuffer(prependedBuffer), file.contents);
return cb(null, file);
}

file.contents = Buffer.concat([prependedBuffer, file.contents],
prependedBuffer.length + file.contents.length);
cb(null, file);
};

return stream;
};
Loading

0 comments on commit d75435e

Please sign in to comment.