-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
file-manager.js
108 lines (92 loc) · 3.56 KB
/
file-manager.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
var path = require('path'),
fs = require('./fs'),
PromiseConstructor,
AbstractFileManager = require("../less/environment/abstract-file-manager.js");
try {
PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise;
} catch(e) {
}
var FileManager = function() {
};
FileManager.prototype = new AbstractFileManager();
FileManager.prototype.supports = function(filename, currentDirectory, options, environment) {
return true;
};
FileManager.prototype.supportsSync = function(filename, currentDirectory, options, environment) {
return true;
};
FileManager.prototype.loadFile = function(filename, currentDirectory, options, environment, callback) {
var fullFilename,
data,
isAbsoluteFilename = this.isPathAbsolute(filename),
filenamesTried = [];
options = options || {};
if (options.syncImport || !PromiseConstructor) {
data = this.loadFileSync(filename, currentDirectory, options, environment, 'utf-8');
callback(data.error, data);
return;
}
var paths = isAbsoluteFilename ? [""] : [currentDirectory];
if (options.paths) { paths.push.apply(paths, options.paths); }
if (!isAbsoluteFilename && paths.indexOf('.') === -1) { paths.push('.'); }
// promise is guaranteed to be asyncronous
// which helps as it allows the file handle
// to be closed before it continues with the next file
return new PromiseConstructor(function(fulfill, reject) {
(function tryPathIndex(i) {
if (i < paths.length) {
fullFilename = filename;
if (paths[i]) {
fullFilename = path.join(paths[i], fullFilename);
}
fs.stat(fullFilename, function (err) {
if (err) {
filenamesTried.push(fullFilename);
tryPathIndex(i + 1);
} else {
fs.readFile(fullFilename, 'utf-8', function(e, data) {
if (e) { reject(e); return; }
fulfill({ contents: data, filename: fullFilename});
});
}
});
} else {
reject({ type: 'File', message: "'" + filename + "' wasn't found. Tried - " + filenamesTried.join(",") });
}
}(0));
});
};
FileManager.prototype.loadFileSync = function(filename, currentDirectory, options, environment, encoding) {
var fullFilename, paths, filenamesTried = [], isAbsoluteFilename = this.isPathAbsolute(filename) , data;
options = options || {};
paths = isAbsoluteFilename ? [""] : [currentDirectory];
if (options.paths) {
paths.push.apply(paths, options.paths);
}
if (!isAbsoluteFilename && paths.indexOf('.') === -1) {
paths.push('.');
}
var err, result;
for (var i = 0; i < paths.length; i++) {
try {
fullFilename = filename;
if (paths[i]) {
fullFilename = path.join(paths[i], fullFilename);
}
filenamesTried.push(fullFilename);
fs.statSync(fullFilename);
break;
} catch (e) {
fullFilename = null;
}
}
if (!fullFilename) {
err = { type: 'File', message: "'" + filename + "' wasn't found. Tried - " + filenamesTried.join(",") };
result = { error: err };
} else {
data = fs.readFileSync(fullFilename, encoding);
result = { contents: data, filename: fullFilename};
}
return result;
};
module.exports = FileManager;