Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for .sass syntax. Only output file if variables exists. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,41 @@ var gutil = require('gulp-util');

module.exports = function() {
return through(function (file) {
var regex = /\$(.*?):(.*?);/g;
var regex = /\$(.*?):(.*?)(\n|;)/g;
var variables = {};
var stringifiedContent;
var jsonVariables;
var filename;
var m;

// if it does not have a .scss suffix, ignore the file
if (!gulpmatch(file,'**/*.scss')) {
// if it does not have a .scss or .sass suffix, ignore the file
if (!gulpmatch(file,'**/*.{scss,sass}')) {
this.push(file);
return;
}

// load the JSON
stringifiedContent = String(file.contents);

while ((m = regex.exec(stringifiedContent)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
// only look at files that actually have sass vars
if(stringifiedContent.match(/\$.*?:/g)) {

while ((m = regex.exec(stringifiedContent)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}

variables[m[1].trim()] = m[2].trim();
}

variables[m[1].trim()] = m[2].trim();
}
jsonVariables = JSON.stringify(variables, null, '\t');
file.contents = Buffer(jsonVariables);
filename = file.path.split('/').pop();
file.path = file.path.replace(filename, filename.replace(/^_/, ''));
file.path = gutil.replaceExtension(file.path, '.json');

jsonVariables = JSON.stringify(variables, null, '\t');
file.contents = Buffer(jsonVariables);
filename = file.path.split('/').pop();
file.path = file.path.replace(filename, filename.replace(/^_/, ''));
file.path = gutil.replaceExtension(file.path, '.json');
this.push(file);

this.push(file);
}
});
};