2019-05-21 19:41:50 -05:00
|
|
|
const { DEFAULT_EXT, DEFAULT_TABLE_NAME } = require('./constants');
|
2019-06-04 00:37:17 +02:00
|
|
|
const { resolveClientNameWithAliases } = require('../../src/helpers');
|
2018-12-05 08:30:55 +01:00
|
|
|
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,
|
2019-05-21 19:41:50 -05:00
|
|
|
tableName: opts.migrationsTableName || DEFAULT_TABLE_NAME,
|
2018-12-05 08:30:55 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function tryLoadingDefaultConfiguration() {
|
2019-06-11 02:05:31 +02:00
|
|
|
const jsPath = resolveDefaultKnexfilePath('js');
|
|
|
|
if (fs.existsSync(jsPath)) {
|
|
|
|
return require(jsPath);
|
2018-12-05 08:30:55 +01:00
|
|
|
}
|
2019-06-11 02:05:31 +02:00
|
|
|
|
|
|
|
const tsPath = resolveDefaultKnexfilePath('ts');
|
|
|
|
if (fs.existsSync(tsPath)) {
|
|
|
|
return require(tsPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.warn(
|
|
|
|
`Failed to find configuration at default location of ${resolveDefaultKnexfilePath(
|
|
|
|
'js'
|
|
|
|
)}`
|
|
|
|
);
|
2018-12-05 08:30:55 +01:00
|
|
|
}
|
|
|
|
|
2019-06-11 02:05:31 +02:00
|
|
|
function resolveDefaultKnexfilePath(extension) {
|
|
|
|
return process.cwd() + `/knexfile.${extension}`;
|
2018-12-05 08:30:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
mkConfigObj,
|
|
|
|
tryLoadingDefaultConfiguration,
|
|
|
|
};
|