-
Notifications
You must be signed in to change notification settings - Fork 0
/
jk-json-generator.js
114 lines (91 loc) · 3.8 KB
/
jk-json-generator.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
// Author: Jobin Kurian
// Email: [email protected]
var fs = require('fs');
var path = require('path');
var dummyjson = require('dummy-json');
var jsonlint = require("jsonlint");
var input_dir = "input_template";
var output_dir = "json_output";
var output_extenstion = ".json";
var template_extenstion = ".jk";
// Checking if the input directory exist.
if (!fs.existsSync(input_dir)){
console.log("\n\nCould not find the templates folder. \n\nPlease create folder named '" + input_dir + "' and add some templates with '" + template_extenstion + "' extention for converstion.\n\n");
process.exit(1);
}
// Checking and creating output directory.
if (!fs.existsSync(output_dir)){
fs.mkdirSync(output_dir);
}
// Getting all the files inside the input directory.
var all_files_array = getAllTemplateFiles(input_dir);
// Taversing through the input_dir.
all_files_array.forEach(function (templateFile, index) {
var template_file_data = fs.readFileSync(templateFile, {encoding : 'utf8'});
var generated_JSON_data = dummyjson.parse(template_file_data);
// Checking if there is an error in the generated JSON.
// If there is error that means the original template is having error.
try {
// Validating the generated J
jsonlint.parse(generated_JSON_data);
writeToFile(templateFile, generated_JSON_data);
console.log('Converting %d/%d', index + 1, index + 1);
if (all_files_array.length - 1 == index) {
console.log("Completed Converting All Files To JSON");
};
} catch (err) {
console.log("\n\nThere is an error in the template '" + templateFile + "'\nPlease correct the below error and re-run the converstion");
console.log(err + "\n\n");
}
});
// Function to file the json data to the file
function writeToFile(templateFile, generated_JSON_data) {
// This will remove the filename and give back the path without the file.
var path_without_file = path.dirname(templateFile);
var parentFolder = null;
// Checking if the path has subdirectory
if (path_without_file.indexOf("/") != -1) {
parentFolder = path_without_file.substr(path_without_file.indexOf('/'), path_without_file.length);
}
// Creating the finale folder directory where the files will be saved.
var output_folderpath = null
if (parentFolder) {
output_folderpath = output_dir + parentFolder + '/';
}
else {
output_folderpath = output_dir + '/';
}
// Creating the folder structure to save the file.
if (!fs.existsSync(output_folderpath)){
fs.mkdirSync(output_folderpath);
}
// The output file path where the JSON will be saved.
var output_filepath = output_folderpath + path.basename(templateFile, template_extenstion) + output_extenstion;
// Writing the JSON data to the file.
fs.writeFile(output_filepath, generated_JSON_data, function(err) {
if(err) {
console.log("Error in writing to a file " +err);
return;
}
});
}
// Function to get all the templates inside the input directory
function getAllTemplateFiles (dir, files_) {
files_ = files_ || [];
var files = fs.readdirSync(dir);
for (var i in files){
var name = dir + '/' + files[i];
if (fs.statSync(name).isDirectory()){
getAllTemplateFiles(name, files_);
} else {
if (path.extname(name) == template_extenstion) {
files_.push(name);
}
}
}
if (files_.length == 0) {
console.log("\n\nThe Template Folder Seems To Be Empty :( \n\nPlease add some templates with '" + template_extenstion + "' extention for converstion.\n\n");
process.exit(1);
};
return files_;
}