forked from SINTEF-9012/Proto2TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.ts
157 lines (127 loc) · 4.2 KB
/
command.ts
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
153
154
155
156
157
/// <reference path="./definitions/node.d.ts" />
/// <reference path="./definitions/dustjs-linkedin.d.ts" />
var argv = require('optimist')
.usage('Convert a ProtoBuf.js JSON description in TypeScript definitions.\nUsage: $0')
.demand('f')
.alias('f', 'file')
.describe('f', 'The JSON file')
.boolean('c')
.alias('c', 'camelCaseGetSet')
.describe('c', 'Generate getter and setters in camel case notation')
.default('c', true)
.boolean('u')
.alias('u', 'underscoreGetSet')
.describe('u', 'Generate getter and setters in underscore notation')
.default('u', false)
.boolean('p')
.alias('p', 'properties')
.describe('p', 'Generate properties')
.default('p', true)
.argv;
// Import in typescript and commondjs style
//var ProtoBuf = require("protobufjs");
import DustJS = require("dustjs-linkedin");
import fs = require("fs");
// Keep line breaks
DustJS.optimizers.format = (ctx, node)=> node;
// Create view filters
DustJS.filters["firstLetterInUpperCase"] = (value : string)=> {
return value.charAt(0).toUpperCase() + value.slice(1);
};
DustJS.filters["firstLetterInLowerCase"] = (value : string)=> {
return value.charAt(0).toLowerCase() + value.slice(1);
};
DustJS.filters["camelCase"] = (value: string) => {
return value.replace(/(_[a-zA-Z])/g, (match)=> match[1].toUpperCase());
};
DustJS.filters["convertType"] = (value : string)=> {
switch (value.toLowerCase()) {
case 'string':
return 'string';
case 'bool':
return 'boolean';
case 'bytes':
return 'ByteBuffer';
case 'double':
case 'float':
case 'int32':
case 'int64':
case 'uint32':
case 'uint64':
case 'sint32':
case 'sint64':
case 'fixed32':
case 'fixed64':
case 'sfixed32':
case 'sfixed64':
return "number";
}
// By default, it's a message identifier
return value;
};
DustJS.filters["optionalFieldDeclaration"] = (value : string)=> value == "optional" ? "?" : "";
DustJS.filters["repeatedType"] = (value : string)=> value == "repeated" ? "[]" : "";
function loadDustTemplate(name : string) : void {
var template = fs.readFileSync(__dirname+"/templates/"+name+".dust", "UTF8").toString(),
compiledTemplate = DustJS.compile(template, name);
DustJS.loadSource(compiledTemplate);
}
// Generate the names for the model, the types, and the interfaces
function generateNames (model : any, prefix : string, name : string = "") : void{
model.fullPackageName = prefix+(name != "." ? name : "");
// Copies the settings (I'm lazy)
model.properties = argv.properties;
model.camelCaseGetSet = argv.camelCaseGetSet;
model.underscoreGetSet = argv.underscoreGetSet;
var newDefinitions = {};
// Generate names for messages
// Recursive call for all messages
var key;
for (key in model.messages) {
var message = model.messages[key];
newDefinitions[message.name] = "Builder";
generateNames(message,model.fullPackageName, "."+(model.name ? model.name : ""));
}
// Generate names for enums
for (key in model.enums) {
var currentEnum = model.enums[key];
newDefinitions[currentEnum.name] = "";
currentEnum.fullPackageName = model.fullPackageName + (model.name ? "."+model.name : "");
}
// For fields of types which are defined in the same message,
// update the field type in consequence
for (key in model.fields) {
var field = model.fields[key];
if (typeof newDefinitions[field.type] !== "undefined") {
field.type = model.name + "." + field.type;
}
}
// Add the new definitions in the model for generate builders
var definitions: any[] = [];
for (key in newDefinitions) {
definitions.push({name: key, type: ((model.name ? (model.name + ".") : "") + key)+newDefinitions[key]});
}
model.definitions = definitions;
}
// Load dust templates
loadDustTemplate("module");
loadDustTemplate("interface");
loadDustTemplate("enum");
loadDustTemplate("builder");
// Load the json file
var model = JSON.parse(fs.readFileSync(argv.file).toString());
// If a packagename isn't present, use a default package name
if (!model.package) {
model.package = "Proto2TypeScript";
}
// Generates the names of the model
generateNames(model, model.package);
// Render the model
DustJS.render("module", model, (err, out)=> {
if (err != null) {
console.error(err);
process.exit(1);
} else {
console.log(out);
}
});