forked from db-migrate/node-db-migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (67 loc) · 2.01 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
module.exports.version = require('./package.json').version;
var fs = require('fs');
var path = require('path');
var log = require('db-migrate-shared').log;
exports.dataType = require('db-migrate-shared').dataType;
function loadPluginList (options) {
try {
fs.accessSync(path.join(options.cwd, 'package.json'), fs.constants.R_OK);
} catch (err) {
throw new Error(
'There was no package.json found in the current working dir!',
options.cwd
);
}
try {
var plugins = JSON.parse(
fs.readFileSync(path.join(options.cwd, 'package.json'), 'utf-8')
);
} catch (err) {
throw new Error('Error parsing package.json', err);
}
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 (options) {
var plugins = loadPluginList(options);
var i = 0;
var length = plugins.length;
var hooks = {};
for (; i < length; ++i) {
var plugin = require(path.join(options.cwd, 'node_modules', plugins[i]));
if (!plugin.hooks || !plugin.loadPlugin) {
continue;
}
// name is now derived from package name
plugin.name = plugins[i];
plugin.hooks.forEach(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('yargs')];
var Mod = require('./api.js');
var plugins = {};
options.cwd = options.cwd || process.cwd();
try {
if (!options || !options.noPlugins) plugins = loadPlugins(options);
} catch (ex) {
log.verbose('No plugin could be loaded!');
log.verbose(ex);
}
if (options && options.plugins) {
plugins = Object.assign(plugins, options.plugins);
}
return new Mod(plugins, isModule, options, callback);
};