forked from db-migrate/node-db-migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (49 loc) · 1.47 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require('pkginfo')(module, 'version'); // jshint ignore:line
var fs = require('fs');
var path = require('path');
exports.dataType = require('db-migrate-shared').dataType;
function loadPluginList () {
var plugins = JSON.parse(
fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf-8')
);
var targets = [];
plugins = Object.assign(plugins.dependencies, plugins.devDependencies);
for (var plugin in plugins) {
if (plugin.startsWith('db-migrate-plugin')) targets.push(plugin);
}
return targets;
}
function loadPlugins () {
var plugins = loadPluginList();
var i = 0;
var length = plugins.length;
var hooks = {};
for (; i < length; ++i) {
var plugin = require(path.join(process.cwd(), 'node_modules', plugins[i]));
if (
typeof plugin.name !== 'string' ||
!plugin.hooks ||
!plugin.loadPlugin
) {
continue;
}
plugin.hooks.map(function (hook) {
hooks[hook] = hooks[hook] || [];
hooks[hook].push(plugin);
});
}
return hooks;
}
module.exports.getInstance = function (isModule, options, callback) {
delete require.cache[require.resolve('./api.js')];
delete require.cache[require.resolve('optimist')];
var Mod = require('./api.js');
var plugins = {};
try {
if (!options || !options.noPlugins) plugins = loadPlugins();
} catch (ex) {}
if (options && options.plugins) {
plugins = Object.assign(plugins, options.plugins);
}
return new Mod(plugins, isModule, options, callback);
};