forked from linkedin/eyeglass
/
FSImporter.js
44 lines (40 loc) · 1.45 KB
/
FSImporter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"use strict";
var path = require("path");
var ImportUtilities = require("./ImportUtilities");
var fileUtils = require("../util/files");
function FSImporter(eyeglass, sass, options, fallbackImporter) {
var fsURI = /^fs\(([-_a-zA-Z][-_a-zA-Z0-9]+)\)$/;
return ImportUtilities.createImporter(function(uri, prev, done) {
var importUtils = new ImportUtilities(eyeglass, sass, options, fallbackImporter, this);
var match = uri.match(fsURI);
if (match) {
var identifier = match[1];
var absolutePath = null;
if (identifier === "root") {
absolutePath = options.eyeglass.root;
} else if (!fileUtils.existsSync(prev)) {
absolutePath = path.resolve(".");
} else {
absolutePath = path.resolve(path.dirname(prev));
}
/* istanbul ignore else - TODO: revisit this */
if (absolutePath) {
var sassContents = '@import "eyeglass/fs"; @include fs-register-path('
+ identifier + ', "' + absolutePath + '");';
var data = {
contents: sassContents,
file: "fs:" + identifier + ":" + absolutePath
};
importUtils.importOnce(data, done);
} else {
// TODO (test) - how do we get here? needs test case
done(new Error("Cannot resolve filesystem location of " + prev));
}
} else {
importUtils.fallback(uri, prev, done, function() {
done(sass.NULL);
});
}
});
}
module.exports = FSImporter;