Skip to content

Instantly share code, notes, and snippets.

@FotoVerite
Created November 11, 2014 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FotoVerite/1193d7d6cb34bb0b22ec to your computer and use it in GitHub Desktop.
Save FotoVerite/1193d7d6cb34bb0b22ec to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
'use strict';
var realist = require('realist'),
fs = require('fs'),
colors = require('colors'),
read = require('fs').readFileSync,
join = require('path').join,
_ = require('lodash'),
async = require('async'),
glob = require("glob"),
ANNOTATE = require('../index');
var USAGE = read(join(__dirname, '/../usage.txt')).toString();
var options = {
sql: ['s', 'sql'],
directory: ['d', 'directory']
};
function annotate(opts, path, tableName, database) {
var dataBaseConf = database || process.env.DATABASE_URL;
if (!dataBaseConf) {
console.log(colors.red('database url not set.\nPlease set environment DATABASE_URL or give as argument.'));
process.exit();
}
if (opts.directory) {
processDirectory(opts, path, tableName, dataBaseConf);
} else {
processFile(opts, path, tableName, dataBaseConf);
}
}
function processDirectory(opts, path, tableName, dataBaseConf) {
if (!path) {
return usage();
}
fs.exists(path, function(exists) {
if (!exists) {
console.log(colors.red('Directory ' + path + ' not found'));
process.exit();
} else {
var jsAndSqlFilePaths = glob.sync(path+ '+(*.sql|*.js)');
console.log(jsAndSqlFilePaths);
_.each(jsAndSqlFilePaths, function(filePath) {
processFile(opts, filePath, tableName, dataBaseConf);
});
}
});
}
function processFile(opts, path, tableName, dataBaseConf) {
console.log(path)
if (!path) {
return usage();
}
fs.exists(path, function(exists) {
if (!exists) {
console.log(colors.red('File ' + path + ' not found'));
process.exit();
} else {
ANNOTATE(opts, path, tableName, dataBaseConf);
}
});
}
function usage() {
console.log(USAGE);
process.exit();
}
function version(app) {
console.log(require('../package.json').version);
app.stop();
}
var events = {
'option help': usage,
'option version': version
};
realist(annotate, options, events);
process.on('uncaughtException', function(error) {
console.log('Uncaught exception!'.red);
console.log(error && error.stack || error);
process.exit();
});
// seperate file
'use strict';
var fs = require('fs'),
colors = require('colors'),
path = require('path'),
join = require('path').join,
pf = require('./lib/pf'),
pluralize = require('pluralize'),
pg = require('pg.js'),
utils = require('./lib/utils');
function queryPG(database, tableName, cb) {
pg.connect(database, function(err, client, done) {
if (err) {
return console.error('error fetching client from pool', err);
}
client.query('SELECT column_name, data_type, column_default FROM information_schema.columns WHERE table_name = $1', [tableName], function(err, result) {
//call `done()` to release the client back to the pool
cb(result.rows);
done();
if (err) {
return console.error('error running query', err);
}
});
});
}
function annotate(opts, filePath, tableName, dataBaseConf, cb) {
var ext = path.extname(filePath);
var commentToken = ext === '.sql' ? '-- ' : '//';
var baseFileName = path.basename(filePath, ext);
if(! tableName) {
tableName = pluralize(utils.sanitizeFileName(utils.toDash(baseFileName)));
}
queryPG(dataBaseConf, tableName, function(columns) {
var annotation = utils.createPrependString(commentToken, columns);
pf(filePath, annotation, function(done) {
if (done) {
console.log(colors.green('Data prepended to ' + filePath));
cb('result');
} else {
console.log(colors.red('ERROR Processed stopped abruptly'));
cb('error')
process.exit();
}
});
});
}
module.exports = annotate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment