Skip to content

Commit b20fa1c

Browse files
committed
feat(hook): inject new template into create migration command
Signed-off-by: Tobias Gurtzick <[email protected]>
1 parent cfec59d commit b20fa1c

File tree

3 files changed

+80
-18
lines changed

3 files changed

+80
-18
lines changed

api.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ function dbmigrate (plugins, isModule, options, callback) {
3737
}
3838
var internals = this.internals;
3939

40-
console.log(load('fn/plugin'));
4140
this.internals.plugins = load('fn/plugin')(plugins);
4241

4342
if (typeof callback === 'function') this.internals.onComplete = callback;

index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@ function loadPlugins (options) {
4747
for (; i < length; ++i) {
4848
var plugin = require(path.join(options.cwd, 'node_modules', plugins[i]));
4949

50-
if (
51-
typeof plugin.name !== 'string' ||
52-
!plugin.hooks ||
53-
!plugin.loadPlugin
54-
) {
50+
if (!plugin.hooks || !plugin.loadPlugin) {
5551
continue;
5652
}
5753

54+
// name is now derived from package name
55+
plugin.name = plugins[i];
56+
5857
plugin.hooks.forEach(function (hook) {
5958
hooks[hook] = hooks[hook] || [];
6059
hooks[hook].push(plugin);

lib/commands/create-migration.js

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ async function createMigrationDir (dir) {
2222
async function executeCreateMigration (internals, config) {
2323
let migrationsDir = internals.argv['migrations-dir'];
2424
let path;
25+
let hooks = false;
26+
let pluginTemplate = false;
27+
let customWrite = false;
28+
const { plugins } = internals;
2529

2630
internals.runTimestamp = new Date();
2731

@@ -79,19 +83,79 @@ async function executeCreateMigration (internals, config) {
7983
} else if (shouldCreateCoffeeFile(internals, config)) {
8084
templateType = Migration.TemplateType.DEFAULT_COFFEE;
8185
}
82-
const migration = new Migration(
83-
internals.argv.title +
84-
(shouldCreateCoffeeFile(internals, config) ? '.coffee' : '.js'),
85-
path,
86-
internals.runTimestamp,
87-
templateType,
88-
internals.plugins
89-
);
9086

91-
await migration.write();
92-
log.info(util.format('Created migration at %s', migration.file.path));
93-
if (shouldCreateSqlFiles(internals, config)) {
94-
return createSqlFiles(internals, config);
87+
if (plugins) {
88+
hooks = plugins.hook('create:template') || [];
89+
}
90+
91+
if (hooks !== false) {
92+
for (const plugin of hooks) {
93+
const template = plugin['init:template']();
94+
95+
if (internals.argv[template.option] || config[template.option]) {
96+
pluginTemplate = plugin;
97+
customWrite = plugin['create:template:custom:write'] === true;
98+
99+
if (customWrite) {
100+
templateType = `${plugin.name}${templateType}`;
101+
} else {
102+
templateType = template.type;
103+
}
104+
105+
// no reason to continue processing here
106+
break;
107+
}
108+
}
109+
}
110+
111+
if (!pluginTemplate && !customWrite) {
112+
const migration = new Migration(
113+
internals.argv.title +
114+
(shouldCreateCoffeeFile(internals, config) ? '.coffee' : '.js'),
115+
path,
116+
internals.runTimestamp,
117+
templateType,
118+
internals.plugins
119+
);
120+
121+
await migration.write();
122+
log.info(util.format('Created migration at %s', migration.file.path));
123+
if (shouldCreateSqlFiles(internals, config)) {
124+
return createSqlFiles(internals, config);
125+
}
126+
} else {
127+
const plugin = pluginTemplate;
128+
129+
if (typeof plugin['write:template'] !== 'function') {
130+
log.error(`Plugin ${plugin.name} does not have function write:template`);
131+
throw new Error(`write:template not existent!`);
132+
}
133+
134+
await plugin['write:template'](
135+
{ argv: { ...internals.argv }, config: { ...config } },
136+
async opts => {
137+
let title = internals.argv.title;
138+
let _path = path;
139+
const extension = opts.extension || '.js';
140+
141+
if (opts.suffix) {
142+
title += opts.suffix;
143+
}
144+
145+
if (opts.pathExtension) {
146+
_path += opts.pathExtension;
147+
}
148+
149+
const migration = new Migration(
150+
title + extension,
151+
_path,
152+
internals.runTimestamp,
153+
opts.type,
154+
internals.plugins
155+
);
156+
await migration.write();
157+
}
158+
);
95159
}
96160

97161
return Promise.resolve();

0 commit comments

Comments
 (0)