Skip to content

Commit

Permalink
Fix support for default knexfile in working dir (#2941)
Browse files Browse the repository at this point in the history
* Fix support for default knexfile in working dir

* Bump version

* Remove incorrect comment
  • Loading branch information
kibertoad authored and elhigu committed Dec 5, 2018
1 parent 8bb0dc3 commit ac6423f
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 58 deletions.
31 changes: 9 additions & 22 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ const commander = require('commander');
const argv = require('minimist')(process.argv.slice(2));
const fs = Promise.promisifyAll(require('fs'));
const cliPkg = require('../package');
const { resolveClientNameWithAliases } = require('../lib/helpers');
const DEFAULT_EXT = 'js';
const {
mkConfigObj,
tryLoadingDefaultConfiguration,
} = require('./utils/cli-config-utils');
const { DEFAULT_EXT } = require('./utils/constants');

function exit(text) {
if (text instanceof Error) {
Expand All @@ -33,27 +36,10 @@ function checkLocalModule(env) {
chalk.red('No local knex install found in:'),
chalk.magenta(tildify(env.cwd))
);
exit('Try running: npm install knex.');
exit('Try running: npm install knex');
}
}

function mkConfigObj(opts) {
const envName = opts.env || process.env.NODE_ENV || 'development';
const resolvedClientName = resolveClientNameWithAliases(opts.client);
const useNullAsDefault = resolvedClientName === 'sqlite3';
return {
ext: DEFAULT_EXT,
[envName]: {
useNullAsDefault,
client: opts.client,
connection: opts.connection,
migrations: {
directory: opts.migrationsDirectory,
},
},
};
}

function initKnex(env, opts) {
checkLocalModule(env);
if (process.cwd() !== env.cwd) {
Expand All @@ -65,7 +51,8 @@ function initKnex(env, opts) {
}

if (!opts.knexfile) {
env.configuration = mkConfigObj(opts);
const configuration = tryLoadingDefaultConfiguration();
env.configuration = configuration || mkConfigObj(opts);
}
// If knexfile is specified
else {
Expand All @@ -74,7 +61,7 @@ function initKnex(env, opts) {

if (!env.configuration) {
exit(
'No knexfile found in this directory. Specify a path with --knexfile or pass --client and --connection params in commandline'
'Knexfile not found. Specify a path with --knexfile or pass --client and --connection params in commandline'
);
}
}
Expand Down
43 changes: 43 additions & 0 deletions bin/utils/cli-config-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { DEFAULT_EXT } = require('./constants');
const { resolveClientNameWithAliases } = require('../../lib/helpers');
const fs = require('fs');

function mkConfigObj(opts) {
if (!opts.client) {
const path = resolveDefaultKnexfilePath();
throw new Error(
`No default configuration file '${path}' found and no commandline connection parameters passed`
);
}

const envName = opts.env || process.env.NODE_ENV || 'development';
const resolvedClientName = resolveClientNameWithAliases(opts.client);
const useNullAsDefault = resolvedClientName === 'sqlite3';
return {
ext: DEFAULT_EXT,
[envName]: {
useNullAsDefault,
client: opts.client,
connection: opts.connection,
migrations: {
directory: opts.migrationsDirectory,
},
},
};
}

function tryLoadingDefaultConfiguration() {
const path = resolveDefaultKnexfilePath();
if (fs.existsSync(path)) {
return require(path);
}
}

function resolveDefaultKnexfilePath() {
return process.cwd() + '/knexfile.js';
}

module.exports = {
mkConfigObj,
tryLoadingDefaultConfiguration,
};
5 changes: 5 additions & 0 deletions bin/utils/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const DEFAULT_EXT = 'js';

module.exports = {
DEFAULT_EXT,
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "knex",
"version": "0.16.1-next1",
"version": "0.16.1-next2",
"description": "A batteries-included SQL query & schema builder for Postgres, MySQL and SQLite3 and the Browser",
"main": "knex.js",
"types": "types/knex.d.ts",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ function assertExec(cmd, desc) {
});
}

function assertExecError(cmd, desc) {
desc = desc || 'Run ' + cmd;
return new Promise((resolve, reject) => {
let stderr = '';
console.log(`Executing: ${cmd}`);
const bin = jake.createExec([cmd]);
bin.addListener('error', (msg, code) => {
resolve(stderr);
});
bin.addListener('cmdEnd', () => {
throw new Error('Error was expected, but none thrown');
});
bin.addListener('stderr', (data) => (stderr += data.toString()));
bin.run();
});
}

function test(taskList, description, func) {
const tmpDirPath = os.tmpdir() + '/knex-test-';
rimrafSync(tmpDirPath);
Expand All @@ -39,7 +56,7 @@ function test(taskList, description, func) {
})
.then(() => {
rimrafSync(tmpDirPath);
rimrafSync('test/jake/test.sqlite3');
rimrafSync(__dirname + '/../test.sqlite3');
if (itFails) {
process.exit(1);
}
Expand All @@ -49,5 +66,6 @@ function test(taskList, description, func) {

module.exports = {
assertExec,
assertExecError,
test,
};
4 changes: 2 additions & 2 deletions test/jake/Jakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/* eslint-disable no-undef */
/* eslint-disable no-console */

const knexfileTests = require('./jakelib/knexfile').taskList;
const migrateTests = require('./jakelib/migrate').taskList;
const knexfileTests = require('./jakelib/knexfile-test').taskList;
const migrateTests = require('./jakelib/migrate-test').taskList;

const tests = [
...knexfileTests,
Expand Down
76 changes: 76 additions & 0 deletions test/jake/jakelib/knexfile-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env jake
'use strict';
/* eslint-disable no-undef */
/* eslint-disable no-console */

const path = require('path');
const {
assertExec,
assertExecError,
test,
} = require('../../jake-util/helpers/migration-test-helper');
const { assert } = require('chai');
const fs = require('fs');
const rimraf = require('rimraf');

const KNEX = path.normalize(__dirname + '/../../../bin/cli.js');

const taskList = [];
/* * * TESTS * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

test(taskList, 'Run migrations with knexfile passed', (temp) => {
return assertExec(
`node ${KNEX} migrate:latest --knexfile=test/jake-util/knexfile/knexfile.js --knexpath=../knex.js`
);
});

test(taskList, 'Throws informative error when no knexfile is found', (temp) => {
return assertExecError(
`node ${KNEX} migrate:latest --knexpath=../knex.js`
).then((err) => {
assert.include(err, 'No default configuration file');
});
});

test(
taskList,
'Resolves default knexfile in working directory correctly',
(temp) => {
const path = process.cwd() + '/knexfile.js';
let error;
fs.writeFileSync(
path,
`
module.exports = {
client: 'sqlite3',
connection: {
filename: __dirname + '/test/jake-util/test.sqlite3',
},
migrations: {
directory: __dirname + '/test//jake-util/knexfile_migrations',
},
};
`
);
return assertExec(`node ${KNEX} migrate:latest --knexpath=../knex.js`)
.catch((err) => {
error = err;
})
.then(() => {
rimraf.sync(path);
if (error) {
throw error;
}
});
}
);

test(taskList, 'resolves knexfile correctly with cwd specified', (temp) => {
return assertExec(
`node ${KNEX} migrate:latest --cwd=test/jake-util/knexfile --knexfile=knexfile.js`
);
});

module.exports = {
taskList,
};
32 changes: 0 additions & 32 deletions test/jake/jakelib/knexfile.js

This file was deleted.

File renamed without changes.

0 comments on commit ac6423f

Please sign in to comment.