|
| 1 | +#!/usr/bin/env node |
| 2 | +// |
| 3 | +// Command line interface for Alasql |
| 4 | +// Version: 0.2.2 |
| 5 | +// Date: 28.07.2015 |
| 6 | +// (c) 2014-2015, Andrey Gershun & M. Rangel Wulff |
| 7 | +// |
| 8 | + |
| 9 | +var alasql = require('alasql'); |
| 10 | +var path = require('path'); |
| 11 | +var fs = require('fs'); |
| 12 | +var yargs = require('yargs') |
| 13 | + .demand(1) |
| 14 | + .strict() |
| 15 | + .usage('AlaSQL command-line utility (version '+alasql.version+')\n\nUsage: $0 [options] [sql] [params]') |
| 16 | + |
| 17 | + .example('$0 "sql-statement"', 'Run SQL statement and output result as JSON') |
| 18 | + .example('') |
| 19 | + .example('$0 \'value of select 2+?\' 40', 'Outputs 42') |
| 20 | + .example('') |
| 21 | + .example('$0 \'select count(*) from txt()\' < city.txt', 'Count lines in city.txt') |
| 22 | + .example('') |
| 23 | + .example('$0 \'select * into xlsx("city.xlsx") from txt("city.txt")\'', 'Convert from txt to xlsx') |
| 24 | + .example('') |
| 25 | + .example('$0 --file file.sql France 1960', 'Run SQL from file with 2 parameters') |
| 26 | + |
| 27 | + .version('v', 'Echo AlaSQL version', alasql.version) |
| 28 | + .alias('v', 'version') |
| 29 | + |
| 30 | + .boolean('m') |
| 31 | + .describe('m', 'Minify json output') |
| 32 | + .alias('m', 'minify') |
| 33 | + |
| 34 | + .describe('f', 'Load SQL from file') |
| 35 | + .alias('f', 'file') |
| 36 | + .nargs('f', 1) |
| 37 | + .normalize('f') |
| 38 | + |
| 39 | + .help('h') |
| 40 | + .alias('h', 'help') |
| 41 | + |
| 42 | + .epilog('\nMore information about the library: www.alasql.org') |
| 43 | +var argv = yargs.argv; |
| 44 | +var sql = ''; |
| 45 | +var params = []; |
| 46 | + |
| 47 | + |
| 48 | +if(argv.v) { |
| 49 | + console.log(alasql.version); |
| 50 | + process.exit(0); |
| 51 | +} |
| 52 | + |
| 53 | +if(argv.f) { |
| 54 | + if (!fs.existsSync(argv.f)) { |
| 55 | + console.log('Error: file not found'); |
| 56 | + process.exit(1); |
| 57 | + } |
| 58 | + |
| 59 | + if (isDirectory(argv.f)) { |
| 60 | + console.log('Error: file expected but directory found'); |
| 61 | + process.exit(1); |
| 62 | + } |
| 63 | + |
| 64 | + sql = fs.readFileSync(argv.f, 'utf8').toString(); |
| 65 | +} else { |
| 66 | + sql = argv._.shift() || ''; |
| 67 | +} |
| 68 | + |
| 69 | +params = argv._; |
| 70 | + |
| 71 | +if(0===sql.trim().length){ |
| 72 | + yargs.showHelp(); |
| 73 | + process.exit(1); |
| 74 | +} |
| 75 | + |
| 76 | +for(var i=1;i<params.length;i++) { |
| 77 | + var a = params[i]; |
| 78 | + if(a[0] !== '"' && a[0] !== "'") { |
| 79 | + if(+a == a){ // jshint ignore:line |
| 80 | + params[i] = +a; |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +alasql.promise(sql,params) |
| 86 | + .then(function(res){ |
| 87 | + if(!alasql.options.stdout){ |
| 88 | + console.log(formatOutput(res)); |
| 89 | + } |
| 90 | + process.exit(0); |
| 91 | + }).catch(function(err){ |
| 92 | + console.log(formatOutput({error:err})); |
| 93 | + process.exit(1); |
| 94 | + }); |
| 95 | + |
| 96 | + |
| 97 | +/** |
| 98 | + * Is a Directory |
| 99 | + * |
| 100 | + * @param {String} filePath |
| 101 | + * @returns {Boolean} |
| 102 | + */ |
| 103 | +function isDirectory(filePath){ |
| 104 | + var isDir = false; |
| 105 | + try { |
| 106 | + var absolutePath = path.resolve(filePath); |
| 107 | + isDir = fs.lstatSync(absolutePath).isDirectory(); |
| 108 | + } catch (e) { |
| 109 | + isDir = e.code === 'ENOENT'; |
| 110 | + } |
| 111 | + return isDir; |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +/** |
| 116 | + * Format output |
| 117 | + * |
| 118 | + * @param {Object} Object to be formatted according to -p flag |
| 119 | + * @returns {JSON string} |
| 120 | + */ |
| 121 | +function formatOutput(obj){ |
| 122 | + if(argv.m){ |
| 123 | + return JSON.stringify(obj); |
| 124 | + } |
| 125 | + return JSON.stringify(obj, null, 2); |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | + |
0 commit comments