@@ -46,16 +46,31 @@ void preGen(
46
46
return exit (1 );
47
47
}
48
48
49
- // The path in which the route will be created
49
+ // The path in which the route or middleware will be created
50
50
final routePath = normalizeRoutePath (context.vars['route_path' ] as String );
51
51
52
- return _preGenRoute (
53
- context,
54
- routePath: routePath,
55
- routeConfiguration: routeConfiguration,
56
- projectDirectory: projectDirectory,
57
- exit: exit,
58
- );
52
+ if (type == 'route' ) {
53
+ return _preGenRoute (
54
+ context,
55
+ routePath: routePath,
56
+ routeConfiguration: routeConfiguration,
57
+ projectDirectory: projectDirectory,
58
+ exit: exit,
59
+ );
60
+ }
61
+
62
+ if (type == 'middleware' ) {
63
+ return _preGenMiddleware (
64
+ context,
65
+ routePath: routePath,
66
+ routeConfiguration: routeConfiguration,
67
+ projectDirectory: projectDirectory,
68
+ exit: exit,
69
+ );
70
+ }
71
+
72
+ context.logger.err ('Unrecognized type: $type ' );
73
+ return exit (1 );
59
74
}
60
75
61
76
void _preGenRoute (
@@ -125,7 +140,86 @@ void _preGenRoute(
125
140
return exit (1 );
126
141
}
127
142
143
+ context.vars['is_route' ] = true ;
128
144
context.vars['dir_path' ] = path.dirname (routeFileName);
129
145
context.vars['filename' ] = path.basename (routeFileName);
130
146
context.vars['params' ] = parameterNames;
131
147
}
148
+
149
+ void _preGenMiddleware (
150
+ HookContext context, {
151
+ required String routePath,
152
+ required RouteConfiguration routeConfiguration,
153
+ required io.Directory projectDirectory,
154
+ required void Function (int exitCode) exit,
155
+ }) {
156
+ final routesDirectoryPath = path.relative (
157
+ io.Directory (path.join (projectDirectory.path, 'routes' )).path,
158
+ );
159
+
160
+ const middlewareFilename = '_middleware.dart' ;
161
+
162
+ // Get the path to directory containing the middleware file
163
+ final String middlewareContainingDir;
164
+ if (routePath == '/' ) {
165
+ middlewareContainingDir = routesDirectoryPath;
166
+ } else {
167
+ middlewareContainingDir = path.withoutExtension (
168
+ routeToPath (
169
+ routePath.toBracketParameterSyntax,
170
+ preamble: routesDirectoryPath,
171
+ ),
172
+ );
173
+ }
174
+
175
+ // Verify if the middleware file already exists
176
+ final middlewareFilePath =
177
+ path.join (middlewareContainingDir, middlewareFilename);
178
+ final middlewareExists = io.File (middlewareFilePath).existsSync ();
179
+ if (middlewareExists) {
180
+ context.logger.err ('There is already a middleware at $middlewareFilePath ' );
181
+ return exit (1 );
182
+ }
183
+
184
+ // Verify if the given route already exists as directory
185
+ final routeExistsAsDirectory = io.Directory (
186
+ middlewareContainingDir.toBracketParameterSyntax,
187
+ ).existsSync ();
188
+
189
+ // If the route does not exist as directory, we must check if any of its
190
+ // ancestor routes exists as file routes to avoid rogue routes.
191
+ if (! routeExistsAsDirectory) {
192
+ final fileRoute = routeConfiguration.containingFileRoute (
193
+ routePath,
194
+ includeSelf: true ,
195
+ );
196
+
197
+ if (fileRoute != null ) {
198
+ final filePath = path.normalize (
199
+ path.join (routesDirectoryPath, fileRoute.path),
200
+ );
201
+
202
+ io.Directory (path.withoutExtension (filePath)).createSync ();
203
+
204
+ final newFilepath = filePath.replaceFirst ('.dart' , '/index.dart' );
205
+ io.File (filePath).renameSync (newFilepath);
206
+ context.logger
207
+ .detail ('Renamed $filePath to $newFilepath to avoid rogue routes' );
208
+ }
209
+ }
210
+
211
+ try {
212
+ middlewareContainingDir.toBracketParameterSyntax.getParameterNames ();
213
+ } on FormatException catch (exception) {
214
+ context.logger.err ('Failed to create middleware: ${exception .message }' );
215
+ return exit (1 );
216
+ }
217
+
218
+ context.logger.detail (
219
+ 'Creating middleware file: ${middlewareFilePath .toBracketParameterSyntax }' ,
220
+ );
221
+
222
+ context.vars['is_middleware' ] = true ;
223
+ context.vars['dir_path' ] = middlewareContainingDir.toBracketParameterSyntax;
224
+ context.vars['filename' ] = middlewareFilename;
225
+ }
0 commit comments