11#!/usr/bin/env node
2- //
2+ //
33// Command line interface for Alasql
44// Version: 0.2.2
55// Date: 28.07.2015
66// (c) 2014-2015, Andrey Gershun & M. Rangel Wulff
77//
88
9- var alasql = require ( 'alasql' ) ;
10- var path = require ( 'path' ) ;
11- var fs = require ( 'fs' ) ;
12- var yargs = require ( 'yargs' )
13- . demand ( 1 )
9+ let alasql = require ( 'alasql' ) ;
10+ let path = require ( 'path' ) ;
11+ let fs = require ( 'fs' ) ;
12+ let stdin = process . openStdin ( ) ;
13+ let yargs = require ( 'yargs' )
1414 . 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 ( '' )
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 ( '' )
2121 . example ( '$0 \'select count(*) from txt()\' < city.txt' , 'Count lines in city.txt' )
22- . example ( '' )
22+ . example ( '' )
2323 . 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' )
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' )
2929
30- . boolean ( 'm' )
31- . describe ( 'm' , 'Minify json output' )
32- . alias ( 'm' , 'minify' )
30+ . boolean ( 'm' )
31+ . describe ( 'm' , 'Minify json output' )
32+ . alias ( 'm' , 'minify' )
3333
34- . describe ( 'f' , 'Load SQL from file' )
35- . alias ( 'f' , 'file' )
36- . nargs ( 'f' , 1 )
37- . normalize ( 'f' )
34+ . describe ( 'f' , 'Load SQL from file' )
35+ . alias ( 'f' , 'file' )
36+ . nargs ( 'f' , 1 )
37+ . normalize ( 'f' )
3838
39- . help ( 'h' )
40- . alias ( 'h' , 'help' )
39+ . help ( 'h' )
40+ . alias ( 'h' , 'help' )
4141
42- . epilog ( '\nMore information about the library: www.alasql.org' )
43- var argv = yargs . argv ;
44- var sql = '' ;
45- var params = [ ] ;
42+ . epilog ( '\nMore information about the library: www.alasql.org' )
4643
44+ let argv = yargs . argv ;
45+ let sql = '' ;
46+ let params = [ ] ;
47+ let pipedData = ''
48+ stdin . on ( 'data' , function ( chunk ) {
49+ pipedData += chunk ;
50+ } ) ;
4751
48- if ( argv . v ) {
49- console . log ( alasql . version ) ;
52+ if ( argv . v ) {
53+ console . log ( alasql . version ) ;
5054 process . exit ( 0 ) ;
5155}
5256
53- if ( argv . f ) {
57+ if ( argv . f ) {
5458 if ( ! fs . existsSync ( argv . f ) ) {
55- console . log ( 'Error: file not found' ) ;
59+ console . error ( 'Error: file not found' ) ;
5660 process . exit ( 1 ) ;
57- }
61+ }
5862
5963 if ( isDirectory ( argv . f ) ) {
60- console . log ( 'Error: file expected but directory found' ) ;
64+ console . error ( 'Error: file expected but directory found' ) ;
6165 process . exit ( 1 ) ;
6266 }
6367
6468 sql = fs . readFileSync ( argv . f , 'utf8' ) . toString ( ) ;
69+ execute ( sql , argv . _ ) ;
70+
6571} else {
6672 sql = argv . _ . shift ( ) || '' ;
73+
74+ // if data is not piped
75+ if ( Boolean ( process . stdin . isTTY ) ) {
76+ execute ( sql , argv . _ ) ;
77+ }
6778}
6879
69- params = argv . _ ;
80+ // if data is piped
81+ stdin . on ( 'end' , function ( ) {
82+ execute ( pipedData , argv . _ ) ;
83+ } ) ;
7084
71- if ( 0 === sql . trim ( ) . length ) {
72- yargs . showHelp ( ) ;
73- process . exit ( 1 ) ;
74- }
85+ /**
86+ * Execute SQL query
87+ *
88+ * @sql {String} SQL query
89+ * @param {String } Parameters
90+ * @returns {null } Result will be printet to console.log
91+ */
92+ function execute ( sql , params ) {
7593
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- }
94+ if ( 0 === sql . trim ( ) . length ) {
95+ console . error ( "\nNo SQL to process\n" ) ;
96+ yargs . showHelp ( ) ;
97+ process . exit ( 1 ) ;
8298 }
83- }
8499
85- alasql . promise ( sql , params )
86- . then ( function ( res ) {
87- if ( ! alasql . options . stdout ) {
88- console . log ( formatOutput ( res ) ) ;
100+ for ( var i = 1 ; i < params . length ; i ++ ) {
101+ var a = params [ i ] ;
102+ if ( a [ 0 ] !== '"' && a [ 0 ] !== "'" ) {
103+ if ( + a == a ) { // jshint ignore:line
104+ params [ i ] = + a ;
105+ }
89106 }
90- process . exit ( 0 ) ;
91- } ) . catch ( function ( err ) {
92- console . log ( formatOutput ( { error :err } ) ) ;
93- process . exit ( 1 ) ;
94- } ) ;
107+ }
95108
109+ alasql . promise ( sql , params )
110+ . then ( function ( res ) {
111+ if ( ! alasql . options . stdout ) {
112+ console . log ( formatOutput ( res ) ) ;
113+ }
114+ process . exit ( 0 ) ;
115+ } ) . catch ( function ( err ) {
116+ let errorJsonObj = JSON . parse ( JSON . stringify ( err , Object . getOwnPropertyNames ( err ) ) ) ;
117+ console . error ( formatOutput ( {
118+ error : errorJsonObj
119+ } ) ) ;
120+ process . exit ( 1 ) ;
121+ } ) ;
122+ }
96123
97124/**
98- * Is a Directory
125+ * Is this padh a Directory
99126 *
100127 * @param {String } filePath
101128 * @returns {Boolean }
102129 */
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 ;
130+ function isDirectory ( filePath ) {
131+ var isDir = false ;
132+ try {
133+ var absolutePath = path . resolve ( filePath ) ;
134+ isDir = fs . lstatSync ( absolutePath ) . isDirectory ( ) ;
135+ } catch ( e ) {
136+ isDir = e . code === 'ENOENT' ;
137+ }
138+ return isDir ;
112139}
113140
114141
@@ -118,12 +145,9 @@ function isDirectory(filePath){
118145 * @param {Object } Object to be formatted according to -p flag
119146 * @returns {JSON string }
120147 */
121- function formatOutput ( obj ) {
122- if ( argv . m ) {
148+ function formatOutput ( obj ) {
149+ if ( argv . m ) {
123150 return JSON . stringify ( obj ) ;
124151 }
125152 return JSON . stringify ( obj , null , 2 ) ;
126153}
127-
128-
129-
0 commit comments