|
1 | | -function csvtojson() { |
2 | | - var web = require("../libs/interfaces").web; |
3 | | - var Converter = require("../libs/core/Converter.js"); |
4 | | - var fs = require("fs"); |
5 | | - var options = require("./options.json"); |
6 | | - var cmds = options.commands; |
7 | | - var opts = options.options; |
8 | | - var exps = options.examples; |
9 | | - var pkg=require("../package.json"); |
10 | | - /** |
11 | | - *{ |
12 | | - "cmd": "parse", command to run |
13 | | - "options": {}, options to passe to the command |
14 | | - "inputStream": process.stdin // input stream for the command. default is stdin. can be a file read stream. |
15 | | - }; |
16 | | - * |
17 | | - */ |
18 | | - var parsedCmd; |
19 | | - function _showHelp(errno) { |
20 | | - var key; |
21 | | - errno = typeof errno === "number" ? errno : 0; |
22 | | - console.log("csvtojson: Convert csv to JSON format"); |
23 | | - console.log("version:",pkg.version); |
24 | | - console.log("Usage: csvtojson [<command>] [<options>] filepath\n"); |
25 | | - console.log("Commands: "); |
26 | | - for (key in cmds) { |
27 | | - if (cmds.hasOwnProperty(key)) { |
28 | | - console.log("\t%s: %s", key, cmds[key]); |
29 | | - } |
30 | | - } |
31 | | - console.log("Options: "); |
32 | | - for (key in opts) { |
33 | | - if (opts.hasOwnProperty(key)) { |
34 | | - console.log("\t%s: %s", key, opts[key].desc); |
35 | | - } |
36 | | - } |
37 | | - console.log("Examples: "); |
38 | | - for (var i = 0; i < exps.length; i++) { |
39 | | - console.log("\t%s", exps[i]); |
40 | | - } |
41 | | - process.exit(errno); |
42 | | - } |
43 | | - |
44 | | - function parse() { |
45 | | - var is = parsedCmd.inputStream; |
46 | | - parsedCmd.options.constructResult = false; |
47 | | - parsedCmd.options.toArrayString = true; |
48 | | - if (is === process.stdin && is.isTTY) { |
49 | | - console.log("Please specify csv file path or pipe the csv data through.\n"); |
50 | | - _showHelp(1); |
51 | | - } |
52 | | - is.pipe(new Converter(parsedCmd.options)).pipe(process.stdout); |
53 | | - } |
54 | | - |
55 | | - function run(cmd, options) { |
56 | | - if (cmd === "parse") { |
57 | | - parse(); |
58 | | - } else if (cmd === "startserver") { |
59 | | - web.startWebServer(options); |
60 | | - } else if (cmd ==="version"){ |
61 | | - console.log(pkg.version); |
62 | | - }else { |
63 | | - console.log("unknown command %s.", cmd); |
64 | | - _showHelp(1); |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - function commandParser() { |
69 | | - var parsedCmd = { |
70 | | - "cmd": "parse", |
71 | | - "options": {}, |
72 | | - "inputStream": process.stdin |
73 | | - }; |
74 | | - function parseObject(val,optional){ |
75 | | - try { |
76 | | - return JSON.parse(val); |
77 | | - }catch(e){ |
78 | | - if (optional){ |
79 | | - return val; |
80 | | - }else{ |
81 | | - console.error(e); |
82 | | - process.exit(1); |
83 | | - } |
84 | | - } |
85 | | - } |
86 | | - function parseBool(str, optName) { |
87 | | - str = str.toLowerCase(); |
88 | | - if (str === "true" || str === "y") { |
89 | | - return true; |
90 | | - } else if (str === "false" || str === "n") { |
91 | | - return false; |
92 | | - } |
93 | | - console.log("Unknown boolean value %s for parameter %s.", str, optName); |
94 | | - _showHelp(1); |
95 | | - } |
96 | | - process.argv.slice(2).forEach(function(item) { |
97 | | - if (item.indexOf("--") > -1) { |
98 | | - var itemArr = item.split("="); |
99 | | - var optName = itemArr[0]; |
100 | | - var key, val, type; |
101 | | - if (!opts[optName]) { |
102 | | - console.log("Option %s not supported.", optName); |
103 | | - _showHelp(1); |
104 | | - } |
105 | | - key = optName.replace('--', ''); |
106 | | - val = itemArr[1] || ''; |
107 | | - type = opts[optName].type; |
108 | | - if (type === "string") { |
109 | | - parsedCmd.options[key] = val.toString(); |
110 | | - } else if (type === "boolean") { |
111 | | - parsedCmd.options[key] = parseBool(val, optName); |
112 | | - } else if (type ==="number"){ |
113 | | - parsedCmd.options[key] = parseFloat(val); |
114 | | - } else if (type ==="object"){ |
115 | | - parsedCmd.options[key] = parseObject(val,false); |
116 | | - }else if (type === "~object"){ |
117 | | - parsedCmd.options[key] = parseObject(val,true); |
118 | | - } else { |
119 | | - throw ({ |
120 | | - name: "UnimplementedException", |
121 | | - message: "Option type parsing not implemented. See bin/options.json" |
122 | | - }); |
123 | | - } |
124 | | - } else if (cmds[item]) { |
125 | | - parsedCmd.cmd = item; |
126 | | - } else if (fs.existsSync(item)) { |
127 | | - parsedCmd.inputStream = fs.createReadStream(item); |
128 | | - } else { |
129 | | - console.log("unknown parameter %s.", item); |
130 | | - } |
131 | | - }); |
132 | | - return parsedCmd; |
133 | | - } |
134 | | - process.stdin.setEncoding('utf8'); |
135 | | - parsedCmd = commandParser(); |
136 | | - run(parsedCmd.cmd, parsedCmd.options); |
137 | | -} |
138 | | -module.exports = csvtojson; |
139 | | -if (!module.parent) { |
140 | | - csvtojson(); |
141 | | -} |
| 1 | +function csvtojson() { |
| 2 | + var web = require("../libs/interfaces").web; |
| 3 | + var Converter = require("../libs/core/Converter.js"); |
| 4 | + var fs = require("fs"); |
| 5 | + var options = require("./options.json"); |
| 6 | + var cmds = options.commands; |
| 7 | + var opts = options.options; |
| 8 | + var exps = options.examples; |
| 9 | + var pkg = require("../package.json"); |
| 10 | + /** |
| 11 | + *{ |
| 12 | + "cmd": "parse", command to run |
| 13 | + "options": {}, options to passe to the command |
| 14 | + "inputStream": process.stdin // input stream for the command. default is stdin. can be a file read stream. |
| 15 | + }; |
| 16 | + * |
| 17 | + */ |
| 18 | + var parsedCmd; |
| 19 | + |
| 20 | + function _showHelp(errno) { |
| 21 | + var key; |
| 22 | + errno = typeof errno === "number" ? errno : 0; |
| 23 | + console.log("csvtojson: Convert csv to JSON format"); |
| 24 | + console.log("version:", pkg.version); |
| 25 | + console.log("Usage: csvtojson [<command>] [<options>] filepath\n"); |
| 26 | + console.log("Commands: "); |
| 27 | + for (key in cmds) { |
| 28 | + if (cmds.hasOwnProperty(key)) { |
| 29 | + console.log("\t%s: %s", key, cmds[key]); |
| 30 | + } |
| 31 | + } |
| 32 | + console.log("Options: "); |
| 33 | + for (key in opts) { |
| 34 | + if (opts.hasOwnProperty(key)) { |
| 35 | + console.log("\t%s: %s", key, opts[key].desc); |
| 36 | + } |
| 37 | + } |
| 38 | + console.log("Examples: "); |
| 39 | + for (var i = 0; i < exps.length; i++) { |
| 40 | + console.log("\t%s", exps[i]); |
| 41 | + } |
| 42 | + process.exit(errno); |
| 43 | + } |
| 44 | + |
| 45 | + function parse() { |
| 46 | + var is = parsedCmd.inputStream; |
| 47 | + parsedCmd.options.constructResult = false; |
| 48 | + if (parsedCmd.options.toArrayString === undefined) { |
| 49 | + parsedCmd.options.toArrayString = true; |
| 50 | + } |
| 51 | + if (parsedCmd.options.maxRowLength === undefined) { |
| 52 | + parsedCmd.options.maxRowLength = 10240; |
| 53 | + } |
| 54 | + if (is === process.stdin && is.isTTY) { |
| 55 | + console.log("Please specify csv file path or pipe the csv data through.\n"); |
| 56 | + _showHelp(1); |
| 57 | + } |
| 58 | + var conv = new Converter(parsedCmd.options); |
| 59 | + conv.on("error", function (err, pos) { |
| 60 | + if (!parsedCmd.options.quiet) { |
| 61 | + console.error("csvtojson got an error: ", err); |
| 62 | + if (pos) { |
| 63 | + console.error("The error happens at following line: "); |
| 64 | + console.log(pos); |
| 65 | + } |
| 66 | + } |
| 67 | + process.exit(1); |
| 68 | + }) |
| 69 | + is.pipe(conv).pipe(process.stdout); |
| 70 | + } |
| 71 | + |
| 72 | + function run(cmd, options) { |
| 73 | + if (cmd === "parse") { |
| 74 | + parse(); |
| 75 | + } else if (cmd === "startserver") { |
| 76 | + web.startWebServer(options); |
| 77 | + } else if (cmd === "version") { |
| 78 | + console.log(pkg.version); |
| 79 | + } else { |
| 80 | + console.log("unknown command %s.", cmd); |
| 81 | + _showHelp(1); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + function commandParser() { |
| 86 | + var parsedCmd = { |
| 87 | + "cmd": "parse", |
| 88 | + "options": {}, |
| 89 | + "inputStream": process.stdin |
| 90 | + }; |
| 91 | + |
| 92 | + function parseObject(val, optional) { |
| 93 | + try { |
| 94 | + return JSON.parse(val); |
| 95 | + } catch (e) { |
| 96 | + if (optional) { |
| 97 | + return val; |
| 98 | + } else { |
| 99 | + console.error(e); |
| 100 | + process.exit(1); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + function parseBool(str, optName) { |
| 106 | + str = str.toLowerCase(); |
| 107 | + if (str === "true" || str === "y") { |
| 108 | + return true; |
| 109 | + } else if (str === "false" || str === "n") { |
| 110 | + return false; |
| 111 | + } |
| 112 | + console.log("Unknown boolean value %s for parameter %s.", str, optName); |
| 113 | + _showHelp(1); |
| 114 | + } |
| 115 | + process.argv.slice(2).forEach(function (item) { |
| 116 | + if (item.indexOf("--") > -1) { |
| 117 | + var itemArr = item.split("="); |
| 118 | + var optName = itemArr[0]; |
| 119 | + var key, val, type; |
| 120 | + if (!opts[optName]) { |
| 121 | + console.log("Option %s not supported.", optName); |
| 122 | + _showHelp(1); |
| 123 | + } |
| 124 | + key = optName.replace('--', ''); |
| 125 | + val = itemArr[1] || ''; |
| 126 | + type = opts[optName].type; |
| 127 | + if (type === "string") { |
| 128 | + parsedCmd.options[key] = val.toString(); |
| 129 | + } else if (type === "boolean") { |
| 130 | + parsedCmd.options[key] = parseBool(val, optName); |
| 131 | + } else if (type === "number") { |
| 132 | + parsedCmd.options[key] = parseFloat(val); |
| 133 | + } else if (type === "object") { |
| 134 | + parsedCmd.options[key] = parseObject(val, false); |
| 135 | + } else if (type === "~object") { |
| 136 | + parsedCmd.options[key] = parseObject(val, true); |
| 137 | + } else { |
| 138 | + throw ({ |
| 139 | + name: "UnimplementedException", |
| 140 | + message: "Option type parsing not implemented. See bin/options.json" |
| 141 | + }); |
| 142 | + } |
| 143 | + } else if (cmds[item]) { |
| 144 | + parsedCmd.cmd = item; |
| 145 | + } else if (fs.existsSync(item)) { |
| 146 | + parsedCmd.inputStream = fs.createReadStream(item); |
| 147 | + } else { |
| 148 | + console.log("unknown parameter %s.", item); |
| 149 | + } |
| 150 | + }); |
| 151 | + return parsedCmd; |
| 152 | + } |
| 153 | + process.stdin.setEncoding('utf8'); |
| 154 | + parsedCmd = commandParser(); |
| 155 | + run(parsedCmd.cmd, parsedCmd.options); |
| 156 | +} |
| 157 | +module.exports = csvtojson; |
| 158 | +if (!module.parent) { |
| 159 | + csvtojson(); |
| 160 | +} |
0 commit comments