Skip to content

Commit

Permalink
refactor: replace optimist with yargs
Browse files Browse the repository at this point in the history
Signed-off-by: Robinson Rodriguez <rfrodriguez1992@gmail.com>
  • Loading branch information
rfrm committed Apr 6, 2020
1 parent bb2f609 commit 18317cf
Show file tree
Hide file tree
Showing 8 changed files with 501 additions and 193 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ module.exports.getInstance = function (
callback
) {
delete require.cache[require.resolve('./api.js')];
delete require.cache[require.resolve('optimist')];
delete require.cache[require.resolve('yargs')];
var Mod = require('./api.js');
var plugins = {};

Expand Down
4 changes: 2 additions & 2 deletions lib/commands/create-migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const log = require('db-migrate-shared').log;
const mkdirp = Promise.promisify(require('mkdirp'));
const fs = require('fs');
const stat = Promise.promisify(fs.stat);
const optimist = require('optimist');
const yargs = require('yargs');
const util = require('util');

async function createMigrationDir (dir) {
Expand Down Expand Up @@ -33,7 +33,7 @@ async function executeCreateMigration (internals, config) {
if (internals.argv._.length === 0) {
log.error("'migrationName' is required.");
if (!internals.isModule) {
optimist.showHelp();
yargs.showHelp();
}

throw new Error("'migrationName' is required.");
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/run.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var log = require('db-migrate-shared').log;
var optimist = require('optimist');
var yargs = require('yargs');
var load = require('./');
var transition = load('transition');

Expand Down Expand Up @@ -117,7 +117,7 @@ function run (internals, config) {
'Invalid Action: Must be [up|down|check|create|reset|sync|' +
'db|transition].'
);
optimist.showHelp();
yargs.showHelp();
process.exit(1);
}
break;
Expand Down
22 changes: 9 additions & 13 deletions lib/commands/set-default-argv.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var optimist = require('optimist');
var yargs = require('yargs');
var log = require('db-migrate-shared').log;

module.exports = function (internals, isModule) {
Expand All @@ -20,7 +20,7 @@ module.exports = function (internals, isModule) {
};

if (!isModule) {
internals.argv = optimist
internals.argv = yargs
.default(defaultConfig)
.usage(
'Usage: db-migrate [up|down|check|reset|sync|create|db|transition] ' +
Expand Down Expand Up @@ -110,29 +110,25 @@ module.exports = function (internals, isModule) {
.describe('ignore-completed-migrations', 'Start at the first migration')
.boolean('ignore-completed-migrations')
.describe('log-level', 'Set the log-level, for example sql|warn')
.string('log-level');
.string('log-level')
.parse()
} else {
const _internalsArgv = Object.assign(defaultConfig, internals.cmdOptions);
internals.argv = {
get argv () {
return _internalsArgv;
}
};
internals.argv = Object.assign(defaultConfig, internals.cmdOptions);
}

var plugins = internals.plugins;
var plugin = plugins.hook('init:cli:config:hook');
var _config = internals.argv.argv.config;
var _config = internals.argv.config;

if (plugin) {
plugin.forEach(function (plugin) {
// var configs = plugin['init:cli:config:hook']();
// if (!configs) return;
// hook not yet used, we look into migrating away from optimist first
// hook not yet used, we look into migrating away from yargs first
});
}

internals.argv = deepExtend(internals.argv.argv, rc('db-migrate', {}));
internals.argv = deepExtend(internals.argv, rc('db-migrate', {}));
internals.argv.rcconfig = internals.argv.config;
internals.argv.config = internals.argv.configFile || _config;

Expand All @@ -142,7 +138,7 @@ module.exports = function (internals, isModule) {
}

if (!isModule && (internals.argv.help || internals.argv._.length === 0)) {
optimist.showHelp();
yargs.showHelp();
process.exit(1);
}

Expand Down
Loading

0 comments on commit 18317cf

Please sign in to comment.