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.

populate.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = populatePlaceholders;
  6. var t = require("@babel/types");
  7. function populatePlaceholders(metadata, replacements) {
  8. const ast = t.cloneNode(metadata.ast);
  9. if (replacements) {
  10. metadata.placeholders.forEach(placeholder => {
  11. if (!Object.prototype.hasOwnProperty.call(replacements, placeholder.name)) {
  12. const placeholderName = placeholder.name;
  13. throw new Error(`Error: No substitution given for "${placeholderName}". If this is not meant to be a
  14. placeholder you may want to consider passing one of the following options to @babel/template:
  15. - { placeholderPattern: false, placeholderWhitelist: new Set(['${placeholderName}'])}
  16. - { placeholderPattern: /^${placeholderName}$/ }`);
  17. }
  18. });
  19. Object.keys(replacements).forEach(key => {
  20. if (!metadata.placeholderNames.has(key)) {
  21. throw new Error(`Unknown substitution "${key}" given`);
  22. }
  23. });
  24. }
  25. metadata.placeholders.slice().reverse().forEach(placeholder => {
  26. try {
  27. applyReplacement(placeholder, ast, replacements && replacements[placeholder.name] || null);
  28. } catch (e) {
  29. e.message = `@babel/template placeholder "${placeholder.name}": ${e.message}`;
  30. throw e;
  31. }
  32. });
  33. return ast;
  34. }
  35. function applyReplacement(placeholder, ast, replacement) {
  36. if (placeholder.isDuplicate) {
  37. if (Array.isArray(replacement)) {
  38. replacement = replacement.map(node => t.cloneNode(node));
  39. } else if (typeof replacement === "object") {
  40. replacement = t.cloneNode(replacement);
  41. }
  42. }
  43. const {
  44. parent,
  45. key,
  46. index
  47. } = placeholder.resolve(ast);
  48. if (placeholder.type === "string") {
  49. if (typeof replacement === "string") {
  50. replacement = t.stringLiteral(replacement);
  51. }
  52. if (!replacement || !t.isStringLiteral(replacement)) {
  53. throw new Error("Expected string substitution");
  54. }
  55. } else if (placeholder.type === "statement") {
  56. if (index === undefined) {
  57. if (!replacement) {
  58. replacement = t.emptyStatement();
  59. } else if (Array.isArray(replacement)) {
  60. replacement = t.blockStatement(replacement);
  61. } else if (typeof replacement === "string") {
  62. replacement = t.expressionStatement(t.identifier(replacement));
  63. } else if (!t.isStatement(replacement)) {
  64. replacement = t.expressionStatement(replacement);
  65. }
  66. } else {
  67. if (replacement && !Array.isArray(replacement)) {
  68. if (typeof replacement === "string") {
  69. replacement = t.identifier(replacement);
  70. }
  71. if (!t.isStatement(replacement)) {
  72. replacement = t.expressionStatement(replacement);
  73. }
  74. }
  75. }
  76. } else if (placeholder.type === "param") {
  77. if (typeof replacement === "string") {
  78. replacement = t.identifier(replacement);
  79. }
  80. if (index === undefined) throw new Error("Assertion failure.");
  81. } else {
  82. if (typeof replacement === "string") {
  83. replacement = t.identifier(replacement);
  84. }
  85. if (Array.isArray(replacement)) {
  86. throw new Error("Cannot replace single expression with an array.");
  87. }
  88. }
  89. if (index === undefined) {
  90. t.validate(parent, key, replacement);
  91. parent[key] = replacement;
  92. } else {
  93. const items = parent[key].slice();
  94. if (placeholder.type === "statement" || placeholder.type === "param") {
  95. if (replacement == null) {
  96. items.splice(index, 1);
  97. } else if (Array.isArray(replacement)) {
  98. items.splice(index, 1, ...replacement);
  99. } else {
  100. items[index] = replacement;
  101. }
  102. } else {
  103. items[index] = replacement;
  104. }
  105. t.validate(parent, key, items);
  106. parent[key] = items;
  107. }
  108. }