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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.get = get;
  6. exports.minVersion = minVersion;
  7. exports.getDependencies = getDependencies;
  8. exports.ensure = ensure;
  9. exports.default = exports.list = void 0;
  10. var _traverse = require("@babel/traverse");
  11. var t = require("@babel/types");
  12. var _helpers = require("./helpers");
  13. function makePath(path) {
  14. const parts = [];
  15. for (; path.parentPath; path = path.parentPath) {
  16. parts.push(path.key);
  17. if (path.inList) parts.push(path.listKey);
  18. }
  19. return parts.reverse().join(".");
  20. }
  21. let fileClass = undefined;
  22. function getHelperMetadata(file) {
  23. const globals = new Set();
  24. const localBindingNames = new Set();
  25. const dependencies = new Map();
  26. let exportName;
  27. let exportPath;
  28. const exportBindingAssignments = [];
  29. const importPaths = [];
  30. const importBindingsReferences = [];
  31. const dependencyVisitor = {
  32. ImportDeclaration(child) {
  33. const name = child.node.source.value;
  34. if (!_helpers.default[name]) {
  35. throw child.buildCodeFrameError(`Unknown helper ${name}`);
  36. }
  37. if (child.get("specifiers").length !== 1 || !child.get("specifiers.0").isImportDefaultSpecifier()) {
  38. throw child.buildCodeFrameError("Helpers can only import a default value");
  39. }
  40. const bindingIdentifier = child.node.specifiers[0].local;
  41. dependencies.set(bindingIdentifier, name);
  42. importPaths.push(makePath(child));
  43. },
  44. ExportDefaultDeclaration(child) {
  45. const decl = child.get("declaration");
  46. if (decl.isFunctionDeclaration()) {
  47. if (!decl.node.id) {
  48. throw decl.buildCodeFrameError("Helpers should give names to their exported func declaration");
  49. }
  50. exportName = decl.node.id.name;
  51. }
  52. exportPath = makePath(child);
  53. },
  54. ExportAllDeclaration(child) {
  55. throw child.buildCodeFrameError("Helpers can only export default");
  56. },
  57. ExportNamedDeclaration(child) {
  58. throw child.buildCodeFrameError("Helpers can only export default");
  59. },
  60. Statement(child) {
  61. if (child.isModuleDeclaration()) return;
  62. child.skip();
  63. }
  64. };
  65. const referenceVisitor = {
  66. Program(path) {
  67. const bindings = path.scope.getAllBindings();
  68. Object.keys(bindings).forEach(name => {
  69. if (name === exportName) return;
  70. if (dependencies.has(bindings[name].identifier)) return;
  71. localBindingNames.add(name);
  72. });
  73. },
  74. ReferencedIdentifier(child) {
  75. const name = child.node.name;
  76. const binding = child.scope.getBinding(name, true);
  77. if (!binding) {
  78. globals.add(name);
  79. } else if (dependencies.has(binding.identifier)) {
  80. importBindingsReferences.push(makePath(child));
  81. }
  82. },
  83. AssignmentExpression(child) {
  84. const left = child.get("left");
  85. if (!(exportName in left.getBindingIdentifiers())) return;
  86. if (!left.isIdentifier()) {
  87. throw left.buildCodeFrameError("Only simple assignments to exports are allowed in helpers");
  88. }
  89. const binding = child.scope.getBinding(exportName);
  90. if (binding != null && binding.scope.path.isProgram()) {
  91. exportBindingAssignments.push(makePath(child));
  92. }
  93. }
  94. };
  95. (0, _traverse.default)(file.ast, dependencyVisitor, file.scope);
  96. (0, _traverse.default)(file.ast, referenceVisitor, file.scope);
  97. if (!exportPath) throw new Error("Helpers must default-export something.");
  98. exportBindingAssignments.reverse();
  99. return {
  100. globals: Array.from(globals),
  101. localBindingNames: Array.from(localBindingNames),
  102. dependencies,
  103. exportBindingAssignments,
  104. exportPath,
  105. exportName,
  106. importBindingsReferences,
  107. importPaths
  108. };
  109. }
  110. function permuteHelperAST(file, metadata, id, localBindings, getDependency) {
  111. if (localBindings && !id) {
  112. throw new Error("Unexpected local bindings for module-based helpers.");
  113. }
  114. if (!id) return;
  115. const {
  116. localBindingNames,
  117. dependencies,
  118. exportBindingAssignments,
  119. exportPath,
  120. exportName,
  121. importBindingsReferences,
  122. importPaths
  123. } = metadata;
  124. const dependenciesRefs = {};
  125. dependencies.forEach((name, id) => {
  126. dependenciesRefs[id.name] = typeof getDependency === "function" && getDependency(name) || id;
  127. });
  128. const toRename = {};
  129. const bindings = new Set(localBindings || []);
  130. localBindingNames.forEach(name => {
  131. let newName = name;
  132. while (bindings.has(newName)) newName = "_" + newName;
  133. if (newName !== name) toRename[name] = newName;
  134. });
  135. if (id.type === "Identifier" && exportName !== id.name) {
  136. toRename[exportName] = id.name;
  137. }
  138. const visitor = {
  139. Program(path) {
  140. const exp = path.get(exportPath);
  141. const imps = importPaths.map(p => path.get(p));
  142. const impsBindingRefs = importBindingsReferences.map(p => path.get(p));
  143. const decl = exp.get("declaration");
  144. if (id.type === "Identifier") {
  145. if (decl.isFunctionDeclaration()) {
  146. exp.replaceWith(decl);
  147. } else {
  148. exp.replaceWith(t.variableDeclaration("var", [t.variableDeclarator(id, decl.node)]));
  149. }
  150. } else if (id.type === "MemberExpression") {
  151. if (decl.isFunctionDeclaration()) {
  152. exportBindingAssignments.forEach(assignPath => {
  153. const assign = path.get(assignPath);
  154. assign.replaceWith(t.assignmentExpression("=", id, assign.node));
  155. });
  156. exp.replaceWith(decl);
  157. path.pushContainer("body", t.expressionStatement(t.assignmentExpression("=", id, t.identifier(exportName))));
  158. } else {
  159. exp.replaceWith(t.expressionStatement(t.assignmentExpression("=", id, decl.node)));
  160. }
  161. } else {
  162. throw new Error("Unexpected helper format.");
  163. }
  164. Object.keys(toRename).forEach(name => {
  165. path.scope.rename(name, toRename[name]);
  166. });
  167. for (const path of imps) path.remove();
  168. for (const path of impsBindingRefs) {
  169. const node = t.cloneNode(dependenciesRefs[path.node.name]);
  170. path.replaceWith(node);
  171. }
  172. path.stop();
  173. }
  174. };
  175. (0, _traverse.default)(file.ast, visitor, file.scope);
  176. }
  177. const helperData = Object.create(null);
  178. function loadHelper(name) {
  179. if (!helperData[name]) {
  180. const helper = _helpers.default[name];
  181. if (!helper) {
  182. throw Object.assign(new ReferenceError(`Unknown helper ${name}`), {
  183. code: "BABEL_HELPER_UNKNOWN",
  184. helper: name
  185. });
  186. }
  187. const fn = () => {
  188. const file = {
  189. ast: t.file(helper.ast())
  190. };
  191. if (fileClass) {
  192. return new fileClass({
  193. filename: `babel-helper://${name}`
  194. }, file);
  195. }
  196. return file;
  197. };
  198. const metadata = getHelperMetadata(fn());
  199. helperData[name] = {
  200. build(getDependency, id, localBindings) {
  201. const file = fn();
  202. permuteHelperAST(file, metadata, id, localBindings, getDependency);
  203. return {
  204. nodes: file.ast.program.body,
  205. globals: metadata.globals
  206. };
  207. },
  208. minVersion() {
  209. return helper.minVersion;
  210. },
  211. dependencies: metadata.dependencies
  212. };
  213. }
  214. return helperData[name];
  215. }
  216. function get(name, getDependency, id, localBindings) {
  217. return loadHelper(name).build(getDependency, id, localBindings);
  218. }
  219. function minVersion(name) {
  220. return loadHelper(name).minVersion();
  221. }
  222. function getDependencies(name) {
  223. return Array.from(loadHelper(name).dependencies.values());
  224. }
  225. function ensure(name, newFileClass) {
  226. if (!fileClass) {
  227. fileClass = newFileClass;
  228. }
  229. loadHelper(name);
  230. }
  231. const list = Object.keys(_helpers.default).map(name => name.replace(/^_/, "")).filter(name => name !== "__esModule");
  232. exports.list = list;
  233. var _default = get;
  234. exports.default = _default;