1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | var tslib_1 = require("tslib");
|
---|
4 | var path_1 = tslib_1.__importDefault(require("path"));
|
---|
5 | var fs_1 = tslib_1.__importDefault(require("./fs"));
|
---|
6 | var abstract_file_manager_js_1 = tslib_1.__importDefault(require("../less/environment/abstract-file-manager.js"));
|
---|
7 | var FileManager = function () { };
|
---|
8 | FileManager.prototype = Object.assign(new abstract_file_manager_js_1.default(), {
|
---|
9 | supports: function () {
|
---|
10 | return true;
|
---|
11 | },
|
---|
12 | supportsSync: function () {
|
---|
13 | return true;
|
---|
14 | },
|
---|
15 | loadFile: function (filename, currentDirectory, options, environment, callback) {
|
---|
16 | var fullFilename;
|
---|
17 | var isAbsoluteFilename = this.isPathAbsolute(filename);
|
---|
18 | var filenamesTried = [];
|
---|
19 | var self = this;
|
---|
20 | var prefix = filename.slice(0, 1);
|
---|
21 | var explicit = prefix === '.' || prefix === '/';
|
---|
22 | var result = null;
|
---|
23 | var isNodeModule = false;
|
---|
24 | var npmPrefix = 'npm://';
|
---|
25 | options = options || {};
|
---|
26 | var paths = isAbsoluteFilename ? [''] : [currentDirectory];
|
---|
27 | if (options.paths) {
|
---|
28 | paths.push.apply(paths, options.paths);
|
---|
29 | }
|
---|
30 | if (!isAbsoluteFilename && paths.indexOf('.') === -1) {
|
---|
31 | paths.push('.');
|
---|
32 | }
|
---|
33 | var prefixes = options.prefixes || [''];
|
---|
34 | var fileParts = this.extractUrlParts(filename);
|
---|
35 | if (options.syncImport) {
|
---|
36 | getFileData(returnData, returnData);
|
---|
37 | if (callback) {
|
---|
38 | callback(result.error, result);
|
---|
39 | }
|
---|
40 | else {
|
---|
41 | return result;
|
---|
42 | }
|
---|
43 | }
|
---|
44 | else {
|
---|
45 | // promise is guaranteed to be asyncronous
|
---|
46 | // which helps as it allows the file handle
|
---|
47 | // to be closed before it continues with the next file
|
---|
48 | return new Promise(getFileData);
|
---|
49 | }
|
---|
50 | function returnData(data) {
|
---|
51 | if (!data.filename) {
|
---|
52 | result = { error: data };
|
---|
53 | }
|
---|
54 | else {
|
---|
55 | result = data;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | function getFileData(fulfill, reject) {
|
---|
59 | (function tryPathIndex(i) {
|
---|
60 | if (i < paths.length) {
|
---|
61 | (function tryPrefix(j) {
|
---|
62 | if (j < prefixes.length) {
|
---|
63 | isNodeModule = false;
|
---|
64 | fullFilename = fileParts.rawPath + prefixes[j] + fileParts.filename;
|
---|
65 | if (paths[i]) {
|
---|
66 | fullFilename = path_1.default.join(paths[i], fullFilename);
|
---|
67 | }
|
---|
68 | if (!explicit && paths[i] === '.') {
|
---|
69 | try {
|
---|
70 | fullFilename = require.resolve(fullFilename);
|
---|
71 | isNodeModule = true;
|
---|
72 | }
|
---|
73 | catch (e) {
|
---|
74 | filenamesTried.push(npmPrefix + fullFilename);
|
---|
75 | tryWithExtension();
|
---|
76 | }
|
---|
77 | }
|
---|
78 | else {
|
---|
79 | tryWithExtension();
|
---|
80 | }
|
---|
81 | function tryWithExtension() {
|
---|
82 | var extFilename = options.ext ? self.tryAppendExtension(fullFilename, options.ext) : fullFilename;
|
---|
83 | if (extFilename !== fullFilename && !explicit && paths[i] === '.') {
|
---|
84 | try {
|
---|
85 | fullFilename = require.resolve(extFilename);
|
---|
86 | isNodeModule = true;
|
---|
87 | }
|
---|
88 | catch (e) {
|
---|
89 | filenamesTried.push(npmPrefix + extFilename);
|
---|
90 | fullFilename = extFilename;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | else {
|
---|
94 | fullFilename = extFilename;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | var readFileArgs = [fullFilename];
|
---|
98 | if (!options.rawBuffer) {
|
---|
99 | readFileArgs.push('utf-8');
|
---|
100 | }
|
---|
101 | if (options.syncImport) {
|
---|
102 | try {
|
---|
103 | var data = fs_1.default.readFileSync.apply(this, readFileArgs);
|
---|
104 | fulfill({ contents: data, filename: fullFilename });
|
---|
105 | }
|
---|
106 | catch (e) {
|
---|
107 | filenamesTried.push(isNodeModule ? npmPrefix + fullFilename : fullFilename);
|
---|
108 | return tryPrefix(j + 1);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | else {
|
---|
112 | readFileArgs.push(function (e, data) {
|
---|
113 | if (e) {
|
---|
114 | filenamesTried.push(isNodeModule ? npmPrefix + fullFilename : fullFilename);
|
---|
115 | return tryPrefix(j + 1);
|
---|
116 | }
|
---|
117 | fulfill({ contents: data, filename: fullFilename });
|
---|
118 | });
|
---|
119 | fs_1.default.readFile.apply(this, readFileArgs);
|
---|
120 | }
|
---|
121 | }
|
---|
122 | else {
|
---|
123 | tryPathIndex(i + 1);
|
---|
124 | }
|
---|
125 | })(0);
|
---|
126 | }
|
---|
127 | else {
|
---|
128 | reject({ type: 'File', message: "'" + filename + "' wasn't found. Tried - " + filenamesTried.join(',') });
|
---|
129 | }
|
---|
130 | }(0));
|
---|
131 | }
|
---|
132 | },
|
---|
133 | loadFileSync: function (filename, currentDirectory, options, environment) {
|
---|
134 | options.syncImport = true;
|
---|
135 | return this.loadFile(filename, currentDirectory, options, environment);
|
---|
136 | }
|
---|
137 | });
|
---|
138 | exports.default = FileManager;
|
---|
139 | //# sourceMappingURL=file-manager.js.map |
---|