diff --git a/lib/src/constants/command_constants.dart b/lib/src/constants/command_constants.dart index a1716f6..929285c 100644 --- a/lib/src/constants/command_constants.dart +++ b/lib/src/constants/command_constants.dart @@ -38,6 +38,14 @@ const String ksAppPlatforms = 'platforms'; const String ksProjectPath = 'project-path'; const String ksNoTest = 'no-test'; +/// Source directories to format when no explicit file path is provided. +const List formatSourceDirectories = [ + 'lib', + 'test', + 'integration_test', + 'bin', +]; + /// A list of strings that are used to run the run build_runner /// [build or watch] --delete-conflicting-outputs command. const List buildRunnerArguments = [ diff --git a/lib/src/services/process_service.dart b/lib/src/services/process_service.dart index a400ed3..21b9091 100644 --- a/lib/src/services/process_service.dart +++ b/lib/src/services/process_service.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'dart:io'; +import 'package:path/path.dart' as p; import 'package:stacked_cli/src/constants/command_constants.dart'; import 'package:stacked_cli/src/locator.dart'; import 'package:stacked_cli/src/services/colorized_log_service.dart'; @@ -81,23 +82,42 @@ class ProcessService { ); } - /// Runs the dart format . command on the app's source code. + /// Runs dart format on the app's source directories. + /// + /// When [filePath] is provided, only that path is formatted. Otherwise, + /// existing directories from [formatSourceDirectories] are formatted. /// /// Args: /// appName (String): The name of the app. Future runFormat({String? appName, String? filePath}) async { + final formatPaths = + filePath != null ? [filePath] : await _resolveFormatPaths(appName); + await _runProcess( programName: ksDart, arguments: [ ksFormat, - filePath ?? ksCurrentDirectory, + ...formatPaths, '-l', - _formattingLineLength + _formattingLineLength, ], workingDirectory: appName, ); } + Future> _resolveFormatPaths(String? workingDirectory) async { + final base = workingDirectory ?? Directory.current.path; + final paths = []; + + for (final dir in formatSourceDirectories) { + if (await Directory(p.join(base, dir)).exists()) { + paths.add(dir); + } + } + + return paths.isEmpty ? [ksCurrentDirectory] : paths; + } + /// It runs the `dart pub global activate` command in the app's directory Future runPubGlobalActivate() async { await _runProcess( diff --git a/test/services/process_service_test.dart b/test/services/process_service_test.dart index 5c85ec2..2c4c4ba 100644 --- a/test/services/process_service_test.dart +++ b/test/services/process_service_test.dart @@ -12,7 +12,8 @@ void main() { setUp(() => registerServices()); tearDown(() => locator.reset()); group('runFormat -', () { - test('when called should run dart format . and finish in exit code 0', + test( + 'when called should format source directories and finish in exit code 0', () async { var clog = getAndRegisterColorizedLogService(); var service = _getService(); @@ -21,13 +22,20 @@ void main() { }); test( - 'when called should run dart format . and ouput error if appName is not found', + 'when called should format source directories and output error if appName is not found', () async { var clog = getAndRegisterColorizedLogService(); var service = _getService(); await service.runFormat(appName: "xyz"); verify(clog.error(message: anyNamed('message'))); }); + + test('when filePath is provided should format only that path', () async { + var clog = getAndRegisterColorizedLogService(); + var service = _getService(); + await service.runFormat(filePath: 'lib'); + verify(clog.success(message: 'Command complete. ExitCode: 0')); + }); }); }); }