grunt-includes
Advanced tools
Comparing version 0.1.1 to 0.2.0
{ | ||
"name": "grunt-includes", | ||
"description": "Include other files within a file.", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"author": "vanetix <matmcfarland@gmail.com>", | ||
@@ -31,2 +31,2 @@ "main": "Gruntfile.js", | ||
} | ||
} | ||
} |
@@ -21,3 +21,3 @@ /* | ||
var regex = /^(\s*)include\s+"(\S+)"\s*$/; | ||
var defaultRegexp = /^\s*include\s+"(\S+)"\s*$/; | ||
@@ -29,9 +29,26 @@ /** | ||
grunt.registerMultiTask('includes', 'Your task description goes here.', function() { | ||
grunt.registerMultiTask('includes', 'Include other files within files.', function() { | ||
/** | ||
* Default options | ||
*/ | ||
var opts = this.options({ | ||
debug: false, | ||
duplicates: true, | ||
includeRegexp: defaultRegexp | ||
}); | ||
this.files.forEach(function(f) { | ||
var src = f.src.filter(function(path) { | ||
if(grunt.file.exists(path)) { | ||
var src, cwd = f.cwd; | ||
src = f.src.filter(function(p) { | ||
if(cwd) { | ||
p = path.join(f.cwd, p); | ||
} | ||
if(grunt.file.exists(p)) { | ||
return true; | ||
} else { | ||
grunt.log.warn('Source file "' + path + '" not found.'); | ||
grunt.log.warn('Source file "' + p + '" not found.'); | ||
return false; | ||
@@ -41,10 +58,18 @@ } | ||
if(grunt.file.isFile(f.dest)) { | ||
grunt.fail.warn('Destination directory "' + f.dest + '" is a file.'); | ||
if(src.length > 1 && grunt.file.isFile(f.dest)) { | ||
grunt.log.warn('Source file cannot be more than one when dest is a file.'); | ||
} | ||
src.forEach(function(file) { | ||
grunt.file.write(path.join(f.dest, path.basename(file)), recurse(file)); | ||
src.forEach(function(p) { | ||
var fileName = f.flatten ? path.basename(p) : p; | ||
var outFile = grunt.file.isFile(f.dest) ? f.dest : path.join(f.dest, fileName); | ||
if(cwd) { | ||
p = path.join(cwd, p); | ||
} | ||
grunt.file.write(outFile, recurse(p, opts)); | ||
grunt.log.oklns('Saved ' + outFile); | ||
}); | ||
}); | ||
@@ -54,2 +79,22 @@ }); | ||
/** | ||
* Returns the comment style for file `p` | ||
* | ||
* @param {String} p | ||
* @return {String} | ||
*/ | ||
function commentStyle(p) { | ||
var comments, | ||
ext = path.extname(p).slice(1); | ||
comments = { | ||
js: "/* %s */", | ||
css: "/* %s */", | ||
html: "<!-- %s -->" | ||
}; | ||
return comments[ext] || '/* %s */'; | ||
} | ||
/** | ||
* Helper for `includes` builds all includes for `p` | ||
@@ -61,3 +106,8 @@ * | ||
function recurse(p) { | ||
function recurse(p, opts, included) { | ||
var src, next, match, error, comment, compiled; | ||
comment = commentStyle(p); | ||
included = included || []; | ||
if(!grunt.file.isFile(p)) { | ||
@@ -68,9 +118,55 @@ grunt.log.warn('Included file "' + p + '" not found.'); | ||
var src = grunt.file.read(p).split(grunt.util.linefeed); | ||
var compiled = src.map(function(line) { | ||
var match = line.match(regex); | ||
/** | ||
* If `opts.duplicates` is false and file has been included, | ||
* error | ||
*/ | ||
if(!opts.duplicates && ~included.indexOf(p)) { | ||
error = 'Duplicate include: ' + p + ' skipping.'; | ||
grunt.log.error(error); | ||
if(opts.debug) { | ||
return comment.replace(/%s/g, error); | ||
} else { | ||
return ''; | ||
} | ||
} | ||
/** | ||
* At this point the file is considered included | ||
*/ | ||
included.push(p); | ||
/** | ||
* Split the file on newlines | ||
*/ | ||
src = grunt.file.read(p).split(grunt.util.linefeed); | ||
/** | ||
* Loop through the file calling `recurse` if an include is found | ||
*/ | ||
compiled = src.map(function(line) { | ||
match = line.match(opts.includeRegexp); | ||
/** | ||
* If the line has an include statement, recurse | ||
*/ | ||
if(match) { | ||
return recurse(path.join(path.dirname(p), match[2])); | ||
next = path.join(path.dirname(p), match[1]); | ||
line = recurse(next, opts, included); | ||
/** | ||
* Include debug comments if `opts.debug` | ||
*/ | ||
if(opts.debug) { | ||
line = comment.replace(/%s/g, 'Begin: ' + next) + | ||
'\n' + line + '\n' + comment.replace(/%s/g, 'End: ' + next); | ||
} | ||
} | ||
return line; | ||
@@ -77,0 +173,0 @@ }); |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
7563
12
131
1
0