forked from odk-x/app-designer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacGenConverter.js
116 lines (95 loc) · 3.22 KB
/
macGenConverter.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
var XRegExp = require('./xlsxconverter/XRegExp-All-3.0.0-pre-2014-12-24.js');
var readline = require('readline');
var _ = require('./devEnv/libs/underscore.js');
var XLSXConverter = require('./xlsxconverter/XLSXConverter2.js');
var fs = require('fs');
var path = require('path');
var Buffer = require('buffer').Buffer;
var constants = require('constants');
var XLSX = require('xlsx');
var util = require('./devEnv/js/devenv-util.js');
function to_json(workbook) {
var result = {};
_.each(workbook.SheetNames, function(sheetName) {
var rObjArr = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName],{raw:true});
rObjArr = util.removeEmptyStrings(rObjArr);
if(rObjArr.length > 0){
result[sheetName] = rObjArr;
}
});
return result;
}
function downloadCsvFile (csvContent, fileName) {
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", fileName);
link.click();
}
function writeOutDef(formDefStr) {
// Write the definition.csv if necessary
var formDefJson = JSON.parse(formDefStr);
var tableId = util.getTableIdFromFormDef(formDefJson);
var dtm = formDefJson.specification.dataTableModel;
if (!fs.existsSync(defPath)) {
var defCsv = util.createDefinitionCsvFromDataTableModel(dtm);
fs.writeFileSync(defPath, defCsv);
}
}
function writeOutProp(formDefStr) {
// Write the properties.csv if necessary
var formDefJson = JSON.parse(formDefStr);
var tableId = util.getTableIdFromFormDef(formDefJson);
var dtm = formDefJson.specification.dataTableModel;
if (!fs.existsSync(propPath)) {
var propCsv = util.createPropertiesCsvFromDataTableModel(dtm, formDefJson);
fs.writeFileSync(propPath, propCsv);
}
}
var result = "";
var b64xlsx = "";
var processedWorkbook = {};
// Make sure we got a filename on the command line.
if (process.argv.length < 3) {
console.log('Usage: node ' + process.argv[1] + ' FILENAME');
process.exit(1);
}
// Read the file
var fs = require('fs');
var filename = process.argv[2];
fs.readFile(filename, 'base64', function(err, data) {
if (err) {
throw err;
}
b64xlsx += data;
});
b64xlsx = fs.readFileSync(filename, 'base64');
try {
var xlsx = XLSX.read(b64xlsx, {type: 'base64'});
var jsonWorkbook = to_json(xlsx);
var processedWorkbook = XLSXConverter.processJSONWb(jsonWorkbook);
result = JSON.stringify(processedWorkbook, 2, 2);
} catch(e) {
console.log(e.stack);
}
console.log(result);
// Write out the definition.csv if necessary
var writeOutDefAndProp = util.shouldWriteOutDefinitionAndPropertiesCsv(result);
var tableId = util.getTableIdFromFormDef(processedWorkbook);
var defPath = 'app/config/tables/' + tableId + '/definition.csv';
try {
if (writeOutDefAndProp) {
writeOutDef(result);
}
} catch (e) {
fs.writeFileSync(defPath, e.stack);
}
// Write out the properties.csv if necessary
var propPath = 'app/config/tables/' + tableId + '/properties.csv';
try {
if (util.shouldWriteOutDefinitionAndPropertiesCsv(result)) {
writeOutProp(result);
}
} catch (e) {
fs.writeFileSync(propPath, e.stack);
}