-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
71 lines (68 loc) · 2.68 KB
/
index.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
var globby = require('globby');
var path = require('path');
var eol = require('os').EOL;
function processPaths(paths) {
return paths.filter(function(filepath) {
var ext = path.extname(filepath);
if(ext === '.less' || ext === '.css') {
return true;
}
console.warn('Here is non-less file: ' + filepath + ', ignored');
return false;
});
}
module.exports = {
install: function(less, pluginManager) {
function GlobFileManager() {
}
GlobFileManager.prototype = new less.FileManager();
GlobFileManager.prototype.constructor = GlobFileManager;
GlobFileManager.prototype.supports = function(filename) {
return (filename).indexOf('*') > -1;
};
GlobFileManager.prototype.searchInPath = function(basePath, glob) {
const _this = this;
return globby(glob, {cwd: basePath}).then(function(files) {
return files.map(function(file) {
if (_this.isPathAbsolute(file)) {
return file;
}
return path.join(basePath, file);
});
});
};
GlobFileManager.prototype.loadFile = function(filename, currentDirectory, options) {
var paths = [currentDirectory];
if (!this.isPathAbsolute(filename) && paths.indexOf('.') === -1) {
paths.push('.');
}
if (options.paths && options.paths.length > 0) {
paths.push.apply(paths, options.paths);
}
return Promise.all(paths.map(function(basePath) {
return this.searchInPath(path.resolve(basePath), filename);
}, this)).then(function(paths) {
paths = Array.prototype.concat.apply([], paths);
return processPaths(paths);
}).then(function(files) {
return {
contents: files
.map(function(file) {
return '@import (less) "' + file + '";';
})
.join(eol),
filename: filename
};
});
};
GlobFileManager.prototype.loadFileSync = function(filename, currentDirectory) {
var paths = globby.sync(filename, {cwd: currentDirectory});
paths = processPaths(paths);
return paths.map(function(file) {
return '@import "' + file + '";';
}, this).join(eol);
};
GlobFileManager.prototype.supportsSync = GlobFileManager.prototype.supports;
pluginManager.addFileManager(new GlobFileManager());
}
};