Repository navigation
Expand file tree
/
Copy pathconvert.js
More file actions
188 lines (166 loc) · 3.76 KB
/
Copy pathconvert.js
File metadata and controls
188 lines (166 loc) · 3.76 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// normalize-sales-data
const clear = require("clear");
const outputTypes = require("./src/common/output-types");
const exitProgram = require("./src/common/exit-program");
const fileArg = require("./src/file-arg");
const fileCheck = require("./src/file-check");
const fileRead = require("./src/file-read");
const procData = require("./src/proc-data");
const outputFolder = require("./src/output-folder");
const outputCsvFiles = require("./src/output-csv-files");
const outputSqlFiles = require("./src/output-sql-files");
const outputJsonFiles = require("./src/output-json-files");
runDataNormalization();
// Main function.
function runDataNormalization()
{
var givenOutputType = "";
var preparedFlag = -1;
// Reads and validates file type argument.
givenOutputType = fileArg.readFileType();
if (givenOutputType.length > 0)
{
preparedFlag = fileArg.prepareFileType(givenOutputType);
}
if (preparedFlag > 0)
{
clear();
outputTypes.saveEntry(preparedFlag);
callInputFileCheck();
}
}
// Check source CSV file.
function callInputFileCheck()
{
fileCheck.checkInput(function (inpChkErr)
{
if (inpChkErr !== null)
{
exitProgram.callError(inpChkErr.message, true);
}
else
{
callInputFileRead();
}
});
}
// Read source file.
function callInputFileRead()
{
fileRead.readInput(function (inpReadErr, inpReadData)
{
if (inpReadErr !== null)
{
exitProgram.callError(inpReadErr.message, true);
}
else
{
callDataProcessing(inpReadData);
}
});
}
// Normalize CSV data.
function callDataProcessing(retrievedCsvData)
{
procData.processData(retrievedCsvData, function (dataProcErr, normalizedDataObject)
{
if (dataProcErr !== null)
{
exitProgram.callError(dataProcErr.message, true);
}
else
{
callOutputFolder(normalizedDataObject);
}
});
}
// Create output folder.
function callOutputFolder(normalizedDataObj)
{
outputFolder.createFolder(function (outputFolderErr, outputFolderPath)
{
if (outputFolderErr !== null)
{
exitProgram.callError(outputFolderErr.message, true);
}
else
{
callOutputFileWrite(normalizedDataObj, outputFolderPath);
}
});
}
// Coordinate output.
function callOutputFileWrite(normalizedData, oFolderPth)
{
var oType = outputTypes.getEntry();
if (oType === outputTypes.modes.SQL)
{
// SQL definition files.
callSqlOutput(normalizedData, oFolderPth);
}
else if (oType === outputTypes.modes.CSV)
{
// CSV data files.
callCsvOutput(normalizedData, oFolderPth);
}
else if (oType === outputTypes.modes.ARRAY)
{
// Multi-dimensional JSON array files.
callJsonOutput(normalizedData, oFolderPth, false);
}
else if (oType === outputTypes.modes.OBJECT)
{
// JSON object array files.
callJsonOutput(normalizedData, oFolderPth, true);
}
else
{
// Error
exitProgram.callError("Invalid Output Type", true);
}
}
// Output normalized data as SQL files.
function callSqlOutput(normData, oFolder)
{
outputSqlFiles.writeDataFiles(normData, oFolder, function (sqlOutputErr, sqlOutputRes)
{
if (sqlOutputErr !== null)
{
exitProgram.callError(sqlOutputErr.message, true);
}
else
{
exitProgram.callSuccessful();
}
});
}
// Output normalized data as CSV files.
function callCsvOutput(normData, oFolder)
{
outputCsvFiles.writeDataFiles(normData, oFolder, function (csvOutputErr, csvOutputRes)
{
if (csvOutputErr !== null)
{
exitProgram.callError(csvOutputErr.message, true);
}
else
{
exitProgram.callSuccessful();
}
});
}
// Output normalized data as JSON files.
function callJsonOutput(normData, oFolder, objMode)
{
outputJsonFiles.writeDataFiles(normData, oFolder, objMode, function (jsonOutputErr, jsonOutputRes)
{
if (jsonOutputErr !== null)
{
exitProgram.callError(jsonOutputErr.message, true);
}
else
{
exitProgram.callSuccessful();
}
});
}