Skip to content

Commit

Permalink
fix(dart_frog_gen): windows route generation (felangel#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed May 26, 2022
1 parent 673591e commit 294196e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
28 changes: 13 additions & 15 deletions packages/dart_frog_gen/lib/src/build_route_configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ RouteConfiguration buildRouteConfiguration(Directory directory) {
final globalMiddleware = globalMiddlewareFile.existsSync()
? MiddlewareFile(
name: 'routes_middleware',
path: path.join(
'..',
path.relative(globalMiddlewareFile.path).replaceAll(r'\', '/'),
),
path: path
.join('..', path.relative(globalMiddlewareFile.path))
.replaceAll(r'\', '/'),
)
: null;

Expand Down Expand Up @@ -49,7 +48,8 @@ List<RouteDirectory> _getRouteDirectories(
}) {
final directories = <RouteDirectory>[];
final entities = directory.listSync();
final directorySegment = directory.path.split('routes').last;
final directorySegment =
directory.path.split('routes').last.replaceAll(r'\', '/');
final directoryPath = directorySegment.startsWith('/')
? directorySegment
: '/$directorySegment';
Expand All @@ -58,10 +58,9 @@ List<RouteDirectory> _getRouteDirectories(
if (directory.path != path.join(Directory.current.path, 'routes')) {
final _middleware = File(path.join(directory.path, '_middleware.dart'));
if (_middleware.existsSync()) {
final middlewarePath = path.join(
'..',
path.relative(_middleware.path).replaceAll(r'\', '/'),
);
final middlewarePath = path
.join('..', path.relative(_middleware.path))
.replaceAll(r'\', '/');
middleware = MiddlewareFile(
name: middlewarePath.toAlias(),
path: middlewarePath,
Expand Down Expand Up @@ -132,16 +131,15 @@ List<RouteFile> _getRouteFiles(
String prefix = '',
}) {
final files = <RouteFile>[];
final directorySegment = directory.path.split('routes').last;
final directorySegment =
directory.path.split('routes').last.replaceAll(r'\', '/');
final directoryPath = directorySegment.startsWith('/')
? directorySegment
: '/$directorySegment';
final entities = directory.listSync();
entities.where((e) => e.isRoute).cast<File>().forEach((entity) {
final filePath = path.join(
'..',
path.relative(entity.path).replaceAll(r'\', '/'),
);
final filePath =
path.join('..', path.relative(entity.path)).replaceAll(r'\', '/');
final fileRoutePath = pathToRoute(filePath).split(directoryPath).last;
var fileRoute = fileRoutePath.isEmpty ? '/' : fileRoutePath;
fileRoute = prefix + fileRoute;
Expand Down Expand Up @@ -175,7 +173,7 @@ extension on String {
}

String toRoute() {
return replaceAll('[', '<').replaceAll(']', '>');
return replaceAll('[', '<').replaceAll(']', '>').replaceAll(r'\', '/');
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/dart_frog_gen/lib/src/path_to_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import 'package:path/path.dart' as p;
/// "../routes/hello/<name>.dart" -> "/hello/<name>"
/// ```
String pathToRoute(String path) {
final relativePath = p.relative(path, from: '../routes');
final normalizedPath = path.replaceAll(r'\', '/');
final relativePath =
p.relative(normalizedPath, from: '../routes').replaceAll(r'\', '/');
final route = '/${relativePath.split('.dart').first.replaceAll('index', '')}';

if (route.length > 1 && route.endsWith('/')) {
Expand Down
7 changes: 6 additions & 1 deletion packages/dart_frog_gen/test/src/path_to_route_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ void main() {
'../routes/index.dart': '/',
'../routes/hello.dart': '/hello',
'../routes/hello/world.dart': '/hello/world',
'../routes/hello/<name>.dart': '/hello/<name>',
'../routes/hello/[name].dart': '/hello/[name]',
'../routes/api/v1/index.dart': '/api/v1',
r'..\routes\index.dart': '/',
r'..\routes\hello.dart': '/hello',
r'..\routes\hello\world.dart': '/hello/world',
r'..\routes\hello\[name].dart': '/hello/[name]',
r'..\routes\api\v1\index.dart': '/api/v1',
};

for (final entry in expectedPathToRouteMappings.entries) {
Expand Down

0 comments on commit 294196e

Please sign in to comment.