Skip to content
Open
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
51 changes: 33 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,31 +225,42 @@ var parseJSON = function parseJSON(data, filename) {
* Asynchronously walks the file list until a match is found. If
* no matches are found, calls the callback with an error
**/
var readFirstFile = function readFirstFile(uri, filenames, css, cb, examinedFiles) {
var readFirstFile = function readFirstFile(uri, filenames, options, cb, examinedFiles) {
var filename = filenames.shift();
examinedFiles = examinedFiles || [];
examinedFiles.push(filename);
fs.readFile(filename, function(err, data) {
if (err) {
if (filenames.length) {
readFirstFile(uri, filenames, css, cb, examinedFiles);
fs.readFile(filename, (function (filename) {
return function(err, data) {
if (err) {
if (filenames.length) {
readFirstFile(uri, filenames, options, cb, examinedFiles);
}
else {
cb(new Error('Could not import `' + uri + '` from any of the following locations:\n ' + examinedFiles.join('\n ')));
}
}
else {
cb(new Error('Could not import `' + uri + '` from any of the following locations:\n ' + examinedFiles.join('\n ')));
}
}
else {
if ([ '.js', '.json', '.yml', '.yaml' ].indexOf(path.extname(filename)) !== -1) {
data = parseJSON(data, filename);
if ([ '.js', '.json', '.yml', '.yaml' ].indexOf(path.extname(filename)) !== -1) {
data = parseJSON(data, filename);
}

var contents = data.toString();
var tmp = options.importOnce.transformContent(filename, contents);
if (typeof tmp != 'undefined') {
contents = tmp;
}

cb(null, {
'contents': contents,
'file': filename
});
}
cb(null, {
'contents': data.toString(),
'file': filename
});
}
});
};
})(filename));
};

var noop = function noop () {};

// This is a bootstrap function for calling readFirstFile.
var readAbstractFile = function readAbstractFile(uri, abstractName, cb) {
var gfn = getFileNames.bind(this),
Expand All @@ -267,7 +278,7 @@ var readAbstractFile = function readAbstractFile(uri, abstractName, cb) {
files = files.concat(gbn(uri));
}

readFirstFile(uri, files, css, cb);
readFirstFile(uri, files, this.options, cb);
};

/**
Expand Down Expand Up @@ -299,6 +310,10 @@ var importer = function importer(uri, prev, done) {
this.options.importOnce.css = false;
}

if (typeof this.options.importOnce.transformContent != 'function') {
this.options.importOnce.transformContent = noop;
}

// Create an import cache if it doesn't exist
if (!this._importOnceCache) {
this._importOnceCache = {};
Expand Down