diff --git a/src/commands/database.js b/src/commands/database.js index 53903b03..67f527e8 100644 --- a/src/commands/database.js +++ b/src/commands/database.js @@ -19,12 +19,9 @@ exports.handler = async function (args) { switch (command) { case 'db:create': - await sequelize.query(`CREATE DATABASE ${sequelize.queryInterface.quoteIdentifier(config.database)}`, { + await sequelize.query(`CREATE DATABASE ${sequelize.getQueryInterface().quoteIdentifier(config.database)}`, { type: sequelize.QueryTypes.RAW - }).catch(e => { - helpers.view.error(`Error: ${e.message}`); - process.exit(1); - }); + }).catch(e => helpers.view.error(e)); helpers.view.log( 'Database', @@ -34,12 +31,9 @@ exports.handler = async function (args) { break; case 'db:drop': - await sequelize.query(`DROP DATABASE ${sequelize.queryInterface.quoteIdentifier(config.database)}`, { + await sequelize.query(`DROP DATABASE ${sequelize.getQueryInterface().quoteIdentifier(config.database)}`, { type: sequelize.QueryTypes.RAW - }).catch(e => { - helpers.view.error(`Error: ${e.message}`); - process.exit(1); - }); + }).catch(e => helpers.view.error(e)); helpers.view.log( 'Database', @@ -59,8 +53,7 @@ function getDatabaseLessSequelize () { try { config = helpers.config.readConfig(); } catch (e) { - helpers.view.error(`Error: ${e.message}`); - process.exit(1); + helpers.view.error(e); } config = cloneDeep(config); @@ -82,13 +75,11 @@ function getDatabaseLessSequelize () { default: helpers.view.error(`Dialect ${config.dialect} does not support db:create / db:drop commands`); - process.exit(1); } try { return new Sequelize(config); } catch (e) { - helpers.view.error(`Error: ${e.message}`); - process.exit(1); + helpers.view.error(e); } } diff --git a/src/commands/init.js b/src/commands/init.js index f6ea7fb2..a4e6b919 100644 --- a/src/commands/init.js +++ b/src/commands/init.js @@ -44,9 +44,9 @@ exports.handler = async function (argv) { function initConfig (args) { if (!helpers.config.configFileExists() || !!args.force) { helpers.config.writeDefaultConfig(); - console.log('Created "' + helpers.config.relativeConfigFile() + '"'); + helpers.view.log('Created "' + helpers.config.relativeConfigFile() + '"'); } else { - helpers.init.notifyAboutExistingFile(helpers.config.relativeConfigFile()); + helpers.view.notifyAboutExistingFile(helpers.config.relativeConfigFile()); process.exit(1); } } diff --git a/src/commands/migrate.js b/src/commands/migrate.js index 78764269..7f8c9dff 100644 --- a/src/commands/migrate.js +++ b/src/commands/migrate.js @@ -32,16 +32,12 @@ function migrate(args) { .then(() => migrator.pending()) .then(migrations => { if (migrations.length === 0) { - console.log('No migrations were executed, database schema was already up to date.'); + helpers.view.log('No migrations were executed, database schema was already up to date.'); process.exit(0); } }) - .then(() => migrator.up()) - .catch(err => { - console.error(err); - process.exit(1); - }); - }); + .then(() => migrator.up()); + }).catch(e => helpers.view.error(e)); } function migrationStatus(args) { @@ -50,18 +46,15 @@ function migrationStatus(args) { .then(() => migrator.executed()) .then(migrations => { _.forEach(migrations, migration => { - console.log('up', migration.file); + helpers.view.log('up', migration.file); }); }).then(() => migrator.pending()) .then(migrations => { _.forEach(migrations, migration => { - console.log('down', migration.file); + helpers.view.log('down', migration.file); }); - }).catch(err => { - console.error(err); - process.exit(1); }); - }); + }).catch(e => helpers.view.error(e)); } function migrateSchemaTimestampAdd(args) { @@ -69,14 +62,10 @@ function migrateSchemaTimestampAdd(args) { return addTimestampsToSchema(migrator) .then(items => { if (items) { - console.log('Successfully added timestamps to MetaTable.'); + helpers.view.log('Successfully added timestamps to MetaTable.'); } else { - console.log('MetaTable already has timestamps.'); + helpers.view.log('MetaTable already has timestamps.'); } - }) - .catch(err => { - console.error(err); - process.exit(1); }); - }); + }).catch(e => helpers.view.error(e)); } diff --git a/src/commands/migrate_undo.js b/src/commands/migrate_undo.js index 00c86c9d..32537f77 100644 --- a/src/commands/migrate_undo.js +++ b/src/commands/migrate_undo.js @@ -27,7 +27,7 @@ function migrateUndo (args) { return ensureCurrentMetaSchema(migrator).then(() => migrator.executed()) .then(migrations => { if (migrations.length === 0) { - console.log('No executed migrations found.'); + helpers.view.log('No executed migrations found.'); process.exit(0); } }) @@ -37,10 +37,6 @@ function migrateUndo (args) { } else { return migrator.down(); } - }) - .catch(err => { - console.error(err); - process.exit(1); }); - }); + }).catch(e => helpers.view.error(e)); } \ No newline at end of file diff --git a/src/commands/migrate_undo_all.js b/src/commands/migrate_undo_all.js index 37198f03..53366fd2 100644 --- a/src/commands/migrate_undo_all.js +++ b/src/commands/migrate_undo_all.js @@ -28,14 +28,10 @@ function migrationUndoAll (args) { return ensureCurrentMetaSchema(migrator).then(() => migrator.executed()) .then(migrations => { if (migrations.length === 0) { - console.log('No executed migrations found.'); + helpers.view.log('No executed migrations found.'); process.exit(0); } }) - .then(() => migrator.down({ to: args.to || 0 })) - .catch(err => { - console.error(err); - process.exit(1); - }); - }); + .then(() => migrator.down({ to: args.to || 0 })); + }).catch(e => helpers.view.error(e)); } \ No newline at end of file diff --git a/src/commands/model_generate.js b/src/commands/model_generate.js index 109f1eda..0cfa866a 100644 --- a/src/commands/model_generate.js +++ b/src/commands/model_generate.js @@ -49,7 +49,6 @@ function ensureModelsFolder () { helpers.path.getModelsPath() + '). Did you run ' + clc.blueBright('sequelize init') + '?' ); - process.exit(1); } } @@ -60,7 +59,6 @@ function ensureMigrationsFolder () { helpers.path.getPath('migration') + '). Did you run ' + clc.blueBright('sequelize init') + '?' ); - process.exit(1); } } @@ -68,7 +66,7 @@ function checkModelFileExistence (args) { const modelPath = helpers.path.getModelPath(args.name); if (!args.force && helpers.model.modelFileExists(modelPath)) { - helpers.model.notifyAboutExistingFile(modelPath); + helpers.view.notifyAboutExistingFile(modelPath); process.exit(1); } } \ No newline at end of file diff --git a/src/commands/seed.js b/src/commands/seed.js index 1c9c8ba4..d37f98e0 100644 --- a/src/commands/seed.js +++ b/src/commands/seed.js @@ -29,17 +29,13 @@ function seedAll (args) { return migrator.pending() .then(seeders => { if (seeders.length === 0) { - console.log('No seeders found.'); + helpers.view.log('No seeders found.'); return; } return migrator.up({ migrations: _.chain(seeders).map('file').value() }); - }) - .catch(err => { - console.error('Seed file failed with error:', err.message, err.stack); - process.exit(1); }); - }); + }).catch(e => helpers.view.error(e)); } function seedUndoAll (args) { @@ -49,15 +45,11 @@ function seedUndoAll (args) { ) .then(seeders => { if (seeders.length === 0) { - console.log('No seeders found.'); + helpers.view.log('No seeders found.'); return; } return migrator.down({ migrations: _.chain(seeders).map('file').reverse().value() }); - }) - .catch(err => { - console.error('Seed file failed with error:', err.message, err.stack); - process.exit(1); }); - }); + }).catch(e => helpers.view.error(e)); } diff --git a/src/commands/seed_one.js b/src/commands/seed_one.js index 90142dc1..9a22da79 100644 --- a/src/commands/seed_one.js +++ b/src/commands/seed_one.js @@ -30,22 +30,14 @@ exports.handler = async function (args) { switch (command) { case 'db:seed': await getMigrator('seeder', args).then(migrator => { - return migrator.up(seeds) - .catch(err => { - console.error('Seed file failed with error:', err.message, err.stack); - process.exit(1); - }); - }); + return migrator.up(seeds); + }).catch(e => helpers.view.error(e)); break; case 'db:seed:undo': await getMigrator('seeder', args).then(migrator => { - return migrator.down({ migrations: seeds }) - .catch(err => { - console.error('Seed file failed with error:', err.message, err.stack); - process.exit(1); - }); - }); + return migrator.down({ migrations: seeds }); + }).catch(e => helpers.view.error(e)); break; } diff --git a/src/core/migrator.js b/src/core/migrator.js index 578d724a..c8f2803d 100644 --- a/src/core/migrator.js +++ b/src/core/migrator.js @@ -1,8 +1,9 @@ -import helpers from '../helpers/index'; import Umzug from 'umzug'; import Bluebird from 'bluebird'; import _ from 'lodash'; +import helpers from '../helpers/index'; + const Sequelize = helpers.generic.getSequelize(); export function logMigrator (s) { @@ -17,8 +18,7 @@ function getSequelizeInstance () { try { config = helpers.config.readConfig(); } catch (e) { - console.log(e.message); - process.exit(1); + helpers.view.error(e); } config = _.defaults(config, { logging: logMigrator }); @@ -26,15 +26,14 @@ function getSequelizeInstance () { try { return new Sequelize(config); } catch (e) { - console.warn(e); - throw e; + helpers.view.error(e); } } export function getMigrator (type, args) { return Bluebird.try(() => { if (!(helpers.config.configFileExists() || args.url)) { - console.log( + helpers.view.error( 'Cannot find "' + helpers.config.getConfigFile() + '". Have you run "sequelize init"?' ); @@ -45,7 +44,7 @@ export function getMigrator (type, args) { const migrator = new Umzug({ storage: helpers.umzug.getStorage(type), storageOptions: helpers.umzug.getStorageOptions(type, { sequelize }), - logging: console.log, + logging: helpers.view.log, migrations: { params: [sequelize.getQueryInterface(), Sequelize], path: helpers.path.getPath(type), @@ -63,10 +62,7 @@ export function getMigrator (type, args) { return sequelize .authenticate() .then(() => migrator) - .catch(err => { - console.error('Unable to connect to database: ' + err); - process.exit(1); - }); + .catch(e => helpers.view.error(e)); }); } diff --git a/src/core/yargs.js b/src/core/yargs.js index 2bdf2181..a09180c2 100644 --- a/src/core/yargs.js +++ b/src/core/yargs.js @@ -50,6 +50,11 @@ export function _baseOptions (yargs) { .option('url', { describe: 'The database connection string to use. Alternative to using --config files', type: 'string' + }) + .option('debug', { + describe: 'When available show various debug information', + default: false, + type: 'boolean' }); } diff --git a/src/helpers/config-helper.js b/src/helpers/config-helper.js index 94a2d94e..fe21286f 100644 --- a/src/helpers/config-helper.js +++ b/src/helpers/config-helper.js @@ -120,13 +120,13 @@ const api = { } if (args.url) { - console.log('Parsed url ' + api.filteredUrl(args.url, api.rawConfig)); + helpers.view.log('Parsed url ' + api.filteredUrl(args.url, api.rawConfig)); } else { - console.log('Loaded configuration file "' + api.relativeConfigFile() + '".'); + helpers.view.log('Loaded configuration file "' + api.relativeConfigFile() + '".'); } if (api.rawConfig[env]) { - console.log('Using environment "' + env + '".'); + helpers.view.log('Using environment "' + env + '".'); api.rawConfig = api.rawConfig[env]; } diff --git a/src/helpers/init-helper.js b/src/helpers/init-helper.js index 91b0084c..0bdbeb58 100644 --- a/src/helpers/init-helper.js +++ b/src/helpers/init-helper.js @@ -1,44 +1,36 @@ import helpers from './index'; import path from 'path'; import fs from 'fs'; -import clc from 'cli-color'; function createFolder (folderName, folder, force) { if (force) { - console.log('Deleting the ' + folderName + ' folder. (--force)'); + helpers.view.log('Deleting the ' + folderName + ' folder. (--force)'); try { fs.readdirSync(folder).forEach(filename => { fs.unlinkSync(path.resolve(folder, filename)); }); } catch (e) { - console.log(e); + helpers.view.error(e); } try { fs.rmdirSync(folder); - console.log('Successfully deleted the ' + folderName + ' folder.'); + helpers.view.log('Successfully deleted the ' + folderName + ' folder.'); } catch (e) { - console.log(e); + helpers.view.error(e); } } try { helpers.asset.mkdirp(folder); - console.log('Successfully created ' + folderName + ' folder at "' + folder + '".'); + helpers.view.log('Successfully created ' + folderName + ' folder at "' + folder + '".'); } catch (e) { - console.log(e); + helpers.view.error(e); } }; const init = { - notifyAboutExistingFile: file => { - helpers.view.log( - 'The file ' + clc.blueBright(file) + ' already exists. Run ' + - '"sequelize init --force" to overwrite it.' - ); - }, - createMigrationsFolder: force => { createFolder('migrations', helpers.path.getPath('migration'), force); }, @@ -61,7 +53,7 @@ const init = { if (!helpers.path.existsSync(modelsPath)) { helpers.view.log('Models folder not available.'); } else if (helpers.path.existsSync(indexPath) && !force) { - this.notifyAboutExistingFile(indexPath); + helpers.view.notifyAboutExistingFile(indexPath); } else { const relativeConfigPath = path.relative( helpers.path.getModelsPath(), diff --git a/src/helpers/model-helper.js b/src/helpers/model-helper.js index 287aff4c..3539ad0c 100644 --- a/src/helpers/model-helper.js +++ b/src/helpers/model-helper.js @@ -1,14 +1,6 @@ -import clc from 'cli-color'; import helpers from './index'; module.exports = { - notifyAboutExistingFile (file) { - helpers.view.error( - 'The file ' + clc.blueBright(file) + ' already exists. ' + - 'Run "sequelize model:create --force" to overwrite it.' - ); - }, - transformAttributes (flag) { /* possible flag formats: diff --git a/src/helpers/view-helper.js b/src/helpers/view-helper.js index af946a30..16e09670 100644 --- a/src/helpers/view-helper.js +++ b/src/helpers/view-helper.js @@ -1,6 +1,9 @@ import clc from 'cli-color'; import _ from 'lodash'; import helpers from './index'; +import getYArgs from '../core/yargs'; + +const args = getYArgs().argv; module.exports = { teaser () { @@ -16,17 +19,45 @@ module.exports = { // Remove in v4 if (helpers.version.getOrmVersion().match(/^4./)) { - this.log(clc.yellow( - 'WARNING: This version of Sequelize CLI is not ' + + this.warn( + 'This version of Sequelize CLI is not ' + 'fully compatible with Sequelize v4. ' + 'https://github.com/sequelize/cli#sequelize-support' - )); + ); this.log(); } }, - log: console.log, - error: console.error, + log () { + console.log.apply(this, arguments); + }, + + error (error) { + let message = error; + + if (error instanceof Error) { + message = !args.debug + ? error.message + : error.stack; + } + + this.log(); + console.error(`${clc.red('ERROR:')} ${message}`); + this.log(); + + process.exit(1); + }, + + warn (message) { + this.log(`${clc.yellow('WARNING:')} ${message}`); + }, + + notifyAboutExistingFile (file) { + this.error( + 'The file ' + clc.blueBright(file) + ' already exists. ' + + 'Run command with --force to overwrite it.' + ); + }, pad (s, smth) { let margin = smth;