Last active
August 29, 2015 14:20
-
-
Save tjbenton/393302393840c2cf2be4 to your computer and use it in GitHub Desktop.
This function allows you to concatenate file paths together so that you can use them in a `gulp.src`. Using this method instead of `gulp-include` allows you to generate sourcemaps to make it easier to debug issues.
This file contains hidden or 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
// @name Include | |
// @author Tyler Benton | |
// | |
// @description | |
// This function allows you to concatenate file paths together so that you can use them in a `gulp.src` | |
// Using this method instead of `gulp-include` allows you to generate sourcemaps to make it easier to debug issues. | |
// | |
// @arg {string} path - The path for the file you want to look in | |
// | |
// @returns {array} - The array of file paths | |
// | |
// @markup {js} Example js file | |
// //= include [ | |
// // "helpers/*.js", | |
// // "components/*.js" | |
// // ]; | |
// | |
// @markup {js} Example gulp task | |
// gulp.task("js:build", function(){ | |
// return gulp.src(include("app/lib/js/main.js")) | |
// .pipe($.concatSourcemap("main.js", { | |
// sourcesContent: true | |
// })) | |
// .pipe(gulp.dest(dest + paths.js)); | |
// }); | |
function include(path){ | |
var relativePath = path.slice(0, path.lastIndexOf("/") + 1), | |
file = fs.readFileSync(path, "utf8", function(err, data){ | |
return err ? console.log(err.message) : data; | |
}), | |
includes = [path], | |
lines = file.split("\n"), | |
commentBlock = [], | |
isBlock = false, | |
cleanLineRegex = /(?:\/\/)|[\[\],;"'\s]/g; | |
for(var i = 0, l = lines.length; i < l; i++){ | |
var line = lines[i], | |
startIndex = line.indexOf("//= include "); | |
// console.log(i); | |
// console.log(line); | |
if(startIndex >= 0){ | |
line = "// " + line.slice(startIndex + 12); // removes `//=include ` | |
if(!isBlock && line.indexOf(" [") >= 0){ // is array | |
// console.log("is array"); | |
if(line.indexOf("]") >= 0){ // is array on a single line | |
// console.log("on a single line"); | |
includes = includes.concat(line.replace(/(?:\/\/)|[\[\];"'\s]/g, "").split(",")); | |
}else{ // is multiline array | |
// console.log("on multiple lines"); | |
isBlock = true; | |
} | |
}else{ // is single file | |
// console.log("is single file"); | |
includes.push(line.replace(cleanLineRegex, "")); | |
} | |
} | |
if(isBlock && line.indexOf("// ") >= 0){ // is still in the comment block | |
// console.log("isBlock and has comment"); | |
line = line.replace(cleanLineRegex, ""); | |
line !== "" && commentBlock.push(line); | |
}else{ // is no longer in a comment block | |
isBlock = false; | |
if(commentBlock.length > 0){ // checks to see if any files where added and if they where then concats them with the includes array | |
includes = includes.concat(commentBlock); | |
commentBlock = []; // resets the commentBlock array | |
} | |
} | |
if(i === l - 1 && commentBlock.length > 0){ // ensures that the commentBlock gets added if it's the last line in the file | |
includes = includes.concat(commentBlock); | |
} | |
// console.log(""); | |
} | |
// this adds the relative path to each file path | |
for(var i = 1, l = includes.length; i < l; i++){ | |
includes[i] = relativePath + includes[i]; | |
} | |
// ensures there isn't any duplicate files being added | |
return includes.filter(function(v, i, a){ | |
return a.indexOf(v) == i; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment