From 61764d51231138505237c4b40d2dbd94868508c8 Mon Sep 17 00:00:00 2001 From: Xlander11 Date: Fri, 11 Dec 2015 20:35:53 +0400 Subject: [PATCH 1/2] Added possibility to add wildcards in list --- index.js | 128 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 75 insertions(+), 53 deletions(-) diff --git a/index.js b/index.js index fda2105..ca6a8de 100644 --- a/index.js +++ b/index.js @@ -11,92 +11,114 @@ var through = require('through2'), var PLUGIN_NAME = 'gulp-include-source'; var placeholders = { - 'js' : '', - 'css' : '' + 'js': '', + 'css': '' }; function matchExpressions(contents) { - return contents.match(//); + return contents.match(//); } function replaceExtension(filename, type, options) { - if( options.scriptExt && type === 'js' ) { - filename = replaceExt(filename, '.' + options.scriptExt); - } else if( options.styleExt && type === 'css' ) { - filename = replaceExt(filename, '.' + options.styleExt); - } + if (options.scriptExt && type === 'js') { + filename = replaceExt(filename, '.' + options.scriptExt); + } else if (options.styleExt && type === 'css') { + filename = replaceExt(filename, '.' + options.styleExt); + } - return filename; + return filename; } function parseFiles(source, cwd) { - if( source.indexOf('list:') === 0 ) { - var cleanSrc = source.replace('list:', ''); - return fs.readFileSync( cleanSrc ).toString().split('\n'); - } + if (source.indexOf('list:') === 0) { + var cleanSrc = source.replace('list:', ''); + var re = fs.readFileSync(cleanSrc).toString().split('\n'); + var fileGlob = null, + arrayLength = re.length; + + //Allows wild cards to be implemented in list + for(var i = 0;i < arrayLength;i++){ + var val = re[i]; + + if(val.match(/(\*+)/)){ + fileGlob = glob.sync(val, {cwd: cwd}); + Array.prototype.splice.apply(re, [i, 1].concat(fileGlob)); + arrayLength = re.length; + } + } - return glob.sync( source, { cwd : cwd } ); + return re; + } + + return glob.sync(source, {cwd: cwd}); } function injectFiles(file, options) { + var contents = file.contents.toString(); + var cwd = options.cwd || path.dirname(file.path); + var matches = matchExpressions(contents); - var contents = file.contents.toString(); - var cwd = options.cwd || path.dirname(file.path); - var matches = matchExpressions(contents); + while (matches) { - while( matches ) { + var type = matches[1]; + var placeholder = placeholders[type]; + var files = parseFiles(matches[2], cwd); + var includes = ''; - var type = matches[1]; - var placeholder = placeholders[ type ]; - var files = parseFiles(matches[2], cwd); - var includes = ''; + if (placeholder && files && files.length > 0) { - if( placeholder && files && files.length > 0 ) { + includes = files.map(function (filename) { + filename = replaceExtension(filename, type, options); + return placeholder.split('%').join(filename); + }).join('\n'); + } - includes = files.map(function(filename) { - filename = replaceExtension(filename, type, options); - return placeholder.split('%').join(filename); - }).join('\n'); + contents = contents.substring(0, matches.index) + includes + contents.substring(matches.index + matches[0].length); + matches = matchExpressions(contents); } - contents = contents.substring(0, matches.index) + includes + contents.substring(matches.index + matches[0].length); - matches = matchExpressions(contents); - } - - return contents; + return contents; } function gulpIncludeSource(options) { - options = options || {}; + options = options || {}; - var stream = through.obj(function(file, enc, callback) { + var stream = through.obj(function (file, enc, callback) { - if (file.isNull()) { - this.push(file); // Do nothing if no contents - return callback(); - } + if (file.isNull()) { + this.push(file); // Do nothing if no contents + return callback(); + } - if (file.isStream()) { - this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported!')); - return callback(); - } + if (file.isStream()) { + this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported!')); + return callback(); + } - if (file.isBuffer()) { - try { - file.contents = new Buffer( injectFiles( file, options ) ); - } catch (err) { - this.emit('error', new gutil.PluginError(PLUGIN_NAME, err)); - } - } + if (file.isBuffer()) { + try { + file.contents = new Buffer(injectFiles(file, options)); + } catch (err) { + this.emit('error', new gutil.PluginError(PLUGIN_NAME, err)); + } + } - this.push(file); - return callback(); - }); + this.push(file); + return callback(); + }); - return stream; + return stream; } + +fs.readFile("./index.tpl.html", 'utf8', function (err, data) { + if(err) throw err; + + var x = {contents: data}; + injectFiles(x, {}); +}); + module.exports = gulpIncludeSource; \ No newline at end of file From 39affc516b370be061edce0fda0f05bc56e0fff5 Mon Sep 17 00:00:00 2001 From: Xlander11 Date: Fri, 11 Dec 2015 20:40:33 +0400 Subject: [PATCH 2/2] Added possibility to add wildcards in list --- index.js | 150 ++++++++++++++++++++++++++----------------------------- 1 file changed, 71 insertions(+), 79 deletions(-) diff --git a/index.js b/index.js index ca6a8de..aefc1a0 100644 --- a/index.js +++ b/index.js @@ -1,124 +1,116 @@ 'use strict'; var through = require('through2'), - glob = require('glob'), - path = require('path'), - replaceExt = require('replace-ext'), - gutil = require('gulp-util'), - fs = require('fs'), - PluginError = gutil.PluginError; +glob = require('glob'), +path = require('path'), +replaceExt = require('replace-ext'), +gutil = require('gulp-util'), +fs = require('fs'), +PluginError = gutil.PluginError; var PLUGIN_NAME = 'gulp-include-source'; var placeholders = { - 'js': '', - 'css': '' + 'js': '', + 'css': '' }; function matchExpressions(contents) { - return contents.match(//); + return contents.match(//); } function replaceExtension(filename, type, options) { - if (options.scriptExt && type === 'js') { - filename = replaceExt(filename, '.' + options.scriptExt); - } else if (options.styleExt && type === 'css') { - filename = replaceExt(filename, '.' + options.styleExt); - } + if (options.scriptExt && type === 'js') { + filename = replaceExt(filename, '.' + options.scriptExt); + } else if (options.styleExt && type === 'css') { + filename = replaceExt(filename, '.' + options.styleExt); + } - return filename; + return filename; } function parseFiles(source, cwd) { - if (source.indexOf('list:') === 0) { - var cleanSrc = source.replace('list:', ''); - var re = fs.readFileSync(cleanSrc).toString().split('\n'); - var fileGlob = null, - arrayLength = re.length; - - //Allows wild cards to be implemented in list - for(var i = 0;i < arrayLength;i++){ - var val = re[i]; + if (source.indexOf('list:') === 0) { + var cleanSrc = source.replace('list:', ''); + var re = fs.readFileSync(cleanSrc).toString().split('\n'); + var fileGlob = null, + arrayLength = re.length; - if(val.match(/(\*+)/)){ - fileGlob = glob.sync(val, {cwd: cwd}); - Array.prototype.splice.apply(re, [i, 1].concat(fileGlob)); - arrayLength = re.length; - } - } + //Allows wild cards to be implemented in list + for (var i = 0; i < arrayLength; i++) { + var val = re[i]; - return re; + if (val.match(/(\*+)/)) { + fileGlob = glob.sync(val, {cwd: cwd}); + Array.prototype.splice.apply(re, [i, 1].concat(fileGlob)); + arrayLength = re.length; + } } - return glob.sync(source, {cwd: cwd}); + return re; + } + + return glob.sync(source, {cwd: cwd}); } function injectFiles(file, options) { - var contents = file.contents.toString(); - var cwd = options.cwd || path.dirname(file.path); - var matches = matchExpressions(contents); - - while (matches) { + var contents = file.contents.toString(); + var cwd = options.cwd || path.dirname(file.path); + var matches = matchExpressions(contents); - var type = matches[1]; - var placeholder = placeholders[type]; - var files = parseFiles(matches[2], cwd); - var includes = ''; + while (matches) { - if (placeholder && files && files.length > 0) { + var type = matches[1]; + var placeholder = placeholders[type]; + var files = parseFiles(matches[2], cwd); + var includes = ''; - includes = files.map(function (filename) { - filename = replaceExtension(filename, type, options); - return placeholder.split('%').join(filename); - }).join('\n'); - } + if (placeholder && files && files.length > 0) { - contents = contents.substring(0, matches.index) + includes + contents.substring(matches.index + matches[0].length); - matches = matchExpressions(contents); + includes = files.map(function (filename) { + filename = replaceExtension(filename, type, options); + return placeholder.split('%').join(filename); + }).join('\n'); } - return contents; + contents = contents.substring(0, matches.index) + includes + contents.substring(matches.index + matches[0].length); + matches = matchExpressions(contents); + } + + return contents; } function gulpIncludeSource(options) { - options = options || {}; + options = options || {}; - var stream = through.obj(function (file, enc, callback) { + var stream = through.obj(function (file, enc, callback) { - if (file.isNull()) { - this.push(file); // Do nothing if no contents - return callback(); - } + if (file.isNull()) { + this.push(file); // Do nothing if no contents + return callback(); + } - if (file.isStream()) { - this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported!')); - return callback(); - } + if (file.isStream()) { + this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported!')); + return callback(); + } - if (file.isBuffer()) { - try { - file.contents = new Buffer(injectFiles(file, options)); - } catch (err) { - this.emit('error', new gutil.PluginError(PLUGIN_NAME, err)); - } - } + if (file.isBuffer()) { + try { + file.contents = new Buffer(injectFiles(file, options)); + } catch (err) { + this.emit('error', new gutil.PluginError(PLUGIN_NAME, err)); + } + } - this.push(file); - return callback(); - }); + this.push(file); + return callback(); + }); - return stream; + return stream; } - -fs.readFile("./index.tpl.html", 'utf8', function (err, data) { - if(err) throw err; - - var x = {contents: data}; - injectFiles(x, {}); -}); - module.exports = gulpIncludeSource; \ No newline at end of file