Socialify

Folder ..

Viewing generateMschema.js
73 lines (60 loc) • 1.7 KB

 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var blaver = require("../");

var items = Object.keys(blaver);

items = items.filter(function (i) {
  if (
    ["locales", "definitions", "locale", "localeFallback"].indexOf(i) === -1
  ) {
    return i;
  }
});

var schema = {
  methods: {
    type: "string",
    enum: [],
  },
};

schema.modules = {
  type: "string",
  enum: [],
};

schema.methodSchemas = {};

items.forEach(function (item) {
  schema.modules.enum.push(item);
  for (var q in blaver[item]) {
    //console.log(item + '.' + q);

    // check to see if an existing schema existing on the function
    var fnLine = blaver[item][q].toString().split("\n").slice(0, 1)[0];
    var prop;

    if (typeof blaver[item][q].schema === "object") {
      // if so, we'll want to merge that onto the exported schemas here
      prop = blaver[item][q].schema;
    } else {
      // if not, fall back to the ones we can parse from the method itself

      // find first (
      var start = fnLine.search(/\(/);

      // find first )
      var end = fnLine.search(/\)/);

      // substr on those positions
      fnLine = fnLine.substr(start + 1, end - start - 1);
      if (fnLine === "") {
        //console.log(item + '.' + q, 'no arguments')
        prop = {};
      } else {
        // split on ,
        fnLine = fnLine.split(",");
        //console.log(item + '.' + q, fnLine);
        prop = {};
        prop.properties = {};
        fnLine.forEach(function (arg) {
          prop.properties[arg] = {
            type: "any",
          };
        });
      }
    }

    schema.methods.enum.push(item + "." + q);
    schema.methodSchemas[item + "." + q] = prop;
  }
});

var util = require("util");
console.log(util.inspect(schema, false, null));