Skip to content

Instantly share code, notes, and snippets.

@ssovit
Created September 8, 2016 06:29
Show Gist options
  • Save ssovit/3adf8ef782eca1ecb624ef47899a9adf to your computer and use it in GitHub Desktop.
Save ssovit/3adf8ef782eca1ecb624ef47899a9adf to your computer and use it in GitHub Desktop.
Gulp - Nicely Commented Less Import from a folder
var gulp = require('gulp');
var less = require('gulp-less');
var lessImport=require('less-imports.js');
gulp.task('less_parts', function() {
gulp.src("src/less/parts/*.less")
.pipe(lessImport('style.css'))
.pipe(less())
.pipe(gulp.dest("./"));
});
gulp.task('default', ['less_parts']);
var through = require('through2');
var fs = require('fs');
var path = require('path');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var File = gutil.File;
module.exports = function(dest, option) {
option = option || {};
if (typeof option.newLine !== 'string') {
option.newLine = gutil.linefeed;
}
var LessContent;
var latestFile = false;
var lessPath;
var config = JSON.parse(fs.readFileSync('src/config.json'));
var setting = config.theme
var header = [];
var less_content = [];
var legends = "";
var options = {
"Theme Name": setting.title,
"Author": setting.author,
"Author URI": setting.authorUrl,
"Version": setting.version,
"License": setting.license,
"License URI": setting.licenseUrl,
"Description": setting.description,
"Text Domain": setting.textdomain,
};
header.push("/*");
for (var obj in options) {
header.push(obj + ": " + options[obj]);
}
header.push("*/");
header.push("");
header.push("/* ==========================================");
header.push("\tTABLE OF CONTENTS");
header.push(" ==========================================");
header.push(" ");
var prepareFiles = function(file, encoding, callback) {
if (file.isNull()) {
callback();
return;
}
if (file.isStream()) {
callback();
return;
}
var filename = file.relative;
var nameonly = filename;
nameonly = nameonly.replace(/_/g, " ");
nameonly = nameonly.replace("-", ". ");
nameonly = nameonly.toUpperCase();
if (filename.substring(0, 1) != "_") {
header.push("\t" + nameonly);
less_content.push("/* ==========================================");
less_content.push("\t");
less_content.push(nameonly);
less_content.push(" ========================================== */");
}
less_content.push('@import "' + file.path + '";');
latestFile = file;
callback();
};
var endStream = function(callback) {
if (!latestFile && less_content.length < 1) {
callback();
return;
}
var finalFile;
if (typeof dest === 'string') {
LessContent = header.join(option.newLine) + option.newLine + less_content.join(option.newLine);
var content = new Buffer(LessContent);
} else {
finalFile = new File(dest);
finalFile.contents = content;
}
finalFile = latestFile.clone({
contents: false
});
finalFile.path = path.join(latestFile.base, dest);
finalFile.contents = content;
this.push(finalFile);
callback();
}
return through.obj(prepareFiles, endStream);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment