gulp-include
Advanced tools
Comparing version 2.1.1 to 2.2.0
44
index.js
@@ -12,4 +12,5 @@ var fs = require('fs'), | ||
var extensions = null, | ||
includedFiles = []; | ||
var extensions = null, // The extension to be searched after | ||
includedFiles = [], // Keeping track of what files have been included | ||
includePaths = false; // The paths to be searched | ||
@@ -20,2 +21,14 @@ module.exports = function (params) { | ||
extensions = null; | ||
includePaths = false; | ||
// Check for includepaths in the params | ||
if (params.includePaths) { | ||
if (typeof params.includePaths == "string") { | ||
// Arrayify the string | ||
includePaths = [params.includePaths]; | ||
}else if (Array.isArray(params.includePaths)) { | ||
// Set this array to the includepaths | ||
includePaths = params.includePaths; | ||
} | ||
} | ||
@@ -66,2 +79,3 @@ if (params.extensions) { | ||
// Apply sourcemaps | ||
var map = null, mapSelf, lastMappedLine, currentPos, insertedLines; | ||
@@ -112,6 +126,2 @@ if (sourceMap) { | ||
// Split the directive and the path | ||
var includeType = split[0]; | ||
var includePath = relativeBasePath + "/" + split[1]; | ||
var currentLine; | ||
@@ -130,4 +140,24 @@ if (sourceMap) { | ||
// SEARCHING STARTS HERE | ||
// Split the directive and the path | ||
var includeType = split[0]; | ||
// Use glob for file searching | ||
var fileMatches = glob.sync(includePath, {mark: true}); | ||
var fileMatches = []; | ||
var includePath = ""; | ||
if (includePaths != false) { | ||
// If includepaths are set, search in those folders | ||
for (var y = 0; y < includePaths.length; y++) { | ||
var includePath = includePaths[y] + "/" + split[1]; | ||
var globResults = glob.sync(includePath, {mark: true}); | ||
fileMatches = fileMatches.concat(globResults); | ||
} | ||
}else{ | ||
// Otherwise search relatively | ||
var includePath = relativeBasePath + "/" + split[1]; | ||
fileMatches = glob.sync(includePath, {mark: true}); | ||
} | ||
var replaceContent = ''; | ||
@@ -134,0 +164,0 @@ for (var y = 0; y < fileMatches.length; y++) { |
{ | ||
"name": "gulp-include", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Makes inclusion of files a breeze. Enables functionality similar to that of snockets / sprockets or other file insertion compilation tools.", | ||
@@ -5,0 +5,0 @@ "homepage": "http://github.com/wiledal/gulp-include", |
@@ -39,5 +39,9 @@ #gulp-include [![NPM version][npm-image]][npm-url] ![Travis build][travis-image] | ||
## Options | ||
* `extensions` (optional) | ||
- `extensions` (optional) | ||
* Takes a `String` or an `Array` of extensions, eg: `"js"` or `["js", "coffee"]` | ||
* If set, all directives that does not match the extension(s) will be ignored | ||
- `includePaths` (optional) | ||
* Takes a `String` or an `Array` of paths, | ||
eg: `__dirname + "/node_modules"` or `[__dirname + "/assets/js", __dirname + "/bower_components"]` | ||
* If set, `gulp-include` will use these folders as base path when searching for files. | ||
@@ -75,2 +79,5 @@ ## Include directives | ||
## Release log | ||
#### 2.2.0 | ||
* Added `includePaths` option | ||
#### 2.1.1 | ||
@@ -77,0 +84,0 @@ * Strip BOMs, by [dhedey](https://github.com/dhedey) |
@@ -8,3 +8,4 @@ var gutil = require("gulp-util"), | ||
gulp = require('gulp'), | ||
sourcemaps = require('gulp-sourcemaps'); | ||
sourcemaps = require('gulp-sourcemaps'), | ||
path = require("path"); | ||
@@ -175,2 +176,26 @@ | ||
}) | ||
it("should include from set includePaths", function(done) { | ||
var file = new gutil.File({ | ||
base: "test/fixtures/", | ||
path: "test/fixtures/js/include-path.js", | ||
contents: fs.readFileSync("test/fixtures/js/include-path.js") | ||
}); | ||
testInclude = include({ | ||
includePaths: [ | ||
__dirname + "/fixtures/js/include-path", | ||
__dirname + "/fixtures/js/include-path2", | ||
__dirname + "/fixtures/js/include-path2/deeper2", | ||
] | ||
}); | ||
testInclude.on("data", function (newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/js/include-path.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}) | ||
}) |
26955
37
503
166