Skip to content

Commit cb54ba3

Browse files
authored
feat(dart_frog_cli): static file support (#110)
1 parent 09a2213 commit cb54ba3

File tree

5 files changed

+125
-9
lines changed

5 files changed

+125
-9
lines changed

packages/dart_frog_cli/e2e/test/helpers/kill_dart_frog_server.dart

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@ Future<void> killDartFrogServer(int pid) async {
1717
return;
1818
}
1919

20-
if (Platform.isLinux || Platform.isMacOS) {
20+
if (Platform.isLinux) {
21+
final result = await Process.run('fuser', ['-n', 'tcp', '-k', '8080']);
22+
23+
if (result.exitCode != 0) {
24+
throw Exception(
25+
'fuser -n tcp -k 8080 exited with code ${result.exitCode}',
26+
);
27+
}
28+
29+
return;
30+
}
31+
32+
if (Platform.isMacOS) {
2133
final result = await Process.run('pkill', ['-f', 'dart_frog']);
2234

2335
if (result.exitCode != 0) {

packages/dart_frog_cli/lib/src/commands/build/templates/dart_frog_prod_server_bundle.dart

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dart_frog_cli/lib/src/commands/dev/dev.dart

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,17 @@ class DevCommand extends DartFrogCommand {
144144
await serve();
145145
progress.complete('Running on http://localhost:$port');
146146

147-
final watcher = _directoryWatcher(path.join(cwd.path, 'routes'));
148-
final subscription = watcher.events.listen((_) => codegen());
147+
final public = path.join(cwd.path, 'public');
148+
final routes = path.join(cwd.path, 'routes');
149+
150+
bool shouldRunCodegen(WatchEvent event) {
151+
return path.isWithin(routes, event.path) ||
152+
path.isWithin(public, event.path);
153+
}
154+
155+
final watcher = _directoryWatcher(path.join(cwd.path));
156+
final subscription =
157+
watcher.events.where(shouldRunCodegen).listen((_) => codegen());
149158

150159
await subscription.asFuture<void>();
151160
await subscription.cancel();

packages/dart_frog_cli/lib/src/commands/dev/templates/dart_frog_dev_server_bundle.dart

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dart_frog_cli/test/src/commands/dev/dev_test.dart

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:args/args.dart';
66
import 'package:dart_frog_cli/src/commands/commands.dart';
77
import 'package:mason/mason.dart';
88
import 'package:mocktail/mocktail.dart';
9+
import 'package:path/path.dart' as path;
910
import 'package:test/test.dart';
1011
import 'package:watcher/watcher.dart';
1112

@@ -123,7 +124,101 @@ void main() {
123124
workingDirectory: any(named: 'workingDirectory'),
124125
onVarsChanged: any(named: 'onVarsChanged'),
125126
),
126-
).called(2);
127+
).called(1);
128+
});
129+
130+
test('runs codegen when changes are made to the public/routes directory',
131+
() async {
132+
final controller = StreamController<WatchEvent>();
133+
final generatorHooks = _MockGeneratorHooks();
134+
when(
135+
() => generatorHooks.preGen(
136+
vars: any(named: 'vars'),
137+
workingDirectory: any(named: 'workingDirectory'),
138+
onVarsChanged: any(named: 'onVarsChanged'),
139+
),
140+
).thenAnswer((invocation) async {
141+
(invocation.namedArguments[const Symbol('onVarsChanged')] as Function(
142+
Map<String, dynamic> vars,
143+
))
144+
.call(<String, dynamic>{});
145+
});
146+
when(
147+
() => generator.generate(
148+
any(),
149+
vars: any(named: 'vars'),
150+
fileConflictResolution: FileConflictResolution.overwrite,
151+
),
152+
).thenAnswer((_) async => []);
153+
when(() => generator.hooks).thenReturn(generatorHooks);
154+
when(() => process.stdout).thenAnswer(
155+
(_) => Stream.value(utf8.encode('[hotreload] hot reload enabled.')),
156+
);
157+
when(() => process.stderr).thenAnswer((_) => const Stream.empty());
158+
when(() => directoryWatcher.events).thenAnswer((_) => controller.stream);
159+
160+
command.run().ignore();
161+
162+
await Future<void>.delayed(Duration.zero);
163+
164+
verify(
165+
() => generator.generate(
166+
any(),
167+
vars: any(named: 'vars'),
168+
fileConflictResolution: FileConflictResolution.overwrite,
169+
),
170+
).called(1);
171+
172+
controller.add(
173+
WatchEvent(
174+
ChangeType.MODIFY,
175+
path.join(Directory.current.path, 'routes', 'index.dart'),
176+
),
177+
);
178+
179+
await Future<void>.delayed(Duration.zero);
180+
181+
verify(
182+
() => generator.generate(
183+
any(),
184+
vars: any(named: 'vars'),
185+
fileConflictResolution: FileConflictResolution.overwrite,
186+
),
187+
).called(1);
188+
189+
controller.add(
190+
WatchEvent(
191+
ChangeType.MODIFY,
192+
path.join(Directory.current.path, 'public', 'hello.txt'),
193+
),
194+
);
195+
196+
await Future<void>.delayed(Duration.zero);
197+
198+
verify(
199+
() => generator.generate(
200+
any(),
201+
vars: any(named: 'vars'),
202+
fileConflictResolution: FileConflictResolution.overwrite,
203+
),
204+
).called(1);
205+
206+
controller.add(
207+
WatchEvent(
208+
ChangeType.MODIFY,
209+
path.join(Directory.current.path, 'tmp', 'message.txt'),
210+
),
211+
);
212+
213+
await Future<void>.delayed(Duration.zero);
214+
215+
verifyNever(
216+
() => generator.generate(
217+
any(),
218+
vars: any(named: 'vars'),
219+
fileConflictResolution: FileConflictResolution.overwrite,
220+
),
221+
);
127222
});
128223

129224
test('caches snapshot when hotreload runs successfully', () async {

0 commit comments

Comments
 (0)