-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
152 lines (143 loc) · 4.68 KB
/
index.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict';
var fs = require('fs');
var _ = require('lodash');
var inflection = require('inflection');
var jsesc = require('jsesc');
var keywords = {
string: {
minLength: 'number',
maxLength: 'number',
pattern: 'string',
format: 'string',
default: 'string'
},
number: {
multipleOf: 'number',
minimum: 'number',
maximum: 'number',
exclusiveMinimum: 'boolean',
exclusiveMaximum: 'boolean',
default: 'number'
},
object: {
minProperties: 'number',
maxProperties: 'number'
},
array: {
minItems: 'number',
maxItems: 'number',
uniqueItems: 'boolean'
}
};
var aliasMap = {};
module.exports = function(typhen, options) {
options = _.defaults(options, {
baseUri: '',
enumType: 'integer'
});
function getEnumValues(enumSymbol) {
return enumSymbol.members.map(getEnumValue).join(', ');
}
function getEnumValue(enumMemberSymbol) {
return options.enumType === 'string' ? '"' + enumMemberSymbol.name + '"' : enumMemberSymbol.value;
}
function formatResult(file) {
file.contents = new Buffer(JSON.stringify(JSON.parse(file.contents.toString()), null, 2));
}
return typhen.createPlugin({
pluginDirectory: __dirname,
namespaceSeparator: '/',
customPrimitiveTypes: ['integer'],
handlebarsOptions: {
data: options,
partials: {
object: fs.readFileSync(__dirname + '/templates/object.hbs', 'utf-8'),
type: fs.readFileSync(__dirname + '/templates/type.hbs', 'utf-8')
},
helpers: {
escape: function(str) {
return jsesc(str, { quotes: 'double' });
},
required: function(properties) {
return properties.filter(function(p) {
return !p.isOptional;
}).map(function(p) {
return '"' + p.name + '"';
}).join(', ');
},
keyword: function(type, tag) {
var keywordsByType;
if (type.name === 'number' || type.name === 'integer') {
keywordsByType = keywords.number;
} else if (type.name === 'string') {
keywordsByType = keywords.string;
} else if (type.isArray || type.isTuple) {
keywordsByType = keywords.array;
} else {
keywordsByType = keywords.object;
}
if (_.isString(keywordsByType[tag.name])) {
if (keywordsByType[tag.name] === 'string') {
return '"' + tag.name + '": "' + tag.value + '",';
} else {
return '"' + tag.name + '": ' + tag[keywordsByType[tag.name]] + ',';
}
} else {
typhen.logger.debug('Unsupported tag: ' + tag.name);
return;
}
},
typeProperty: function(type, tagTable) {
if (type.isPrimitiveType) {
if (type.name === 'any') {
return;
} else if (type.name === 'number' && tagTable && tagTable.integer) {
return '"type": "integer"';
} else {
return '"type": "' + type.name + '"';
}
} else if (type.isEnum) {
return '"enum": [' + getEnumValues(type) + ']';
} else if (type.isLiteralType) {
if (type.isEnumLiteralType) {
return '"enum": [' + getEnumValue(type.enumMember) + ']';
} else {
return '"enum": [' + type.name + ']';
}
} else {
var pathSegments = _.flatten([type.ancestorModules.map(function(m) { return m.name; }), type.name], true);
var typePath = pathSegments.map(function(s) { return inflection.underscore(s); }).join('/');
return '"$ref": "' + options.baseUri + '/' + typePath + '.json"';
}
}
}
},
generate: function(generator, types, modules) {
modules.forEach(function(type) {
type.typeAliases.forEach(function(alias) {
if (_.isEmpty(alias.type.rawName)) {
aliasMap[alias.type.name] = alias.name;
if (alias.docComment) {
alias.type.docComment = alias.docComment;
}
formatResult(generator.generate('templates/base.hbs', 'underscore:**/*.json', alias));
}
});
});
types.forEach(function(type) {
if (type.numberIndex) {
typhen.logger.warn('non-string JSON property keys are not supported:', type.name);
}
switch (type.kind) {
case typhen.SymbolKind.Interface:
case typhen.SymbolKind.Class:
formatResult(generator.generate('templates/base.hbs', 'underscore:**/*.json', type));
break;
}
});
},
rename: function(type, name) {
return aliasMap[name] || name;
}
});
};