Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/src/constants/command_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> buildRunnerArguments = [
Expand Down
26 changes: 23 additions & 3 deletions lib/src/services/process_service.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<void> 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<List<String>> _resolveFormatPaths(String? workingDirectory) async {
final base = workingDirectory ?? Directory.current.path;
final paths = <String>[];

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<void> runPubGlobalActivate() async {
await _runProcess(
Expand Down
12 changes: 10 additions & 2 deletions test/services/process_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'));
});
});
});
}