forked from zk-luke/xlsx2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (146 loc) · 4.54 KB
/
index.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
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
'use strict'
var xlsx = require('./lib/xlsx-to-json.js');
var path = require('path');
var shell = require('child_process');
var fs = require('fs');
var glob = require('glob');
var config = require('./config.json');
/**
* all commands
*/
var commands = {
"--help": {
"alias": ["-h"],
"desc": "show this help manual.",
"action": showHelp
},
"--export": {
"alias": ["-e"],
"desc": "export excel to json. --export [files]",
"action": exportJson,
"default": true
}
};
var keys = []; //cache of command's key ("--help"...)
var alias_map = {}; // mapping of alias_name -> name
var parsed_cmds = []; //cmds of parsed out.
// process.on('uncaughtException', function(err) {
// console.log('error: ' + err);
// });
/**
* initialize
*/
(function() {
keys = Object.keys(commands);
for (var key in commands) {
var alias_array = commands[key]["alias"];
alias_array.forEach(function(element, index, array) {
alias_map[element] = key;
});
};
parsed_cmds = parseCommandLine(process.argv);
// console.log("%j", parsed_cmds);
parsed_cmds.forEach(function(element, index, array) {
exec(element);
});
})();
/**
* export json
* args: --export [cmd_line_args] [.xlsx files list].
*/
function exportJson(args) {
if (typeof args === 'undefined' || args.length == 0) {
glob(config.xlsx.src, function(err, files) {
if (err) {
console.error("exportJson error:", err);
throw err;
};
files.forEach(function(element, index, array) {
xlsx.toJson(path.join(__dirname, element), path.join(__dirname, config.xlsx.dest), config.xlsx.head,config.xlsx.arraySeparator);
});
})
} else {
if (args instanceof Array) {
args.forEach(function(element, index, array) {
xlsx.toJson(path.join(__dirname, element), path.join(__dirname, config.xlsx.dest), config.xlsx.head,config.xlsx.arraySeparator);
});
};
}
};
/**
* show help
*/
function showHelp() {
var usage = "usage: \n";
for (var p in commands) {
if (typeof commands[p] !== "function") {
usage += "\t " + p + "\t " + commands[p].alias + "\t " + commands[p].desc + "\n ";
};
};
usage += "\nexamples: ";
usage += "\n\n $node index.js --export\n\tthis will export all files configed to json.";
usage += "\n\n $node index.js --export ./excel/foo.xlsx ./excel/bar.xlsx\n\tthis will export foo and bar xlsx files.";
console.log(usage);
};
/**************************** parse command line *********************************/
/**
* execute a command
*/
function exec(cmd) {
if (typeof cmd.action === "function") {
cmd.action(cmd.args);
};
};
/**
* parse command line args
*/
function parseCommandLine(args) {
var parsed_cmds = [];
if (args.length <= 2) {
parsed_cmds.push(defaultCommand());
} else {
var cli = args.slice(2);
var pos = 0;
var cmd;
cli.forEach(function(element, index, array) {
//replace alias name with real name.
if (element.indexOf('--') === -1 && element.indexOf('-') === 0) {
cli[index] = alias_map[element];
}
//parse command and args
if (cli[index].indexOf('--') === -1) {
cmd.args.push(cli[index]);
} else {
if (keys[cli[index]] == "undefined") {
throw new Error("not support command:" + cli[index]);
};
pos = index;
cmd = commands[cli[index]];
if (typeof cmd.args == 'undefined') {
cmd.args = [];
};
parsed_cmds.push(cmd);
}
});
};
return parsed_cmds;
};
/**
* default command when no command line argas provided.
*/
function defaultCommand() {
if (keys.length <= 0) {
throw new Error("Error: there is no command at all!");
};
for (var p in commands) {
if (commands[p]["default"]) {
return commands[p];
};
};
if (keys["--help"]) {
return commands["--help"];
} else {
return commands[keys[0]];
};
};
/*************************************************************************/