@@ -22,6 +22,10 @@ async function createMigrationDir (dir) {
22
22
async function executeCreateMigration ( internals , config ) {
23
23
let migrationsDir = internals . argv [ 'migrations-dir' ] ;
24
24
let path ;
25
+ let hooks = false ;
26
+ let pluginTemplate = false ;
27
+ let customWrite = false ;
28
+ const { plugins } = internals ;
25
29
26
30
internals . runTimestamp = new Date ( ) ;
27
31
@@ -79,19 +83,79 @@ async function executeCreateMigration (internals, config) {
79
83
} else if ( shouldCreateCoffeeFile ( internals , config ) ) {
80
84
templateType = Migration . TemplateType . DEFAULT_COFFEE ;
81
85
}
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
- ) ;
90
86
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
+ ) ;
95
159
}
96
160
97
161
return Promise . resolve ( ) ;
0 commit comments