source: trip-planner-front/node_modules/fs-monkey/lib/patchRequire.js@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 7.0 KB
Line 
1"use strict";
2
3function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports["default"] = patchRequire;
9
10var path = _interopRequireWildcard(require("path"));
11
12function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
13
14function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
16var isWin32 = process.platform === 'win32';
17var correctPath = isWin32 ? require('./correctPath').correctPath : function (p) {
18 return p;
19};
20
21function stripBOM(content) {
22 if (content.charCodeAt(0) === 0xFEFF) {
23 content = content.slice(1);
24 }
25
26 return content;
27}
28
29function patchRequire(vol) {
30 var unixifyPaths = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
31 var Module = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : require('module');
32
33 if (isWin32 && unixifyPaths) {
34 var original = vol;
35 vol = {
36 readFileSync: function readFileSync(path, options) {
37 return original.readFileSync(correctPath(path), options);
38 },
39 realpathSync: function realpathSync(path) {
40 return original.realpathSync(correctPath(path));
41 },
42 statSync: function statSync(path) {
43 return original.statSync(correctPath(path));
44 }
45 };
46 }
47
48 function internalModuleReadFile(path) {
49 try {
50 return vol.readFileSync(path, 'utf8');
51 } catch (err) {}
52 }
53
54 function internalModuleStat(filename) {
55 try {
56 return vol.statSync(filename).isDirectory() ? 1 : 0;
57 } catch (err) {
58 return -2;
59 }
60 }
61
62 function stat(filename) {
63 filename = path._makeLong(filename);
64 var cache = stat.cache;
65
66 if (cache !== null) {
67 var _result = cache.get(filename);
68
69 if (_result !== undefined) return _result;
70 }
71
72 var result = internalModuleStat(filename);
73 if (cache !== null) cache.set(filename, result);
74 return result;
75 }
76
77 stat.cache = null;
78 var preserveSymlinks = false;
79
80 function toRealPath(requestPath) {
81 return vol.realpathSync(requestPath);
82 }
83
84 var packageMainCache = Object.create(null);
85
86 function readPackage(requestPath) {
87 var entry = packageMainCache[requestPath];
88 if (entry) return entry;
89 var jsonPath = path.resolve(requestPath, 'package.json');
90 var json = internalModuleReadFile(path._makeLong(jsonPath));
91
92 if (json === undefined) {
93 return false;
94 }
95
96 var pkg;
97
98 try {
99 pkg = packageMainCache[requestPath] = JSON.parse(json).main;
100 } catch (e) {
101 e.path = jsonPath;
102 e.message = 'Error parsing ' + jsonPath + ': ' + e.message;
103 throw e;
104 }
105
106 return pkg;
107 }
108
109 function tryFile(requestPath, isMain) {
110 var rc = stat(requestPath);
111
112 if (preserveSymlinks && !isMain) {
113 return rc === 0 && path.resolve(requestPath);
114 }
115
116 return rc === 0 && toRealPath(requestPath);
117 }
118
119 function tryExtensions(p, exts, isMain) {
120 for (var i = 0; i < exts.length; i++) {
121 var filename = tryFile(p + exts[i], isMain);
122
123 if (filename) {
124 return filename;
125 }
126 }
127
128 return false;
129 }
130
131 function tryPackage(requestPath, exts, isMain) {
132 var pkg = readPackage(requestPath);
133 if (!pkg) return false;
134 var filename = path.resolve(requestPath, pkg);
135 return tryFile(filename, isMain) || tryExtensions(filename, exts, isMain) || tryExtensions(path.resolve(filename, 'index'), exts, isMain);
136 }
137
138 Module._extensions['.js'] = function (module, filename) {
139 var content = vol.readFileSync(filename, 'utf8');
140
141 module._compile(stripBOM(content), filename);
142 };
143
144 Module._extensions['.json'] = function (module, filename) {
145 var content = vol.readFileSync(filename, 'utf8');
146
147 try {
148 module.exports = JSON.parse(stripBOM(content));
149 } catch (err) {
150 err.message = filename + ': ' + err.message;
151 throw err;
152 }
153 };
154
155 var warned = true;
156
157 Module._findPath = function (request, paths, isMain) {
158 if (path.isAbsolute(request)) {
159 paths = [''];
160 } else if (!paths || paths.length === 0) {
161 return false;
162 }
163
164 var cacheKey = request + '\x00' + (paths.length === 1 ? paths[0] : paths.join('\x00'));
165 var entry = Module._pathCache[cacheKey];
166 if (entry) return entry;
167 var exts;
168 var trailingSlash = request.length > 0 && request.charCodeAt(request.length - 1) === 47;
169
170 for (var i = 0; i < paths.length; i++) {
171 var curPath = paths[i];
172 if (curPath && stat(curPath) < 1) continue;
173 var basePath = correctPath(path.resolve(curPath, request));
174 var filename;
175 var rc = stat(basePath);
176
177 if (!trailingSlash) {
178 if (rc === 0) {
179 if (preserveSymlinks && !isMain) {
180 filename = path.resolve(basePath);
181 } else {
182 filename = toRealPath(basePath);
183 }
184 } else if (rc === 1) {
185 if (exts === undefined) exts = Object.keys(Module._extensions);
186 filename = tryPackage(basePath, exts, isMain);
187 }
188
189 if (!filename) {
190 if (exts === undefined) exts = Object.keys(Module._extensions);
191 filename = tryExtensions(basePath, exts, isMain);
192 }
193 }
194
195 if (!filename && rc === 1) {
196 if (exts === undefined) exts = Object.keys(Module._extensions);
197 filename = tryPackage(basePath, exts, isMain);
198 }
199
200 if (!filename && rc === 1) {
201 if (exts === undefined) exts = Object.keys(Module._extensions);
202 filename = tryExtensions(path.resolve(basePath, 'index'), exts, isMain);
203 }
204
205 if (filename) {
206 if (request === '.' && i > 0) {
207 if (!warned) {
208 warned = true;
209 process.emitWarning('warning: require(\'.\') resolved outside the package ' + 'directory. This functionality is deprecated and will be removed ' + 'soon.', 'DeprecationWarning', 'DEP0019');
210 }
211 }
212
213 Module._pathCache[cacheKey] = filename;
214 return filename;
215 }
216 }
217
218 return false;
219 };
220}
Note: See TracBrowser for help on using the repository browser.