4a9f4d799e4680f8e7092f13eb6ddbb03751a2ae2a162a54fe0645c831bed29a42dbc95f34fc85db9fc5e05c99e1630c8378481a69e43e4fc54b4e7571801f 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const definitions = require("../src/definitions");
  2. const flatMap = require("array.prototype.flatmap");
  3. const { typeSignature, mapProps, iterateProps, unique } = require("./util");
  4. const stdout = process.stdout;
  5. function params(fields) {
  6. return mapProps(fields)
  7. .map(typeSignature)
  8. .join(",");
  9. }
  10. function generate() {
  11. stdout.write(`
  12. // @flow
  13. /* eslint no-unused-vars: off */
  14. // THIS FILE IS AUTOGENERATED
  15. // see scripts/generateTypeDefinitions.js
  16. `);
  17. // generate union types
  18. const unionTypes = unique(
  19. flatMap(mapProps(definitions).filter(d => d.unionType), d => d.unionType)
  20. );
  21. unionTypes.forEach(unionType => {
  22. stdout.write(
  23. `type ${unionType} = ` +
  24. mapProps(definitions)
  25. .filter(d => d.unionType && d.unionType.includes(unionType))
  26. .map(d => d.name)
  27. .join("|") +
  28. ";\n\n"
  29. );
  30. });
  31. // generate the type definitions
  32. iterateProps(definitions, typeDef => {
  33. stdout.write(`type ${typeDef.name} = {
  34. ...BaseNode,
  35. type: "${typeDef.name}",
  36. ${params(typeDef.fields)}
  37. };\n\n`);
  38. });
  39. }
  40. generate();