Fix support for default knexfile in working dir (#2941)

* Fix support for default knexfile in working dir

* Bump version

* Remove incorrect comment
This commit is contained in:
Igor Savin 2018-12-05 08:30:55 +01:00 committed by Mikael Lepistö
parent 8bb0dc3191
commit ac6423f3b3
9 changed files with 155 additions and 58 deletions

View File

@ -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) {
@ -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) {
@ -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 {
@ -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'
);
}
}

View File

@ -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
bin/utils/constants.js Normal file
View File

@ -0,0 +1,5 @@
const DEFAULT_EXT = 'js';
module.exports = {
DEFAULT_EXT,
};

View File

@ -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",

View File

@ -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);
@ -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);
}
@ -49,5 +66,6 @@ function test(taskList, description, func) {
module.exports = {
assertExec,
assertExecError,
test,
};

View File

@ -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,

View File

@ -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,
};

View File

@ -1,32 +0,0 @@
#!/usr/bin/env jake
'use strict';
/* eslint-disable no-undef */
/* eslint-disable no-console */
const path = require('path');
const {
assertExec,
test,
} = require('../../jake-util/helpers/migrationtesthelper');
const KNEX = path.normalize(__dirname + '/../../../bin/cli.js');
const taskList = [];
/* * * TESTS * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
test(taskList, 'Run migrations with knexfile passed', (temp) => {
console.log(`process ${process.cwd()} dir ${__dirname}`);
return assertExec(
`node ${KNEX} migrate:latest --knexfile=test/jake-util/knexfile/knexfile.js --knexpath=../knex.js`
);
});
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,
};