forked from SINTEF-9012/Proto2TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.js
executable file
·143 lines (118 loc) · 4.46 KB
/
command.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
#!/usr/bin/env node
/// <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");
var DustJS = require("dustjs-helpers");
var fs = require("fs");
// Keep line breaks
DustJS.optimizers.format = function (ctx, node) {
return node;
};
// Create view filters
DustJS.filters["firstLetterInUpperCase"] = function (value) {
return value.charAt(0).toUpperCase() + value.slice(1);
};
DustJS.filters["firstLetterInLowerCase"] = function (value) {
return value.charAt(0).toLowerCase() + value.slice(1);
};
DustJS.filters["camelCase"] = function (value) {
return value.replace(/(_[a-zA-Z])/g, function (match) {
return match[1].toUpperCase();
});
};
DustJS.filters["convertType"] = function (value) {
switch (value.toLowerCase()) {
case 'string':
return 'string';
case 'bool':
return 'boolean';
case 'bytes':
return 'ByteBuffer';
case 'double':
case 'float':
case 'int32':
case 'uint32':
case 'sint32':
case 'fixed32':
case 'sfixed32':
return "number";
case 'int64':
case 'uint64':
case 'sint64':
case 'fixed64':
case 'sfixed64':
return "Long";
}
// By default, it's a message identifier
return value;
};
DustJS.filters["optionalFieldDeclaration"] = function (value) {
return value == "optional" ? "?" : "";
};
DustJS.filters["repeatedType"] = function (value) {
return value == "repeated" ? "[]" : "";
};
function loadDustTemplate(name) {
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, prefix, name) {
if (typeof name === "undefined") { name = ""; }
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 : ""));
}
for (key in model.enums) {
var currentEnum = model.enums[key];
newDefinitions[currentEnum.name] = "";
currentEnum.fullPackageName = model.fullPackageName + (model.name ? "." + model.name : "");
}
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 = [];
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, function (err, out) {
if (err != null) {
console.error(err);
process.exit(1);
} else {
console.log(out);
}
});
//# sourceMappingURL=command.js.map