-
Notifications
You must be signed in to change notification settings - Fork 175
/
index.js
173 lines (138 loc) · 3.61 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
const xlsx = require('./lib/xlsx-to-json.js');
const path = require('path');
const glob = require('glob');
const config = require('./config.json');
/**
* all commands
*/
let 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
}
};
let alias_map = {}; // mapping of alias_name -> name
let parsed_cmds = []; //cmds of parsed out.
// process.on('uncaughtException', function(err) {
// console.log('error: ' + err);
// });
//cache of command's key ("--help"...)
let keys = Object.keys(commands);
for (let key in commands) {
let alias_array = commands[key].alias;
alias_array.forEach(e => {
alias_map[e] = key;
});
}
parsed_cmds = parseCommandLine(process.argv);
// console.log("%j", parsed_cmds);
parsed_cmds.forEach(function (e) {
exec(e);
});
/**
* 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(item => {
xlsx.toJson(path.join(__dirname, item), path.join(__dirname, config.xlsx.dest));
});
});
} else {
if (args instanceof Array) {
args.forEach(item => {
xlsx.toJson(path.join(__dirname, item), path.join(__dirname, config.xlsx.dest));
});
}
}
}
/**
* show help
*/
function showHelp() {
let usage = "usage: \n";
for (let 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) {
let parsed_cmds = [];
if (args.length <= 2) {
parsed_cmds.push(defaultCommand());
} else {
let cli = args.slice(2);
let pos = 0;
let 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 (let p in commands) {
if (commands[p]["default"]) {
return commands[p];
}
}
if (keys["--help"]) {
return commands["--help"];
} else {
return commands[keys[0]];
}
}
/*************************************************************************/