Projekt

Obecné

Profil

Stáhnout (1.11 KB) Statistiky
| Větev: | Revize:
1
const definitions = require("../src/definitions");
2
const flatMap = require("array.prototype.flatmap");
3
const { typeSignature, mapProps, iterateProps, unique } = require("./util");
4

    
5
const stdout = process.stdout;
6

    
7
function params(fields) {
8
  return mapProps(fields)
9
    .map(typeSignature)
10
    .join(",");
11
}
12

    
13
function generate() {
14
  stdout.write(`
15
    // @flow
16
    /* eslint no-unused-vars: off */
17

    
18
    // THIS FILE IS AUTOGENERATED
19
    // see scripts/generateTypeDefinitions.js
20
  `);
21

    
22
  // generate union types
23
  const unionTypes = unique(
24
    flatMap(mapProps(definitions).filter(d => d.unionType), d => d.unionType)
25
  );
26
  unionTypes.forEach(unionType => {
27
    stdout.write(
28
      `type ${unionType} = ` +
29
        mapProps(definitions)
30
          .filter(d => d.unionType && d.unionType.includes(unionType))
31
          .map(d => d.name)
32
          .join("|") +
33
        ";\n\n"
34
    );
35
  });
36

    
37
  // generate the type definitions
38
  iterateProps(definitions, typeDef => {
39
    stdout.write(`type ${typeDef.name} = {
40
        ...BaseNode,
41
        type: "${typeDef.name}",
42
        ${params(typeDef.fields)}
43
      };\n\n`);
44
  });
45
}
46

    
47
generate();
(2-2/3)