index.js 761 B

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. const utils = require("../utils/ast-utils");
  3. module.exports = function astTransform(j, ast, value, action, key) {
  4. const node = utils.findRootNodesByName(j, ast, key);
  5. if (node.size() !== 0) {
  6. // push to existing key
  7. return ast;
  8. } else {
  9. // get module.exports prop
  10. const root = ast
  11. .find(j.ObjectExpression)
  12. .filter(p => {
  13. return (
  14. utils.safeTraverse(p, [
  15. "parentPath",
  16. "value",
  17. "left",
  18. "object",
  19. "name"
  20. ]) === "module" &&
  21. utils.safeTraverse(p, [
  22. "parentPath",
  23. "value",
  24. "left",
  25. "property",
  26. "name"
  27. ]) === "exports"
  28. );
  29. })
  30. .filter(p => p.value.properties);
  31. return root.forEach(p => {
  32. utils.addProperty(j, p, key, value);
  33. });
  34. }
  35. };