Skip to content

Commit

Permalink
Genero txt-to-sql-defaults.yaml con defyaml.js
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoefe committed Oct 17, 2016
1 parent 0225a7d commit 566db54
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 289 deletions.
16 changes: 14 additions & 2 deletions bin/txt-to-sql-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var Promises = require('best-promise');
var fs = require('fs-promise');
var fsSync = require('fs');
var Path = require('path');
var miniTools = require('mini-tools');
var jsYaml = require('js-yaml');
var changing = require('best-globals').changing;
var readline = require('readline');
Expand Down Expand Up @@ -49,6 +50,17 @@ cmdParams.exportDefaults = program.exportDefaults;
// numero de lineas a leer para analizar entrada
var bufferingThreeshold = 50;

function readConfigData(configFile) {
return Promises.start(function() {
return fs.exists(configFile);
}).then(function(exists) {
if(exists) {
return miniTools.readConfig([configFile]);
}
return {invalid:true};
});
};

function createParams(params, preparedParams) {
var res = {
tableName:params.tableName,
Expand Down Expand Up @@ -219,10 +231,10 @@ getOutputDir(cmdParams.input).then(function(dir) {
var inputBase = Path.resolve(dir, inputName);
var inputYaml = inputBase+'.yaml';
var createInputYaml = false;
return txtToSql.readConfigData(inputYaml).then(function(data) {
return readConfigData(inputYaml).then(function(data) {
if(data.invalid) {
createInputYaml = true;
return txtToSql.readConfigData(inputBase+'.json');
return readConfigData(inputBase+'.json');
}
return data;
}).then(function(data) {
Expand Down
22 changes: 11 additions & 11 deletions lib/txt-to-sql-defaults.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
columnNamesFormat: lowercased_names
separator: false
includePrimaryKey: true
columnAlignedCommas: false
columnAlignedMaxWidth: 100
outputEngine: postgresql
verboseErrors: false
inputEncoding: false
outputEncoding: false
addDropTable: false
ignoreNullLines: false
columnNamesFormat: lowercased_names
separator: false
includePrimaryKey: true
columnAlignedCommas: false
columnAlignedMaxWidth: 100
outputEngine: postgresql
verboseErrors: false
inputEncoding: false
outputEncoding: false
addDropTable: false
ignoreNullLines: false
31 changes: 3 additions & 28 deletions lib/txt-to-sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ var txtToSql = {};

var changing = require('best-globals').changing;
var iconv = require('iconv-lite');
var miniTools = require('mini-tools');
var Promises = require('best-promise');
var fs = require('fs-promise');

var margin = ' ';
var separators=';,\t|';
Expand Down Expand Up @@ -37,16 +34,6 @@ if (typeof Object.assign != 'function') {
})();
}

function readConfigData(configFile) {
return Promises.start(function() {
return fs.exists(configFile);
}).then(function(exists) {
if(exists) {
return miniTools.readConfig([configFile]);
}
return {invalid:true};
});
};

function adaptPlain(x){
if(x===''){ return 'null'; }
Expand Down Expand Up @@ -75,10 +62,10 @@ function mapTypes(typeNames) {
return typeNames.map(function(type, index) { return Object.assign({typeName:type}, types[index]); });
}

function quoteBackTick(objectName) { return '`'+objectName.replace(/`/g,'``')+'`'; }
function quoteBackTick(objectName) { return '`'+objectName.replace(/`/g,'``')+'`'; };
// Solo hay que escapar ']' de acuerdo con: https://technet.microsoft.com/en-us/library/ms176027(v=sql.105).aspx
function quoteBracket(objectName) { return '['+objectName.replace(/]/g,']]')+']'; }
function quoteDouble(objectName) { return '"'+objectName.replace(/"/g,'""')+'"'; }
function quoteBracket(objectName) { return '['+objectName.replace(/]/g,']]')+']'; };
function quoteDouble(objectName) { return '"'+objectName.replace(/"/g,'""')+'"'; };

function dropTableIfExists(tableName) { return "drop table if exists "+tableName; }
function dropTable(tableName) { return "drop table "+tableName; }
Expand Down Expand Up @@ -121,7 +108,6 @@ function throwIfErrors(errors) {
}
}

/*
txtToSql.defaultOpts = {
columnNamesFormat: 'lowercased_names',
separator: false,
Expand All @@ -135,7 +121,6 @@ txtToSql.defaultOpts = {
addDropTable: false,
ignoreNullLines: false
};
*/

var letterTranslator = {
'à':'a', 'á':'a', 'â':'a', 'ã':'a', 'ä':'a', 'å':'a', 'À':'a', 'Á':'a', 'Â':'a', 'Ã':'a', 'Ä':'a', 'Å':'a',
Expand Down Expand Up @@ -165,14 +150,6 @@ function checkEncodingParam(encoding, inOrOut, errors) {
}
}

function readDefaults(info) {
return readConfigData('./lib/txt-to-sql-defaults.yaml').then(function(data) {
if(data.invalid) { throw new Error('default config file not found'); }
txtToSql.defaultOpts = data;
return info;
});
}

function verifyInputParams(info){
info.opts = changing(txtToSql.defaultOpts, info.opts || {});
var errors=[];
Expand Down Expand Up @@ -570,7 +547,6 @@ function processOutputBuffer(info) {

function setup(info) {
return Promise.resolve(info)
.then(readDefaults)
.then(verifyInputParams)
.then(processEncodingOptions)
.then(separateLines)
Expand Down Expand Up @@ -649,6 +625,5 @@ txtToSql.createAdaptedRows = createAdaptedRows;
txtToSql.createInsertInto = createInsertInto;
txtToSql.createInsertValues = createInsertValues;
txtToSql.generatePrepareResult = generatePrepareResult;
txtToSql.readConfigData = readConfigData;

module.exports = txtToSql;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
"start": "node example/server.js",
"defyaml": "node tools/defyaml.js && git add lib",
"web": "node tools/web.js && git add web",
"lint": "jshint .",
"validate": "npm ls"
Expand All @@ -56,6 +57,7 @@
"type": "web"
},
"pre-commit": [
"defyaml",
"web"
]
}
22 changes: 22 additions & 0 deletions tools/defyaml.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";

var txtToSql = require('../lib/txt-to-sql.js');
var jsYaml = require('js-yaml');
var Promises = require('best-promise');
require('fs-extra');
var fs = require('fs-promise');

var Path = require('path');

function createDefaultYaml() {
var defaultYaml = Path.resolve('./lib/txt-to-sql-defaults.yaml');
console.log("Generating '"+defaultYaml+"'...");
return fs.writeFile(defaultYaml, jsYaml.safeDump(txtToSql.defaultOpts), {encoding:'utf8'}).then(function() {
console.log("listo.")
}).catch(function(err) {
console.log("Error", err, err.stack);
process.exit(1);
});
}

createDefaultYaml();
8 changes: 5 additions & 3 deletions tools/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ function generateWeb() {
console.log("Generating web content...");
var desDir = './web';
return processDirectory('./src', desDir).then(function() {
return processDirectory('./lib', desDir);
return processDirectory('./lib', desDir, ['js']);
}).then(function() {
return fs.copy('./node_modules/best-globals/best-globals.js', desDir+'/best-globals.js');
// }).then(function() {
// return processDirectory('./node_modules/best-promise', desDir, ['js']);
//}).then(function() {
// return processDirectory('./node_modules/best-promise', desDir, ['js']);
}).then(function() {
return processDirectory('./node_modules/require-bro/lib', desDir);
}).then(function() {
Expand All @@ -98,6 +98,8 @@ function generateWeb() {
return bundlePromise(b);
}).then(function(bfbuf) {
return fs.writeFile(desDir+'/buffer.js', bfbuf);
// }).then(function() {
// return fs.copy('./node_modules/mini-tools/lib/mini-tools.js', desDir+'/mini-tools.js');
}).catch(function(err) {
console.log("Error", err, err.stack);
process.exit(1);
Expand Down
Loading

0 comments on commit 566db54

Please sign in to comment.