This repository was archived by the owner on Feb 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
78 lines (66 loc) · 2.64 KB
/
Copy pathapp.js
File metadata and controls
78 lines (66 loc) · 2.64 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
#!/usr/bin/env node
var fs = require('fs')
var path = require('path')
var yargs = require('yargs')
var sql2xls = require('./index.js')
// Parse CLI
var argv = yargs
.usage('query2xls path/to/config.json')
// Query Related INPUT/OUTPUT
.alias('q', 'query') .describe('q', 'SQL Query String')
.alias('r', 'read') .describe('r', 'Read SQL Query File (UTF8)')
.alias('o', 'output') .describe('o', 'Output XLS File')
// Database Settings
.alias('u', 'username') .describe('u', 'Database Username')
.alias('p', 'password') .describe('p', 'Database Password')
.alias('s', 'server') .describe('s', 'Database Server Address')
.alias('i', 'instance') .describe('i', 'Database Instance Name')
.alias('d', 'database') .describe('d', 'Database Name')
.argv
// Read config file
var configFile = argv._[0]
if (typeof configFile !== 'string' && !fs.existsSync(configFile)) {
console.error('ERROR: Can\'t find config file ' + configFile + '\r\n')
yargs.showHelp()
process.exit(1)
}
// Read config
config = JSON.parse(fs.readFileSync(configFile, "utf8"))
// Set output file path relative to config file
config.output = path.resolve(path.dirname(configFile),
config.output || path.basename(configFile, path.extname(configFile))+'.xls')
// Set queryFile relative to config file
config.read = path.resolve(path.dirname(configFile),
config.read || path.basename(configFile, path.extname(configFile))+'.sql')
// Overwrite with arguments
if (!!argv.query) config.query = argv.query
if (!!argv.read) config.read = argv.read
if (!!argv.output) config.output = argv.output
if (!!argv.username) config.username = argv.username
if (!!argv.password) config.password = argv.password
if (!!argv.server) config.server = argv.server
if (!!argv.instance) config.instance = argv.instance
if (!!argv.database) config.database = argv.database
// Apply defaults
config.output = config.output || 'output.xls'
// Demand query, or query file
if (!config.query) {
if (!config.read) {
console.error('ERROR: Either query string or query file is required!\r\n')
yargs.showHelp()
process.exit(1)
}
if (!fs.existsSync(config.read)) {
console.error('ERROR: Can\'t find queryFile ' + config.read + '\r\n')
yargs.showHelp()
process.exit(1)
}
// Read from file
config.query = fs.readFileSync(config.read, "utf8")
}
// Execute
sql2xls(config, function (err, buffer) {
if (!!err) throw err;
fs.writeFileSync(config.output, buffer)
process.exit(0)
}, console.log)