Skip to content

Commit 1d2ee9e

Browse files
committed
feat(plugin): add basic plugin support
Added basic plugin support, enabling to inject at multiple targets of the API, config and register pre compiler. Resolves #397 Refers #396
1 parent 688b89f commit 1d2ee9e

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

api.js

+70-6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ function registerPluginLoader(plugins) {
5858
};
5959
}
6060

61+
var APIHooks = {
62+
'init:api:addfunction:hook': function(name, fn) {
63+
64+
this[name] = fn;
65+
return;
66+
},
67+
'init:api:accessapi:hook': function(cb) {
68+
69+
return cb(this);
70+
}
71+
};
72+
6173
function dbmigrate(plugins, isModule, options, callback) {
6274

6375
this.internals = {
@@ -153,6 +165,31 @@ dbmigrate.prototype = {
153165
return true;
154166
},
155167

168+
registerAPIHook: function(callback) {
169+
170+
var plugins = this.internals.plugins;
171+
var self = this;
172+
173+
return Promise.resolve(Object.keys(APIHooks))
174+
.each(function(hook) {
175+
176+
var plugin = plugins.hook(hook);
177+
if(!plugin) return;
178+
179+
var APIHook = APIHooks[hook].bind(self);
180+
181+
return Promise.resolve(plugin)
182+
.map(function(plugin) {
183+
184+
return plugin[hook]();
185+
})
186+
.each(function(args) {
187+
188+
return APIHook.apply(self, args);
189+
});
190+
}).asCallback(callback);
191+
},
192+
156193
_internals: this.internals,
157194

158195
/**
@@ -556,9 +593,24 @@ function setDefaultArgv(internals, isModule) {
556593
.boolean('ignore-completed-migrations')
557594

558595
.describe('log-level', 'Set the log-level, for example sql|warn')
559-
.string('log-level')
596+
.string('log-level');
597+
598+
599+
var plugins = internals.plugins;
600+
var plugin = plugins.hook('init:cli:config:hook');
601+
if(plugin) {
560602

561-
.argv;
603+
plugin.forEach(function(plugin) {
604+
605+
var configs = plugin['init:cli:config:hook']();
606+
if(!configs) return;
607+
608+
//hook not yet used, we look into migrating away from optimist first
609+
return;
610+
});
611+
}
612+
613+
internals.argv = internals.argv.argv;
562614

563615
if (internals.argv.version) {
564616
console.log(internals.dbm.version);
@@ -592,7 +644,7 @@ function setDefaultArgv(internals, isModule) {
592644
}
593645

594646
function createMigrationDir(dir, callback) {
595-
fs.stat(dir, function(err, stat) {
647+
fs.stat(dir, function(err) {
596648
if (err) {
597649
mkdirp(dir, callback);
598650
} else {
@@ -1101,9 +1153,21 @@ function run(internals, config) {
11011153
break;
11021154

11031155
default:
1104-
log.error('Invalid Action: Must be [up|down|create|reset|seed|db].');
1105-
optimist.showHelp();
1106-
process.exit(1);
1156+
var plugins = internals.plugins;
1157+
var plugin = plugins.overwrite(
1158+
'run:default:action:' + action + ':overwrite'
1159+
);
1160+
if(plugin) {
1161+
1162+
plugin['run:default:action:' + action + ':overwrite']
1163+
(internals, config);
1164+
}
1165+
else {
1166+
1167+
log.error('Invalid Action: Must be [up|down|create|reset|seed|db].');
1168+
optimist.showHelp();
1169+
process.exit(1);
1170+
}
11071171
break;
11081172
}
11091173
}

bin/db-migrate

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,9 @@ resolve( 'db-migrate', {
3333
}
3434

3535
dbmigrate = DBMigrate.getInstance();
36-
dbmigrate.run();
36+
dbmigrate.registerAPIHook()
37+
.then(function() {
38+
39+
dbmigrate.run();
40+
});
3741
} );

0 commit comments

Comments
 (0)