-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeGen.js
62 lines (47 loc) · 1.69 KB
/
codeGen.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
const assert = require('assert');
const _ = require('lodash');
function _writeIndentation(output) {
for(var i = 0; i < output.indentation; ++i)
output.text += "\t";
}
function _outdent(output) {
assert(output.indentation > 0, "outdent called on a file output that was already at 0 indentation level");
output.indentation--;
_writeIndentation(output);
output.text += "}\n"
}
function getNamespaceName(moduleName) {
var camelCased = moduleName.replace(/-([a-z])/g, (g) => { return g[1].toUpperCase(); });
return camelCased.replace(/^[a-z]/g, (g) => { return g[0].toUpperCase(); });
}
function generateFileText(output) {
var namespace = getNamespaceName(output.moduleName);
output.indentation = 0;
output.text = `// This typing info was generated by ts-prop-gen.\n\n`;
output.text += `declare namespace ${namespace} {\n`;
output.indentation++;
_.each(output.interfaces, (element, key) => {
_writeIndentation(output);
output.text += `export interface ${element.name} extends React.Props<${element.componentClassName}> {\n`;
output.indentation ++;
_.each(element.members, (member, key) => {
_writeIndentation(output);
output.text += `${member.name}: ${member.type};\n`;
});
_outdent(output);
});
_.each(output.classes, (element, key) => {
_writeIndentation(output);
output.text += `export class ${element.name} extends React.Component<${element.propsInterface}, {}> {\n`;
output.indentation++;
// TODO: Do we ever need to put anything in this?
_outdent(output);
});
_outdent(output);
output.text += `declare module "${output.moduleName}" {\n`
output.text += ` export = ${namespace};\n`;
output.text += `}\n`;
}
module.exports = {
'generateFileText': generateFileText
}