Skip to content

Commit 8b5beac

Browse files
committed
fix(vuln): backport #679
Signed-off-by: Tobias Gurtzick <[email protected]>
1 parent e183046 commit 8b5beac

File tree

8 files changed

+315
-68
lines changed

8 files changed

+315
-68
lines changed

api.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ function dbmigrate (plugins, isModule, options, callback) {
6464
if (typeof options.cwd === 'string') internals.cwd = options.cwd;
6565
else internals.cwd = process.cwd();
6666

67-
if (typeof options.cmdOptions === 'object') internals.cmdOptions = options.cmdOptions;
67+
if (typeof options.cmdOptions === 'object') {
68+
internals.cmdOptions = options.cmdOptions;
69+
}
6870
} else internals.cwd = process.cwd();
6971

7072
if (typeof isModule === 'function') {
@@ -108,10 +110,10 @@ dbmigrate.prototype = {
108110
},
109111

110112
/**
111-
* Registers and initializes hooks.
112-
*
113-
* @returns Promise
114-
*/
113+
* Registers and initializes hooks.
114+
*
115+
* @returns Promise
116+
*/
115117
registerAPIHook: function (callback) {
116118
var plugins = this.internals.plugins;
117119
var self = this;
@@ -316,8 +318,8 @@ dbmigrate.prototype = {
316318
},
317319

318320
/**
319-
* Transition migrations to the latest defined protocol.
320-
*/
321+
* Transition migrations to the latest defined protocol.
322+
*/
321323
transition: function () {
322324
load('transition')(this.internals);
323325
},

generateLoader.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ const path = require('path');
1515
1616
const files = {
1717
${files
18-
.map(x => ` "${x.substring(0, x.indexOf('.js'))}": require('./${x}')`)
19-
.join(',\n')}
18+
.map(x => ` "${x.substring(0, x.indexOf('.js'))}": require('./${x}')`)
19+
.join(',\n')}
2020
}
2121
2222
function register (module) {

index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
require('pkginfo')(module, 'version'); // jshint ignore:line
1+
module.exports.version = require('./package.json').version;
2+
23
var fs = require('fs');
34
var path = require('path');
45
var log = require('db-migrate-shared').log;
@@ -65,7 +66,7 @@ function loadPlugins (options) {
6566

6667
module.exports.getInstance = function (isModule, options = {}, callback) {
6768
delete require.cache[require.resolve('./api.js')];
68-
delete require.cache[require.resolve('optimist')];
69+
delete require.cache[require.resolve('yargs')];
6970
var Mod = require('./api.js');
7071
var plugins = {};
7172
options.cwd = options.cwd || process.cwd();

lib/commands/create-migration.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var _assert = require('./helper/assert');
44
var log = require('db-migrate-shared').log;
55
var mkdirp = require('mkdirp');
66
var fs = require('fs');
7-
var optimist = require('optimist');
7+
var yargs = require('yargs');
88
var util = require('util');
99

1010
function createMigrationDir (dir, callback) {
@@ -32,7 +32,7 @@ function executeCreateMigration (internals, config, callback) {
3232
if (internals.argv._.length === 0) {
3333
log.error("'migrationName' is required.");
3434
if (!internals.isModule) {
35-
optimist.showHelp();
35+
yargs.showHelp();
3636
}
3737

3838
if (typeof callback !== 'function') {

lib/commands/run.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var transition;
77
function run (internals, config) {
88
const { load } = internals;
99
const transition = load('transition');
10-
console.log(load);
10+
console.log('hello');
1111
var action = internals.argv._.shift();
1212
var folder = action.split(':');
1313

lib/commands/set-default-argv.js

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var optimist = require('optimist');
1+
var yargs = require('yargs');
22
var log = require('db-migrate-shared').log;
33

44
module.exports = function (internals, isModule) {
@@ -19,7 +19,7 @@ module.exports = function (internals, isModule) {
1919
};
2020

2121
if (!isModule) {
22-
internals.argv = optimist
22+
internals.argv = yargs
2323
.default(defaultConfig)
2424
.usage(
2525
'Usage: db-migrate [up|down|check|reset|sync|create|db|transition] ' +
@@ -42,7 +42,10 @@ module.exports = function (internals, isModule) {
4242
.string('c')
4343
.describe('dry-run', "Prints the SQL but doesn't run it.")
4444
.boolean('dry-run')
45-
.describe('check', 'Prints the migrations to be run without running them.')
45+
.describe(
46+
'check',
47+
'Prints the migrations to be run without running them.'
48+
)
4649
.boolean('check')
4750
.describe(
4851
'force-exit',
@@ -98,39 +101,36 @@ module.exports = function (internals, isModule) {
98101
.describe('ignore-completed-migrations', 'Start at the first migration')
99102
.boolean('ignore-completed-migrations')
100103
.describe('log-level', 'Set the log-level, for example sql|warn')
101-
.string('log-level');
104+
.string('log-level')
105+
.parse();
102106
} else {
103-
const _internalsArgv = Object.assign(defaultConfig, internals.cmdOptions);
104-
internals.argv = {
105-
get argv () {
106-
return _internalsArgv;
107-
}
108-
};
107+
internals.argv = Object.assign(defaultConfig, internals.cmdOptions);
109108
}
110109

111110
var plugins = internals.plugins;
112111
var plugin = plugins.hook('init:cli:config:hook');
113-
var _config = internals.argv.argv.config;
112+
var _config = internals.argv.config;
114113

115114
if (plugin) {
116115
plugin.forEach(function (plugin) {
117116
// var configs = plugin['init:cli:config:hook']();
118117
// if (!configs) return;
119-
// hook not yet used, we look into migrating away from optimist first
118+
// hook not yet used, we look into migrating away from yargs first
120119
});
121120
}
122121

123-
internals.argv = deepExtend(internals.argv.argv, rc('db-migrate', {}));
122+
internals.argv = deepExtend(internals.argv, rc('db-migrate', {}));
124123
internals.argv.rcconfig = internals.argv.config;
125124
internals.argv.config = internals.argv.configFile || _config;
126125

127126
if (internals.argv.version) {
127+
console.log(internals.argv.version);
128128
console.log(internals.dbm.version);
129129
process.exit(0);
130130
}
131131

132132
if (!isModule && (internals.argv.help || internals.argv._.length === 0)) {
133-
optimist.showHelp();
133+
yargs.showHelp();
134134
process.exit(1);
135135
}
136136

@@ -147,7 +147,7 @@ module.exports = function (internals, isModule) {
147147
internals.notransactions = internals.argv['non-transactional'];
148148
internals.dryRun = internals.argv['dry-run'];
149149
global.dryRun = internals.dryRun;
150-
internals.check = internals.argv['check'];
150+
internals.check = internals.argv.check;
151151

152152
if (internals.dryRun) {
153153
log.info('dry run');

lib/config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ exports.loadObject = function (_config, currentEnv) {
176176

177177
if (currentEnv) {
178178
out.setCurrent(currentEnv);
179-
} else if (config['default']) {
180-
out.setCurrent(config['default']);
179+
} else if (config.default) {
180+
out.setCurrent(config.default);
181181
} else if (config.defaultEnv) {
182182
if (config.defaultEnv.ENV) {
183183
out.setCurrent(process.env[config.defaultEnv.ENV]);

0 commit comments

Comments
 (0)