forked from sipin/gorazor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorazor.js
87 lines (68 loc) · 2.14 KB
/
gorazor.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
var vash = require('./vash.js');
var fs = require('fs');
var path = require('path');
var gz_extension = ".gohtml";
var go_extension = ".go";
if (process.argv.length != 4) {
console.log("Usage: \nnode gorazor.js template_folder_path output_path");
return;
}
var tpl_folder = process.argv[2];
var output_folder = process.argv[3];
function get_names(tpl_path) {
var parts = tpl_path.split(path.sep);
var filename = parts[parts.length - 1];
var names = {};
names.package_name = parts[parts.length - 2];
var func_name = filename.substr(0, filename.length - gz_extension.length);
//Capitalize first character
names.func_name = func_name.substr(0, 1).toUpperCase() + func_name.substr(1);
return names;
}
function normalize_sep(folder_path) {
if (folder_path[folder_path.length - 1] != path.sep) {
folder_path = folder_path + path.sep;
}
return folder_path;
}
function process_file(tpl_path, output_path) {
fs.readFile(tpl_path, 'utf8', function(err, data) {
if (err) throw err;
var options = {};
var names = get_names(tpl_path);
options["package"] = names.package_name;
options["name"] = names.func_name;
var code = vash.compile(data, options).toString();
fs.writeFileSync(output_path, code);
});
}
function process_folder(tpl_path, output_path) {
tpl_path = normalize_sep(tpl_path);
output_path = normalize_sep(output_path);
if(!fs.existsSync(output_path)) {
fs.mkdirSync(output_path);
}
fs.readdir(tpl_path, function(err, files) {
if (err) throw err;
console.log("Processing Folder: " + tpl_path);
//process files
for(var i=0; i < files.length; i++) {
var input = tpl_path + files[i];
var stats = fs.statSync(input);
if (stats.isFile() && path.extname(input) == gz_extension) {
var output = output_path + files[i].replace(gz_extension, go_extension);
console.log("Processing File: " + input);
process_file(input, output);
}
}
//process sub-folders
for(var i=0; i < files.length; i++) {
var input = tpl_path + files[i];
var stats = fs.statSync(input);
if (stats.isDirectory()) {
process_folder(input, output_path + files[i]);
}
}
});
}
process_folder(tpl_folder, output_folder);