Skip to content

Commit

Permalink
Alacon: fixed how to read file. Parsing errors get send to output
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasrw committed Jul 27, 2015
1 parent 761234a commit b0aea93
Showing 1 changed file with 66 additions and 14 deletions.
80 changes: 66 additions & 14 deletions bin/alasql.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
#!/usr/bin/env node
//
// Alacon - Command line interface for Alasql
// Version: 0.2.0
// Version: 0.2.1
// Date: 27.07.2015
// (c) 2014-2015, Andrey Gershun & M. Rangel Wulff
//

var alasql = require('alasql');
var path = require('path');
var fs = require('fs');


/**
* Is a Directory
*
* @param {String} filePath
* @returns {Boolean}
*/

function isDirectory(filePath) {
var isDir = false;
try {
var absolutePath = path.resolve(filePath);
isDir = fs.lstatSync(absolutePath).isDirectory();
} catch (e) {
isDir = e.code === 'ENOENT';
}
return isDir;
}


if(process.argv.length <= 2) {
console.log('AlaSQL command-line utility (version '+alasql.version+') ');
console.log();
Expand All @@ -22,22 +43,39 @@ if(process.argv.length <= 2) {
console.log(' alasql \'select count(*) from txt()\' < city.txt');
console.log(' alasql \'select * into xlsx("city.xlsx") from txt("city.txt")\'');
console.log();
return;
process.exit(0);
}


var sql = process.argv[2];

var parami = 3;

if(sql === '-v' || sql === '--version' ) {
console.log(alasql.version); // Issue #373
process.exit(0);
}

if(sql === '-f' || sql === '--file' ) {
if (!fs.existsSync(sql)) {
console.log('File not found');
return;
} sql = fs.readFile(sql).toString();
if(process.argv.length<=3){
console.log('Error: filename missing');
process.exit(1);
}

var filePath = path.resolve(process.argv[3]);

if (!fs.existsSync(filePath)) {
console.log('Error: file not found');
process.exit(1);
}

if (isDirectory(filePath)) {
console.log('Error: file expected but directory found');
process.exit(1);
}

sql = fs.readFileSync('/Users/mrw/git/slet/file.sql', 'utf8').toString();
parami++;
} else if(sql === '-v' || sql === '--version' ) {
console.log(alasql.version); // Issue #373
return;
}

var params = [];
Expand All @@ -51,9 +89,23 @@ for(var i=parami;i<process.argv.length;i++) {
params.push(a);
}

alasql(sql,params,function(res){
if(!alasql.options.stdout){
console.log(res);
}
});




alasql
.promise(sql,params)
.then(function(res){
if(!alasql.options.stdout){
console.log(res);
}
process.exit(0);
}).catch(function(err){
console.log(err);
process.exit(1);
});





0 comments on commit b0aea93

Please sign in to comment.