Skip to content

Commit 0de35ee

Browse files
committed
feat(plugin): add template plugin hook
Signed-off-by: Tobias Gurtzick <[email protected]>
1 parent 29fb173 commit 0de35ee

File tree

5 files changed

+126
-7
lines changed

5 files changed

+126
-7
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function loadPlugins (options) {
5555
continue;
5656
}
5757

58-
plugin.hooks.map(function (hook) {
58+
plugin.hooks.forEach(function (hook) {
5959
hooks[hook] = hooks[hook] || [];
6060
hooks[hook].push(plugin);
6161
});

lib/commands/create-migration.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ async function executeCreateMigration (internals, config) {
8484
(shouldCreateCoffeeFile(internals, config) ? '.coffee' : '.js'),
8585
path,
8686
internals.runTimestamp,
87-
templateType
87+
templateType,
88+
internals.plugins
8889
);
8990

9091
await migration.write();
@@ -132,7 +133,8 @@ async function createSqlFiles (internals, config) {
132133
internals.argv.title + '-up.sql',
133134
sqlDir,
134135
internals.runTimestamp,
135-
templateTypeDefaultSQL
136+
templateTypeDefaultSQL,
137+
internals.plugins
136138
);
137139
await migrationUpSQL.write();
138140

@@ -144,7 +146,8 @@ async function createSqlFiles (internals, config) {
144146
internals.argv.title + '-down.sql',
145147
sqlDir,
146148
internals.runTimestamp,
147-
templateTypeDefaultSQL
149+
templateTypeDefaultSQL,
150+
internals.plugins
148151
);
149152
migrationDownSQL.write();
150153
log.info(

lib/template.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
'use strict';
22

33
const File = require('./file.js');
4-
const dbmUtil = require('db-migrate-shared').util;
4+
const { util: dbmUtil, log } = require('db-migrate-shared');
55

66
const Template = function () {
7+
this.plugins = arguments[4];
8+
delete arguments[4];
79
this.templateType = arguments[3];
810
// this is a tweak for node 4 this can be translated
911
// to ...arguments instead in upcoming node versions
@@ -257,6 +259,8 @@ Template.TemplateType = {
257259
};
258260

259261
Template.prototype.getTemplate = function () {
262+
let plugin = false;
263+
260264
switch (this.templateType) {
261265
case Template.TemplateType.DEFAULT_SQL:
262266
return this.defaultSqlTemplate();
@@ -269,9 +273,29 @@ Template.prototype.getTemplate = function () {
269273
case Template.TemplateType.SQL_FILE_LOADER_IGNORE_ON_INIT:
270274
return this.sqlFileLoaderIgnoreOnInitTemplate();
271275
case Template.TemplateType.DEFAULT_JS:
272-
/* falls through */
273-
default:
274276
return this.defaultJsTemplate();
277+
default:
278+
if (this.plugins) {
279+
plugin = this.plugins.overwrite(
280+
`template:overwrite:provider:${this.templateType}`
281+
);
282+
}
283+
284+
if (plugin !== false) {
285+
try {
286+
return plugin[`template:overwrite:provider:${this.templateType}`]({
287+
file: {
288+
name: this.file.name
289+
}
290+
});
291+
} catch (ex) {
292+
log.error('Plugin failure "' + plugin.name + '", arborting!');
293+
294+
throw ex;
295+
}
296+
} else {
297+
return this.defaultJsTemplate();
298+
}
275299
}
276300
};
277301

test/migration_test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const proxyquire = require('proxyquire').noPreserveCache();
66
const lab = (exports.lab = Lab.script());
77
const Migration = require('../lib/file.js');
88
const Template = require('../lib/template.js');
9+
const Plugins = require('./pluginHelper.js');
910

1011
const date = createDateForTest();
1112
const dateString = '20140220143050';
@@ -229,6 +230,84 @@ function getTemplate () {
229230
});
230231
});
231232
});
233+
234+
lab.experiment('when template plugin is set', function () {
235+
lab.experiment('as sql file loader', function () {
236+
const name = 'test';
237+
const plugins = Plugins.createSinglePlugin(
238+
`template:overwrite:provider:${name}`,
239+
opts => {
240+
return `test all variables`;
241+
}
242+
);
243+
const migration = new Template(fileName, dirName, date, name, plugins);
244+
245+
lab.test('should return sql file loader template', () => {
246+
const actual = migration.getTemplate();
247+
Code.expect(actual).to.equal(`test all variables`);
248+
});
249+
});
250+
251+
lab.experiment('as default sql', function () {
252+
const migration = new Template(
253+
fileName,
254+
dirName,
255+
date,
256+
Template.TemplateType.DEFAULT_SQL,
257+
internals
258+
);
259+
260+
lab.test('should return default sql template', () => {
261+
const actual = migration.getTemplate();
262+
Code.expect(actual).to.equal(migration.defaultSqlTemplate());
263+
});
264+
});
265+
266+
lab.experiment('as default coffee', function () {
267+
const migration = new Template(
268+
fileName,
269+
dirName,
270+
date,
271+
Template.TemplateType.DEFAULT_COFFEE,
272+
internals
273+
);
274+
275+
lab.test('should return default coffee template', () => {
276+
const actual = migration.getTemplate();
277+
Code.expect(actual).to.equal(migration.defaultCoffeeTemplate());
278+
});
279+
});
280+
281+
lab.experiment('as coffee sql loader', function () {
282+
const migration = new Template(
283+
fileName,
284+
dirName,
285+
date,
286+
Template.TemplateType.COFFEE_SQL_FILE_LOADER,
287+
internals
288+
);
289+
290+
lab.test('should return default coffee template', () => {
291+
const actual = migration.getTemplate();
292+
Code.expect(actual).to.equal(migration.coffeeSqlFileLoaderTemplate());
293+
});
294+
});
295+
296+
lab.experiment('as default javascript', function () {
297+
const migration = new Template(
298+
fileName,
299+
dirName,
300+
date,
301+
Template.TemplateType.DEFAULT_JS,
302+
internals
303+
);
304+
305+
lab.test('should return default sql template', () => {
306+
const actual = migration.getTemplate();
307+
Code.expect(actual).to.equal(migration.defaultJsTemplate());
308+
});
309+
});
310+
});
232311
}
233312

234313
function stubApiInstance (isModule, stubs, options, callback) {

test/pluginHelper.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const load = require('../lib/commands');
2+
3+
module.exports = {
4+
createSinglePlugin: (name, plugin) => {
5+
return load('fn/plugin.js')({
6+
[name]: [
7+
{
8+
[name]: plugin
9+
}
10+
]
11+
});
12+
}
13+
};

0 commit comments

Comments
 (0)