Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for prepend and append #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"]`
Expand Down
53 changes: 41 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ 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 = [];
Expand All @@ -14,6 +15,8 @@ module.exports = function (params) {
var params = params || {};
requiredFiles = {};
extensions = [];
prependCache = '';
appendCache = '';

if (params.extensions) {
extensions = typeof params.extensions === 'string' ? [params.extensions] : params.extensions;
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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+/);
Expand All @@ -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;
Expand All @@ -116,7 +129,11 @@ 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).match(/['"]/g)) {
// optional [] on multiple files
relativeFilePath = '[' + relativeFilePath + ']';
}
if (relativeFilePath.charAt(0) === '[') {
relativeFilePath = eval(relativeFilePath);
for (var i = 0; i < relativeFilePath.length; i++) {
Expand Down Expand Up @@ -177,6 +194,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) {
Expand All @@ -189,7 +218,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) {
Expand All @@ -207,4 +236,4 @@ function difference(arr1, arr2) {
}
}
return arr1;
}
}