Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ModuleMap.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _constants = _interopRequireDefault(require('./constants'));
  7. var fastPath = _interopRequireWildcard(require('./lib/fast_path'));
  8. function _getRequireWildcardCache(nodeInterop) {
  9. if (typeof WeakMap !== 'function') return null;
  10. var cacheBabelInterop = new WeakMap();
  11. var cacheNodeInterop = new WeakMap();
  12. return (_getRequireWildcardCache = function (nodeInterop) {
  13. return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
  14. })(nodeInterop);
  15. }
  16. function _interopRequireWildcard(obj, nodeInterop) {
  17. if (!nodeInterop && obj && obj.__esModule) {
  18. return obj;
  19. }
  20. if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
  21. return {default: obj};
  22. }
  23. var cache = _getRequireWildcardCache(nodeInterop);
  24. if (cache && cache.has(obj)) {
  25. return cache.get(obj);
  26. }
  27. var newObj = {};
  28. var hasPropertyDescriptor =
  29. Object.defineProperty && Object.getOwnPropertyDescriptor;
  30. for (var key in obj) {
  31. if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
  32. var desc = hasPropertyDescriptor
  33. ? Object.getOwnPropertyDescriptor(obj, key)
  34. : null;
  35. if (desc && (desc.get || desc.set)) {
  36. Object.defineProperty(newObj, key, desc);
  37. } else {
  38. newObj[key] = obj[key];
  39. }
  40. }
  41. }
  42. newObj.default = obj;
  43. if (cache) {
  44. cache.set(obj, newObj);
  45. }
  46. return newObj;
  47. }
  48. function _interopRequireDefault(obj) {
  49. return obj && obj.__esModule ? obj : {default: obj};
  50. }
  51. function _defineProperty(obj, key, value) {
  52. if (key in obj) {
  53. Object.defineProperty(obj, key, {
  54. value: value,
  55. enumerable: true,
  56. configurable: true,
  57. writable: true
  58. });
  59. } else {
  60. obj[key] = value;
  61. }
  62. return obj;
  63. }
  64. const EMPTY_OBJ = {};
  65. const EMPTY_MAP = new Map();
  66. class ModuleMap {
  67. static mapToArrayRecursive(map) {
  68. let arr = Array.from(map);
  69. if (arr[0] && arr[0][1] instanceof Map) {
  70. arr = arr.map(el => [el[0], this.mapToArrayRecursive(el[1])]);
  71. }
  72. return arr;
  73. }
  74. static mapFromArrayRecursive(arr) {
  75. if (arr[0] && Array.isArray(arr[1])) {
  76. arr = arr.map(el => [el[0], this.mapFromArrayRecursive(el[1])]);
  77. }
  78. return new Map(arr);
  79. }
  80. constructor(raw) {
  81. _defineProperty(this, '_raw', void 0);
  82. _defineProperty(this, 'json', void 0);
  83. this._raw = raw;
  84. }
  85. getModule(name, platform, supportsNativePlatform, type) {
  86. if (type == null) {
  87. type = _constants.default.MODULE;
  88. }
  89. const module = this._getModuleMetadata(
  90. name,
  91. platform,
  92. !!supportsNativePlatform
  93. );
  94. if (module && module[_constants.default.TYPE] === type) {
  95. const modulePath = module[_constants.default.PATH];
  96. return modulePath && fastPath.resolve(this._raw.rootDir, modulePath);
  97. }
  98. return null;
  99. }
  100. getPackage(name, platform, _supportsNativePlatform) {
  101. return this.getModule(name, platform, null, _constants.default.PACKAGE);
  102. }
  103. getMockModule(name) {
  104. const mockPath =
  105. this._raw.mocks.get(name) || this._raw.mocks.get(name + '/index');
  106. return mockPath && fastPath.resolve(this._raw.rootDir, mockPath);
  107. }
  108. getRawModuleMap() {
  109. return {
  110. duplicates: this._raw.duplicates,
  111. map: this._raw.map,
  112. mocks: this._raw.mocks,
  113. rootDir: this._raw.rootDir
  114. };
  115. }
  116. toJSON() {
  117. if (!this.json) {
  118. this.json = {
  119. duplicates: ModuleMap.mapToArrayRecursive(this._raw.duplicates),
  120. map: Array.from(this._raw.map),
  121. mocks: Array.from(this._raw.mocks),
  122. rootDir: this._raw.rootDir
  123. };
  124. }
  125. return this.json;
  126. }
  127. static fromJSON(serializableModuleMap) {
  128. return new ModuleMap({
  129. duplicates: ModuleMap.mapFromArrayRecursive(
  130. serializableModuleMap.duplicates
  131. ),
  132. map: new Map(serializableModuleMap.map),
  133. mocks: new Map(serializableModuleMap.mocks),
  134. rootDir: serializableModuleMap.rootDir
  135. });
  136. }
  137. /**
  138. * When looking up a module's data, we walk through each eligible platform for
  139. * the query. For each platform, we want to check if there are known
  140. * duplicates for that name+platform pair. The duplication logic normally
  141. * removes elements from the `map` object, but we want to check upfront to be
  142. * extra sure. If metadata exists both in the `duplicates` object and the
  143. * `map`, this would be a bug.
  144. */
  145. _getModuleMetadata(name, platform, supportsNativePlatform) {
  146. const map = this._raw.map.get(name) || EMPTY_OBJ;
  147. const dupMap = this._raw.duplicates.get(name) || EMPTY_MAP;
  148. if (platform != null) {
  149. this._assertNoDuplicates(
  150. name,
  151. platform,
  152. supportsNativePlatform,
  153. dupMap.get(platform)
  154. );
  155. if (map[platform] != null) {
  156. return map[platform];
  157. }
  158. }
  159. if (supportsNativePlatform) {
  160. this._assertNoDuplicates(
  161. name,
  162. _constants.default.NATIVE_PLATFORM,
  163. supportsNativePlatform,
  164. dupMap.get(_constants.default.NATIVE_PLATFORM)
  165. );
  166. if (map[_constants.default.NATIVE_PLATFORM]) {
  167. return map[_constants.default.NATIVE_PLATFORM];
  168. }
  169. }
  170. this._assertNoDuplicates(
  171. name,
  172. _constants.default.GENERIC_PLATFORM,
  173. supportsNativePlatform,
  174. dupMap.get(_constants.default.GENERIC_PLATFORM)
  175. );
  176. if (map[_constants.default.GENERIC_PLATFORM]) {
  177. return map[_constants.default.GENERIC_PLATFORM];
  178. }
  179. return null;
  180. }
  181. _assertNoDuplicates(name, platform, supportsNativePlatform, relativePathSet) {
  182. if (relativePathSet == null) {
  183. return;
  184. } // Force flow refinement
  185. const previousSet = relativePathSet;
  186. const duplicates = new Map();
  187. for (const [relativePath, type] of previousSet) {
  188. const duplicatePath = fastPath.resolve(this._raw.rootDir, relativePath);
  189. duplicates.set(duplicatePath, type);
  190. }
  191. throw new DuplicateHasteCandidatesError(
  192. name,
  193. platform,
  194. supportsNativePlatform,
  195. duplicates
  196. );
  197. }
  198. static create(rootDir) {
  199. return new ModuleMap({
  200. duplicates: new Map(),
  201. map: new Map(),
  202. mocks: new Map(),
  203. rootDir
  204. });
  205. }
  206. }
  207. exports.default = ModuleMap;
  208. _defineProperty(ModuleMap, 'DuplicateHasteCandidatesError', void 0);
  209. class DuplicateHasteCandidatesError extends Error {
  210. constructor(name, platform, supportsNativePlatform, duplicatesSet) {
  211. const platformMessage = getPlatformMessage(platform);
  212. super(
  213. `The name \`${name}\` was looked up in the Haste module map. It ` +
  214. `cannot be resolved, because there exists several different ` +
  215. `files, or packages, that provide a module for ` +
  216. `that particular name and platform. ${platformMessage} You must ` +
  217. `delete or exclude files until there remains only one of these:\n\n` +
  218. Array.from(duplicatesSet)
  219. .map(
  220. ([dupFilePath, dupFileType]) =>
  221. ` * \`${dupFilePath}\` (${getTypeMessage(dupFileType)})\n`
  222. )
  223. .sort()
  224. .join('')
  225. );
  226. _defineProperty(this, 'hasteName', void 0);
  227. _defineProperty(this, 'platform', void 0);
  228. _defineProperty(this, 'supportsNativePlatform', void 0);
  229. _defineProperty(this, 'duplicatesSet', void 0);
  230. this.hasteName = name;
  231. this.platform = platform;
  232. this.supportsNativePlatform = supportsNativePlatform;
  233. this.duplicatesSet = duplicatesSet;
  234. }
  235. }
  236. function getPlatformMessage(platform) {
  237. if (platform === _constants.default.GENERIC_PLATFORM) {
  238. return 'The platform is generic (no extension).';
  239. }
  240. return `The platform extension is \`${platform}\`.`;
  241. }
  242. function getTypeMessage(type) {
  243. switch (type) {
  244. case _constants.default.MODULE:
  245. return 'module';
  246. case _constants.default.PACKAGE:
  247. return 'package';
  248. }
  249. return 'unknown';
  250. }
  251. ModuleMap.DuplicateHasteCandidatesError = DuplicateHasteCandidatesError;