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.

index.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. 'use strict'
  2. var BuiltinModule = require('module')
  3. // Guard against poorly mocked module constructors
  4. var Module = module.constructor.length > 1
  5. ? module.constructor
  6. : BuiltinModule
  7. var nodePath = require('path')
  8. var modulePaths = []
  9. var moduleAliases = {}
  10. var moduleAliasNames = []
  11. var oldNodeModulePaths = Module._nodeModulePaths
  12. Module._nodeModulePaths = function (from) {
  13. var paths = oldNodeModulePaths.call(this, from)
  14. // Only include the module path for top-level modules
  15. // that were not installed:
  16. if (from.indexOf('node_modules') === -1) {
  17. paths = modulePaths.concat(paths)
  18. }
  19. return paths
  20. }
  21. var oldResolveFilename = Module._resolveFilename
  22. Module._resolveFilename = function (request, parentModule, isMain, options) {
  23. for (var i = moduleAliasNames.length; i-- > 0;) {
  24. var alias = moduleAliasNames[i]
  25. if (isPathMatchesAlias(request, alias)) {
  26. var aliasTarget = moduleAliases[alias]
  27. // Custom function handler
  28. if (typeof moduleAliases[alias] === 'function') {
  29. var fromPath = parentModule.filename
  30. aliasTarget = moduleAliases[alias](fromPath, request, alias)
  31. if (!aliasTarget || typeof aliasTarget !== 'string') {
  32. throw new Error('[module-alias] Expecting custom handler function to return path.')
  33. }
  34. }
  35. request = nodePath.join(aliasTarget, request.substr(alias.length))
  36. // Only use the first match
  37. break
  38. }
  39. }
  40. return oldResolveFilename.call(this, request, parentModule, isMain, options)
  41. }
  42. function isPathMatchesAlias (path, alias) {
  43. // Matching /^alias(\/|$)/
  44. if (path.indexOf(alias) === 0) {
  45. if (path.length === alias.length) return true
  46. if (path[alias.length] === '/') return true
  47. }
  48. return false
  49. }
  50. function addPathHelper (path, targetArray) {
  51. path = nodePath.normalize(path)
  52. if (targetArray && targetArray.indexOf(path) === -1) {
  53. targetArray.unshift(path)
  54. }
  55. }
  56. function removePathHelper (path, targetArray) {
  57. if (targetArray) {
  58. var index = targetArray.indexOf(path)
  59. if (index !== -1) {
  60. targetArray.splice(index, 1)
  61. }
  62. }
  63. }
  64. function addPath (path) {
  65. var parent
  66. path = nodePath.normalize(path)
  67. if (modulePaths.indexOf(path) === -1) {
  68. modulePaths.push(path)
  69. // Enable the search path for the current top-level module
  70. var mainModule = getMainModule()
  71. if (mainModule) {
  72. addPathHelper(path, mainModule.paths)
  73. }
  74. parent = module.parent
  75. // Also modify the paths of the module that was used to load the
  76. // app-module-paths module and all of it's parents
  77. while (parent && parent !== mainModule) {
  78. addPathHelper(path, parent.paths)
  79. parent = parent.parent
  80. }
  81. }
  82. }
  83. function addAliases (aliases) {
  84. for (var alias in aliases) {
  85. addAlias(alias, aliases[alias])
  86. }
  87. }
  88. function addAlias (alias, target) {
  89. moduleAliases[alias] = target
  90. // Cost of sorting is lower here than during resolution
  91. moduleAliasNames = Object.keys(moduleAliases)
  92. moduleAliasNames.sort()
  93. }
  94. /**
  95. * Reset any changes maded (resets all registered aliases
  96. * and custom module directories)
  97. * The function is undocumented and for testing purposes only
  98. */
  99. function reset () {
  100. var mainModule = getMainModule()
  101. // Reset all changes in paths caused by addPath function
  102. modulePaths.forEach(function (path) {
  103. if (mainModule) {
  104. removePathHelper(path, mainModule.paths)
  105. }
  106. // Delete from require.cache if the module has been required before.
  107. // This is required for node >= 11
  108. Object.getOwnPropertyNames(require.cache).forEach(function (name) {
  109. if (name.indexOf(path) !== -1) {
  110. delete require.cache[name]
  111. }
  112. })
  113. var parent = module.parent
  114. while (parent && parent !== mainModule) {
  115. removePathHelper(path, parent.paths)
  116. parent = parent.parent
  117. }
  118. })
  119. modulePaths = []
  120. moduleAliases = {}
  121. moduleAliasNames = []
  122. }
  123. /**
  124. * Import aliases from package.json
  125. * @param {object} options
  126. */
  127. function init (options) {
  128. if (typeof options === 'string') {
  129. options = { base: options }
  130. }
  131. options = options || {}
  132. var candidatePackagePaths
  133. if (options.base) {
  134. candidatePackagePaths = [nodePath.resolve(options.base.replace(/\/package\.json$/, ''))]
  135. } else {
  136. // There is probably 99% chance that the project root directory in located
  137. // above the node_modules directory,
  138. // Or that package.json is in the node process' current working directory (when
  139. // running a package manager script, e.g. `yarn start` / `npm run start`)
  140. candidatePackagePaths = [nodePath.join(__dirname, '../..'), process.cwd()]
  141. }
  142. var npmPackage
  143. var base
  144. for (var i in candidatePackagePaths) {
  145. try {
  146. base = candidatePackagePaths[i]
  147. npmPackage = require(nodePath.join(base, 'package.json'))
  148. break
  149. } catch (e) {
  150. // noop
  151. }
  152. }
  153. if (typeof npmPackage !== 'object') {
  154. var pathString = candidatePackagePaths.join(',\n')
  155. throw new Error('Unable to find package.json in any of:\n[' + pathString + ']')
  156. }
  157. //
  158. // Import aliases
  159. //
  160. var aliases = npmPackage._moduleAliases || {}
  161. for (var alias in aliases) {
  162. if (aliases[alias][0] !== '/') {
  163. aliases[alias] = nodePath.join(base, aliases[alias])
  164. }
  165. }
  166. addAliases(aliases)
  167. //
  168. // Register custom module directories (like node_modules)
  169. //
  170. if (npmPackage._moduleDirectories instanceof Array) {
  171. npmPackage._moduleDirectories.forEach(function (dir) {
  172. if (dir === 'node_modules') return
  173. var modulePath = nodePath.join(base, dir)
  174. addPath(modulePath)
  175. })
  176. }
  177. }
  178. function getMainModule () {
  179. return require.main._simulateRepl ? undefined : require.main
  180. }
  181. module.exports = init
  182. module.exports.addPath = addPath
  183. module.exports.addAlias = addAlias
  184. module.exports.addAliases = addAliases
  185. module.exports.isPathMatchesAlias = isPathMatchesAlias
  186. module.exports.reset = reset