Skip to content

Commit

Permalink
Merge pull request #464 from vschoettke/more-jshint-restrictions
Browse files Browse the repository at this point in the history
Added more JSHint restrictions
  • Loading branch information
vschoettke committed Sep 3, 2014
2 parents 9777a1c + 4c43d69 commit eec5bdc
Show file tree
Hide file tree
Showing 13 changed files with 220 additions and 226 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"browser": false,
"latedef": false,
"latedef": true,
"eqeqeq" : true,
"newcap": false,
"node": true,
"strict": true,
Expand Down
10 changes: 5 additions & 5 deletions knex.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ function Knex() {
return Knex.initialize.apply(null, arguments);
}

// Require the main constructors necessary for a `Knex` instance,
// each of which are injected with the current instance, so they maintain
// the correct client reference & grammar.
var Raw = require('./lib/raw');

// Run a "raw" query, though we can't do anything with it other than put
// it in a query statement.
Knex.raw = function(sql, bindings) {
return new Raw(sql, bindings);
};

// Require the main constructors necessary for a `Knex` instance,
// each of which are injected with the current instance, so they maintain
// the correct client reference & grammar.
var Raw = require('./lib/raw');

// Doing it this way makes it easier to build for browserify.
var mysql = function() { return require('./lib/dialects/mysql'); };
var mysql2 = function() { return require('./lib/dialects/mysql2'); };
Expand Down
108 changes: 54 additions & 54 deletions lib/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,6 @@ var cliPkg = require('../../package');
var argv = require('minimist')(process.argv.slice(2));
var fs = Promise.promisifyAll(require('fs'));

var cli = new Liftoff({
name: 'knex',
extensions: interpret.jsVariants
});

cli.on('require', function(name) {
console.log('Requiring external module', chalk.magenta(name));
});

cli.on('requireFail', function(name) {
console.log(chalk.red('Failed to load external module'), chalk.magenta(name));
});

cli.launch({
cwd: argv.cwd,
configPath: argv.knexfile,
require: argv.require,
completion: argv.completion
}, invoke);

