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

add support for autoExtensions #80

Open
wants to merge 1 commit 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ gulp.task("default", ["scripts"]);
eg: `"js"` or `["js", "coffee"]`
* If set, all directives that does not match the extension(s) will be ignored

- `autoExtensions` (optional)
* Takes a `String` or an `Array` of extensions.
eg: `"js"` or `["js", "coffee"]`
* If set, the extensions will be added to the search pattern when looking for files
eg: `//= require ./somescript` will resolve to `./somescript{,.js,.coffee}`

- `includePaths` (optional)
* Takes a `String` or an `Array` of paths.
Expand Down
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module.exports = function (params) {
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
hardFail = false; // Throw error when no match
hardFail = false, // Throw error when no match
autoExtensions = false; // additional extensions added to the filename for search

// Check for includepaths in the params
if (params.includePaths) {
Expand All @@ -37,6 +38,11 @@ module.exports = function (params) {
extensions = typeof params.extensions === 'string' ? [params.extensions] : params.extensions;
}

if (params.autoExtensions) {
autoExtensions = typeof params.autoExtensions === 'string' ? [params.autoExtensions] : params.autoExtensions;
autoExtensions = autoExtensions.map(function (ext) { return '.' + ext })
}

function include(file, callback) {
if (file.isNull()) {
return callback(null, file);
Expand Down Expand Up @@ -136,22 +142,28 @@ module.exports = function (params) {
// SEARCHING STARTS HERE
// Split the directive and the path
var includeType = split[0];
var fileName = split[1];

// Use glob for file searching
var fileMatches = [];
var includePath = "";

// add the extensions from autoExtensions if any
if (autoExtensions != false) {
fileName = fileName + '{,' + autoExtensions.join(',') + '}';
}

if (includePaths != false) {
// If includepaths are set, search in those folders
for (var y = 0; y < includePaths.length; y++) {
includePath = includePaths[y] + "/" + split[1];
includePath = includePaths[y] + "/" + fileName;

var globResults = glob.sync(includePath, {mark: true});
fileMatches = fileMatches.concat(globResults);
}
}else{
// Otherwise search relatively
includePath = relativeBasePath + "/" + split[1];
includePath = relativeBasePath + "/" + fileName;
var globResults = glob.sync(includePath, {mark: true});
fileMatches = globResults;
}
Expand Down