Skip to content

minimit's support codekit prepend and append #40

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

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#gulp-include [![NPM version][npm-image]][npm-url] ![Travis build][travis-image]
>Makes inclusion of files a breeze.
>Makes inclusion of files a breeze.
Enables functionality similar to that of snockets / sprockets or other file insertion compilation tools.

> Made for gulp 3
Expand All @@ -9,7 +9,7 @@ Enables functionality similar to that of snockets / sprockets or other file inse
First, install `gulp-include` as a dev dependency:
`npm install --save-dev gulp-include`

Then, add your _include-comments_ to your file.
Then, add your _include-comments_ to your file.
_People who have experience with `sprockets` or `snockets` will feel at home._


Expand Down 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
49 changes: 40 additions & 9 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 @@ -86,14 +93,20 @@ 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 @@ -178,6 +195,20 @@ 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) {
return whitespace + line;
Expand Down