Add dry-run

This commit is contained in:
Tobe O 2019-07-29 18:05:46 -04:00
parent bf5c4084b4
commit bfdb681ec2

View file

@ -15,6 +15,11 @@ var argParser = ArgParser()
..addOption('tab-size', ..addOption('tab-size',
help: 'The number of spaces to output where a TAB would be inserted.', help: 'The number of spaces to output where a TAB would be inserted.',
defaultsTo: '2') defaultsTo: '2')
..addFlag('dry-run',
abbr: 'n',
help:
'Print the names of files that would be changed, without actually overwriting them.',
negatable: false)
..addFlag('help', ..addFlag('help',
abbr: 'h', help: 'Print this usage information.', negatable: false) abbr: 'h', help: 'Print this usage information.', negatable: false)
..addFlag('insert-spaces', ..addFlag('insert-spaces',
@ -89,7 +94,16 @@ Future<void> formatFile(File file, ArgResults argResults) async {
var formatted = await format(file.path, content, argResults); var formatted = await format(file.path, content, argResults);
if (formatted == null) return; if (formatted == null) return;
if (argResults['overwrite'] as bool) { if (argResults['overwrite'] as bool) {
await file.writeAsStringSync(formatted); if (formatted != content) {
if (argResults['dry-run'] as bool) {
print('Would have formatted ${file.path}');
} else {
await file.writeAsStringSync(formatted);
print('Formatted ${file.path}');
}
} else {
print('Unchanged ${file.path}');
}
} else { } else {
print(formatted); print(formatted);
} }