function exit(text) {
if (text instanceof Error) {
chalk.red(console.error(text.stack));
Expand All @@ -48,6 +28,44 @@ function success(text) {
process.exit(0);
}

function checkLocalModule(env) {
if (!env.modulePath) {
console.log(chalk.red('No local knex install found in:'), chalk.magenta(tildify(env.cwd)));
exit('Try running: npm install knex.');
}

// check for semver difference between cli and local installation
if (semver.gt(cliPkg.version, env.modulePackage.version)) {
console.log(chalk.blue('Running knex is: ', chalk.green(cliPkg.version)));
console.log(chalk.blue('Local knex (installed in knexfile dir) is', chalk.green(env.modulePackage.version)));
}
}

function initKnex(env) {

checkLocalModule(env);

if (!env.configPath) {
exit('No knexfile found in this directory. Specify a path with --knexfile');
}

if (process.cwd() !== env.cwd) {
process.chdir(env.cwd);
console.log('Working directory changed to', chalk.magenta(tildify(env.cwd)));
}

var environmentVar = commander.env || process.env.NODE_ENV || 'development';
var config = require(env.configPath)[environmentVar];

if (!config) {
console.log(chalk.red('Warning: unable to read knexfile config for environment ' + environmentVar));
process.exit(1);
}

var knex = require(env.modulePath);
return knex(config);
}

function invoke(env) {

var pending, filetypes = ['js', 'coffee'];
Expand Down Expand Up @@ -159,40 +177,22 @@ function invoke(env) {
});
}

function checkLocalModule(env) {
if (!env.modulePath) {
console.log(chalk.red('No local knex install found in:'), chalk.magenta(tildify(env.cwd)));
exit('Try running: npm install knex.');
}

// check for semver difference between cli and local installation
if (semver.gt(cliPkg.version, env.modulePackage.version)) {
console.log(chalk.blue('Running knex is: ', chalk.green(cliPkg.version)));
console.log(chalk.blue('Local knex (installed in knexfile dir) is', chalk.green(env.modulePackage.version)));
}
}

function initKnex(env) {

checkLocalModule(env);

if (!env.configPath) {
exit('No knexfile found in this directory. Specify a path with --knexfile');
}

if (process.cwd() !== env.cwd) {
process.chdir(env.cwd);
console.log('Working directory changed to', chalk.magenta(tildify(env.cwd)));
}
var cli = new Liftoff({
name: 'knex',
extensions: interpret.jsVariants
});

var environmentVar = commander.env || process.env.NODE_ENV || 'development';
var config = require(env.configPath)[environmentVar];
cli.on('require', function(name) {
console.log('Requiring external module', chalk.magenta(name));
});

if (!config) {
console.log(chalk.red('Warning: unable to read knexfile config for environment ' + environmentVar));
process.exit(1);
}
cli.on('requireFail', function(name) {
console.log(chalk.red('Failed to load external module'), chalk.magenta(name));
});

var knex = require(env.modulePath);
return knex(config);
}
cli.launch({
cwd: argv.cwd,
configPath: argv.knexfile,
require: argv.require,
completion: argv.completion
}, invoke);
4 changes: 2 additions & 2 deletions lib/dialects/mysql/schema/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ ColumnCompiler_MySQL.prototype.bit = function(length) {
ColumnCompiler_MySQL.prototype.defaultTo = function(value) {
/*jshint unused: false*/
var defaultVal = ColumnCompiler_MySQL.super_.prototype.defaultTo.apply(this, arguments);
if (this.type != 'blob' && this.type.indexOf('text') === -1) {
if (this.type !== 'blob' && this.type.indexOf('text') === -1) {
return defaultVal;
}
return '';
Expand All @@ -103,4 +103,4 @@ ColumnCompiler_MySQL.prototype.comment = function(comment) {
client.ColumnBuilder = ColumnBuilder_MySQL;
client.ColumnCompiler = ColumnCompiler_MySQL;

};
};
3 changes: 1 addition & 2 deletions lib/dialects/mysql/schema/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ TableCompiler_MySQL.prototype.createQuery = function(columns, ifNot) {
if (collation) sql += ' collate ' + collation;
if (engine) sql += ' engine = ' + engine;

var hasComment = this.single.comment != void 0;
if (hasComment) {
if (this.single.comment) {
var comment = (this.single.comment || '');
if (comment.length > 60) helpers.warn('The max length for a table comment is 60 characters');
sql += " comment = '" + comment + "'";
Expand Down
42 changes: 21 additions & 21 deletions lib/dialects/mysql/string.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
'use strict';

function zeroPad(number, length) {
number = number.toString();
while (number.length < length) {
number = '0' + number;
}

return number;
}

function convertTimezone(tz) {
if (tz === "Z") return 0;

var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/);
if (m) {
return (m[1] === '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60;
}
return false;
}

var SqlString = exports;

SqlString.escapeId = function (val, forbidQualified) {
Expand Down Expand Up @@ -74,7 +93,7 @@ SqlString.format = function(sql, values, stringifyObjects, timeZone) {
return match;
}

if (match == "??") {
if (match === "??") {
return SqlString.escapeId(values.shift());
}
return SqlString.escape(values.shift(), stringifyObjects, timeZone);
Expand All @@ -84,7 +103,7 @@ SqlString.format = function(sql, values, stringifyObjects, timeZone) {
SqlString.dateToString = function(date, timeZone) {
var dt = new Date(date);

if (timeZone != 'local') {
if (timeZone !== 'local') {
var tz = convertTimezone(timeZone);

dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000));
Expand Down Expand Up @@ -132,22 +151,3 @@ SqlString.objectToValues = function(object, timeZone) {

return values.join(', ');
};

function zeroPad(number, length) {
number = number.toString();
while (number.length < length) {
number = '0' + number;
}

return number;
}

function convertTimezone(tz) {
if (tz == "Z") return 0;

var m = tz.match(/([\+\-\s])(\d\d):?(\d\d)?/);
if (m) {
return (m[1] == '-' ? -1 : 1) * (parseInt(m[2], 10) + ((m[3] ? parseInt(m[3], 10) : 0) / 60)) * 60;
}
return false;
}
6 changes: 3 additions & 3 deletions lib/dialects/mysql2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var _ = require('lodash');
var Client_MySQL = require('../mysql');
var Promise = require('../../promise');

var configOptions = ['user', 'database', 'host', 'password', 'ssl', 'connection', 'stream'];

var mysql2;

// Always initialize with the "QueryBuilder" and "QueryCompiler"
Expand Down Expand Up @@ -44,6 +46,4 @@ Client_MySQL2.prototype.acquireRawConnection = function() {
});
};

var configOptions = ['user', 'database', 'host', 'password', 'ssl', 'connection', 'stream'];

module.exports = Client_MySQL2;
module.exports = Client_MySQL2;
Loading

0 comments on commit eec5bdc

Please sign in to comment.