This repository was archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathnpm-file-manager.js
83 lines (70 loc) · 2.83 KB
/
npm-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
var resolveNpmFile = require('resolve').sync,
win32 = process.platform === 'win32',
PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise,
path = require('path');
module.exports = function(less) {
var FileManager = less.FileManager;
function NpmFileManager(options) {
this.options = options || {};
if (this.options.prefix === undefined) {
this.options.prefix = 'npm://';
}
}
NpmFileManager.prototype = new FileManager();
NpmFileManager.prototype.supports = function(filename, currentDirectory, options, environment) {
var npmProtocolPrefixRegex = new RegExp('^' + this.options.prefix, 'i');
return filename.match(npmProtocolPrefixRegex) || currentDirectory.match(npmProtocolPrefixRegex);
};
NpmFileManager.prototype.supportsSync = NpmFileManager.prototype.supports;
NpmFileManager.prototype.resolve = function(filename, currentDirectory) {
filename = filename.replace(this.options.prefix, "");
currentDirectory = currentDirectory.replace(this.options.prefix, "");
currentDirectory = path.resolve(currentDirectory);
return resolveNpmFile(filename, {
basedir: currentDirectory,
extensions: ['.less', '.css'],
packageFilter: function packageFilter(info, pkgdir) {
// no style field, keep info unchanged
if (!info.style) {
return info;
}
// replace main
if (typeof info.style === 'string') {
info.main = info.style;
return info;
}
return info;
},
paths: process.env.NODE_PATH ? process.env.NODE_PATH.split(win32 ? ';' : ':') : []
});
};
NpmFileManager.prototype.loadFile = function(filename, currentDirectory, options, environment) {
try {
filename = this.resolve(filename, currentDirectory);
}
catch(e) {
return new PromiseConstructor(
function(fullfill, reject) {
reject(e);
}
);
}
return FileManager.prototype.loadFile.call(this, filename, "", options, environment);
};
NpmFileManager.prototype.loadFileSync = function(filename, currentDirectory, options, environment) {
try {
filename = this.resolve(filename, currentDirectory);
}
catch(e) {
return { error: e };
}
return FileManager.prototype.loadFileSync.call(this, filename, "", options, environment);
};
NpmFileManager.prototype.tryAppendExtension = function(path, ext) {
return path;
};
NpmFileManager.prototype.tryAppendLessExtension = function(path) {
return path;
};
return NpmFileManager;
};