gulp-include
Advanced tools
Comparing version 0.1.0 to 0.2.0
82
index.js
var fs = require("fs"), | ||
path = require("path"), | ||
es = require("event-stream"), | ||
gutil = require("gulp-util"); | ||
DIRECTIVE_REGEX = /^(.*=\s*(\w+.*?))$/gm | ||
function getFiles(dir, cb){ | ||
var files = fs.readdirSync(dir); | ||
for(var i in files){ | ||
if (!files.hasOwnProperty(i)) continue; | ||
var name = dir+'/'+files[i]; | ||
if (fs.statSync(name).isDirectory()){ | ||
getFiles(name, cb); | ||
}else{ | ||
cb(name); | ||
} | ||
} | ||
} | ||
function matchExtension(extension, params) { | ||
if (params.extensions) { | ||
if (Array.isArray(params.extensions)) { | ||
if (params.extensions.indexOf(extension) > -1) return true; | ||
} else if (typeof params.extensions == "string") { | ||
if (params.extensions == extension) return true; | ||
} else { | ||
throw new gutil.PluginError('gulp-include', 'extensions param only allows Array or String'); | ||
} | ||
}else{ | ||
return true; | ||
} | ||
return false; | ||
} | ||
module.exports = function(params) { | ||
var params = params || {}; | ||
function include(file, callback) { | ||
@@ -24,12 +56,50 @@ if (file.isNull()) { | ||
while (matches = DIRECTIVE_REGEX.exec(text)) { | ||
if (matches[1].match(/include|require/)) { | ||
if (matches[1].match(/include_tree|require_tree/)) { | ||
var match = matches[1], | ||
directive = matches[2].replace(/['"]/g, '').split(/\s+/), | ||
relPath = file.base, | ||
fullPath = relPath + directive[1], | ||
absolutePath = path.resolve(fullPath); | ||
if (fs.existsSync(fullPath)) { | ||
var stats = fs.statSync(fullPath); | ||
if (stats.isDirectory()) { | ||
var filesStr = ""; | ||
getFiles(absolutePath, function(fileName) { | ||
if (fs.existsSync(fileName)) { | ||
var extension = fileName.split(".").pop(); | ||
currentExtension = extension; | ||
if (matchExtension(extension, params)) { | ||
var includeContent = String(fs.readFileSync(fileName)); | ||
filesStr = filesStr + includeContent + gutil.linefeed; | ||
} | ||
} else { | ||
throw new gutil.PluginError('gulp-include', 'File not found: ' + fullPath); | ||
} | ||
}); | ||
var replaceWith = "" | ||
if (params.extensions) { | ||
replaceWith = match + gutil.linefeed + filesStr; | ||
} else { | ||
replaceWith = filesStr; | ||
} | ||
newText = newText.replace(match, replaceWith); | ||
} | ||
} | ||
} else if (matches[1].match(/include|require/)) { | ||
var match = matches[1], | ||
directive = matches[2].replace(/['"]/g, '').split(/\s+/), | ||
relPath = file.base, | ||
fullPath = relPath + directive[1]; | ||
extension = directive[1].split(".").pop() | ||
if (fs.existsSync(fullPath)) { | ||
var includeContent = String(fs.readFileSync(fullPath)); | ||
newText = newText.replace(match, includeContent); | ||
if (matchExtension(extension, params)) { | ||
var includeContent = String(fs.readFileSync(fullPath)); | ||
newText = newText.replace(match, includeContent + gutil.linefeed); | ||
} | ||
} else { | ||
@@ -47,2 +117,2 @@ throw new gutil.PluginError('gulp-include', 'File not found: ' + fullPath); | ||
return es.map(include) | ||
} | ||
} |
{ | ||
"name": "gulp-include", | ||
"version": "0.1.0", | ||
"version": "0.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", |
@@ -13,3 +13,3 @@ #gulp-include [![NPM version][npm-image]][npm-url] ![Travis build][travis-image] | ||
Then, add your _include-comments_ to your file. | ||
_People who have experience with `sprockets` or `snockets` will feel at home._ | ||
_People who have experience with `sprockets` or `snockets` will feel at home._ | ||
@@ -23,3 +23,3 @@ | ||
```coffeescript | ||
#= include relative/path/to/file.coffee | ||
#= require_tree relative/path/to/folder | ||
``` | ||
@@ -31,9 +31,9 @@ or even | ||
= require relative/path/to/file2.js | ||
= include relative/path/to/file.js | ||
= include_tree relative/path/to/folder | ||
*/ | ||
``` | ||
`gulp-include` does not care about whitespace, as long as the comment-line starts with a _newline_ followed `=` and contains `include` or `require` | ||
`gulp-include` does not care about whitespace, as long as the comment-line starts with a _newline_ followed `=` and contains `include`, `require` or `include_tree`, `require_tree`. | ||
The example below compiles a coffee-file with a heap of inclusion inside into a single js-file: | ||
The example below compiles a several coffee-files and js-files into a single js-file: | ||
@@ -43,8 +43,11 @@ `app.coffee`: | ||
```coffeescript | ||
#= require views/AppView.coffee | ||
#= require views/LandingView.coffee | ||
#= require views/AboutView.coffee | ||
#= require views/CheeseView.coffee | ||
` | ||
//= require vendor/jquery.js | ||
//= require vendor/modernizr.js | ||
` | ||
class Main extends AppView | ||
#= require controllers/AppController.coffee | ||
#= require_tree views | ||
class Main extends AppController | ||
constructor: -> | ||
@@ -55,2 +58,3 @@ console.log "This is main!" | ||
``` | ||
*Note:* The example above uses backsticks (\`) to allow `gulp-coffee` to compile inline javascript | ||
@@ -74,2 +78,16 @@ `gulpfile.js`: | ||
## Options | ||
* `extensions` (optional) | ||
* Takes a `String` or an `Array` of extensions, eg: `"js"` or `["js", "coffee"]` | ||
* If set, all inclusions that does not match the extension(s) will be ignored | ||
## Release log | ||
#### 0.2.0 | ||
* Added `require_tree`/`include_tree` (Thanks to [juanghurtado](https://github.com/juanghurtado)!) | ||
* Method now takes an `extensions` param for controlling what types of files to include | ||
#### 0.1.0 | ||
* Basic include | ||
## Licence | ||
@@ -76,0 +94,0 @@ (MIT License) |
@@ -0,1 +1,3 @@ | ||
//= include header.txt | ||
// =include lib/a.js | ||
@@ -16,2 +18,3 @@ /* | ||
// =include lib/b.js | ||
//= require lib/nested/c.js | ||
//= require lib/nested/c.js | ||
// = require_tree lib/nested/deeply_nested |
@@ -21,3 +21,3 @@ var gutil = require("gulp-util"), | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/app.js"), "utf8")) | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/app_all_extensions.js"), "utf8")) | ||
done(); | ||
@@ -27,2 +27,42 @@ }); | ||
}); | ||
it("should only include the files with the provided SINGLE extension", function(done) { | ||
var file = new gutil.File({ | ||
base: "test/fixatures/", | ||
path: "test/fixatures/app.js", | ||
contents: fs.readFileSync("test/fixatures/app.js") | ||
}); | ||
testInclude = include({ | ||
extensions: "txt" | ||
}); | ||
testInclude.on("data", function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/app_only_txt.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}); | ||
it("should only include the files with the provided MULTIPLE extensions", function(done) { | ||
var file = new gutil.File({ | ||
base: "test/fixatures/", | ||
path: "test/fixatures/app.js", | ||
contents: fs.readFileSync("test/fixatures/app.js") | ||
}); | ||
testInclude = include({ | ||
extensions: ["txt", "js"] | ||
}); | ||
testInclude.on("data", function(newFile) { | ||
should.exist(newFile); | ||
should.exist(newFile.contents); | ||
String(newFile.contents).should.equal(String(fs.readFileSync("test/expected/app_multiple_extensions.js"), "utf8")) | ||
done(); | ||
}); | ||
testInclude.write(file); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10393
19
222
114