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