From 572713c237740c3f284ffed3dabd6bc0316a0a59 Mon Sep 17 00:00:00 2001 From: Riccardo Caroli Date: Wed, 22 Oct 2014 23:34:28 +0200 Subject: [PATCH 1/3] support for prepend and append with // @codekit-prepend path/to/file.js // @codekit-append path/to/file.js --- index.js | 51 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 1243999..6843674 100644 --- a/index.js +++ b/index.js @@ -4,11 +4,14 @@ var fs = require('fs'), gutil = require('gulp-util'), glob = require('glob'); - var DIRECTIVE_REGEX = /^[\/\s#]*?=\s*?((?:require|include)(?:_tree|_directory)?)\s+(.*$)/mg; +var PREPEND_REGEX = /^[\/\s#]*\s*?((?:@codekit-prepend))\s+(.*$)/mg; +var APPEND_REGEX = /^[\/\s#]*\s*?((?:@codekit-append))\s+(.*$)/mg; var requiredFiles = {}, - extensions = []; + extensions = [], + prependCache = '', + appendCache = ''; module.exports = function (params) { var params = params || {}; @@ -29,7 +32,11 @@ module.exports = function (params) { } if (file.isBuffer()) { - var newText = expand(String(file.contents), file.path); + var newText = expand(String(file.contents), file.path, DIRECTIVE_REGEX); + newText = expand(newText, file.path, PREPEND_REGEX); + newText = prependCache + newText; + newText = expand(newText, file.path, APPEND_REGEX); + newText = newText + appendCache; file.contents = new Buffer(newText); } @@ -39,15 +46,15 @@ module.exports = function (params) { return es.map(include) }; -function expand(fileContents, filePath) { +function expand(fileContents, filePath, regex) { var regexMatch, matches = [], returnText = fileContents, i, j; - DIRECTIVE_REGEX.lastIndex = 0; + regex.lastIndex = 0; - while (regexMatch = DIRECTIVE_REGEX.exec(fileContents)) { + while (regexMatch = regex.exec(fileContents)) { matches.push(regexMatch); } @@ -70,7 +77,7 @@ function expand(fileContents, filePath) { for (j = 0; j < files.length; j++) { fileName = files[j]; - newMatchText = expand(String(fs.readFileSync(fileName)), fileName); + newMatchText = expand(String(fs.readFileSync(fileName)), fileName, regex); //Try to retain the same indent level from the original include line whitespace = original.match(/^\s+/); @@ -85,15 +92,21 @@ function expand(fileContents, filePath) { } thisMatchText += newMatchText + "\n"; - - if (directiveType.indexOf('require') !== -1 || directiveType.indexOf('include') !== -1) { + + if (directiveType.indexOf('require') !== -1 || directiveType.indexOf('include') !== -1 || directiveType.indexOf('codekit') !== -1) { requiredFiles[fileName] = true; } } thisMatchText = thisMatchText || original; - returnText = replaceStringByIndices(returnText, start, end, thisMatchText); + if(directiveType === '@codekit-prepend'){ + returnText = prependString(returnText, start, end, thisMatchText); + }else if(directiveType === '@codekit-append'){ + returnText = appendString(returnText, start, end, thisMatchText); + }else{ + returnText = replaceStringByIndices(returnText, start, end, thisMatchText); + } } return returnText ? returnText : fileContents; @@ -116,7 +129,7 @@ function globMatch(match, filePath) { directiveType = directiveType.replace('_directory', ''); } - if (directiveType === 'require' || directiveType === 'include') { + if (directiveType === 'require' || directiveType === 'include' || directiveType.indexOf('codekit') !== -1) { if (relativeFilePath.charAt(0) === '[') { relativeFilePath = eval(relativeFilePath); for (var i = 0; i < relativeFilePath.length; i++) { @@ -177,6 +190,18 @@ function _internalGlob(thisGlob, filePath) { function replaceStringByIndices(string, start, end, replacement) { return string.substring(0, start) + replacement + string.substring(end); } +function prependString(string, start, end, prepend) { + // cache prepend + prependCache = prepend + prependCache; + // remove directive + return string.substring(0, start) + string.substring(end); +} +function appendString(string, start, end, append) { + // cache append + appendCache = append + appendCache; + // remove directive + return string.substring(0, start) + string.substring(end); +} function addLeadingWhitespace(whitespace, string) { return string.split("\n").map(function(line) { @@ -189,7 +214,7 @@ function union(arr1, arr2) { if (arr1.length == 0) { return arr2; } - + var index; for (var i = 0; i < arr2.length; i++) { if ((index = arr1.indexOf(arr2[i])) !== -1) { @@ -207,4 +232,4 @@ function difference(arr1, arr2) { } } return arr1; -} \ No newline at end of file +} From ec63da3dd7febaa66cf1dd7b982b461d5c715399 Mon Sep 17 00:00:00 2001 From: Riccardo Caroli Date: Thu, 23 Oct 2014 10:27:54 +0200 Subject: [PATCH 2/3] fix clear cache + optional [] on multiple files --- index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index.js b/index.js index 6843674..82eabbf 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,8 @@ module.exports = function (params) { var params = params || {}; requiredFiles = {}; extensions = []; + prependCache = ''; + appendCache = ''; if (params.extensions) { extensions = typeof params.extensions === 'string' ? [params.extensions] : params.extensions; @@ -130,6 +132,10 @@ function globMatch(match, filePath) { } if (directiveType === 'require' || directiveType === 'include' || directiveType.indexOf('codekit') !== -1) { + if (relativeFilePath.charAt(0).match(/['"]/g)) { + // optional [] on multiple files + relativeFilePath = '[' + relativeFilePath + ']'; + } if (relativeFilePath.charAt(0) === '[') { relativeFilePath = eval(relativeFilePath); for (var i = 0; i < relativeFilePath.length; i++) { From 9f84f1ce2f0129c70ab170d6610f4fcfc50954b2 Mon Sep 17 00:00:00 2001 From: Riccardo Caroli Date: Thu, 23 Oct 2014 10:42:07 +0200 Subject: [PATCH 3/3] prepend and append on readme --- README.md | 9 +++++++++ index.js | 4 +--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8165137..8fba020 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,15 @@ gulp.task("scripts", function() { gulp.task("default", "scripts"); ``` +## Prepend and Append +We support prepend and append to the file with the codekit syntax: + +```javascript +// @codekit-prepend relative/path/to/file.js +// @codekit-append relative/path/to/file.js +// @codekit-prepend "relative/path/to/file.js ", "relative/path/to/file.js " +``` + ## Options * `extensions` (optional) * Takes a `String` or an `Array` of extensions, eg: `"js"` or `["js", "coffee"]` diff --git a/index.js b/index.js index 82eabbf..59ed3e2 100644 --- a/index.js +++ b/index.js @@ -9,9 +9,7 @@ var PREPEND_REGEX = /^[\/\s#]*\s*?((?:@codekit-prepend))\s+(.*$)/mg; var APPEND_REGEX = /^[\/\s#]*\s*?((?:@codekit-append))\s+(.*$)/mg; var requiredFiles = {}, - extensions = [], - prependCache = '', - appendCache = ''; + extensions = []; module.exports = function (params) { var params = params || {};