-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
82 lines (61 loc) · 1.88 KB
/
index.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
import 'source-map-support/register';
import * as fs from 'fs';
import * as path from 'path';
import * as ejs from 'ejs';
import * as TYPES from './types';
import * as minimist from 'minimist';
var args = minimist(process.argv.slice(2));
let specfile = args['_'] && args['_'][0];
if (!specfile || !fs.existsSync(specfile)) {
throw "No specification file specified, you must provide a spec json file.";
}
let options = {
outputDir: args['o'] || args['outdir'] || './output',
filename: args['f'] || args['filename'] || 'serialization.ts'
}
let spec = JSON.parse(fs.readFileSync(specfile).toString());
let definition = {
TYPES,
Models: spec,
ModuleName: 'TransferModels',
capitalize,
capitalizeType
};
if (!fs.existsSync(options.outputDir)) fs.mkdirSync(options.outputDir);
let autogeneratedWarning = `
/*
------------------------------------------
THIS CODE IS AUTO-GENERATED. DO NOT EDIT.
------------------------------------------
*/
`;
let structSource = getTemplate('struct.ts');
let serializerResult = renderTemplate('serialization.ejs', definition);
let output = [
autogeneratedWarning,
structSource,
serializerResult
].join('');
let outputFile = path.join(options.outputDir, options.filename);
fs.writeFileSync(outputFile, output);
/*
------------------------------------------
Helper Functions
------------------------------------------
*/
function capitalizeType(string) {
if (string.charAt(0) === 'u') {
return 'U' + capitalize(string.slice(1));
}
return capitalize(string);
}
function capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function getTemplate(file: string) {
return fs.readFileSync('./templates/' + file).toString();
}
function renderTemplate(file: string, definition: any) {
let template = getTemplate(file);
return ejs.render(template, definition);
}