|
| 1 | +// ignore_for_file: avoid_print |
| 2 | + |
| 3 | +import 'dart:io'; |
| 4 | + |
| 5 | +import 'package:approval_tests/src/widget/src/common.dart'; |
| 6 | +import 'package:approval_tests/src/widget/src/git_diffs.dart'; |
| 7 | + |
| 8 | +void main() async { |
| 9 | + final searchDirectory = Directory.current; |
| 10 | + |
| 11 | + final List<Future<void>> tasks = []; |
| 12 | + |
| 13 | + /// Recursively search for current files |
| 14 | + await for (final file in searchDirectory.list(recursive: true)) { |
| 15 | + if (file.path.endsWith('.unapproved.txt')) { |
| 16 | + final reviewFile = file; |
| 17 | + final approvedFileName = file.path.replaceAll('.unapproved.txt', '.approved.txt'); |
| 18 | + final approvedFile = File(approvedFileName); |
| 19 | + |
| 20 | + if (approvedFile.existsSync()) { |
| 21 | + tasks.add(processFile(approvedFile, reviewFile)); |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + await Future.wait(tasks); |
| 27 | + |
| 28 | + print('Review process completed.'); |
| 29 | +} |
| 30 | + |
| 31 | +/// Check of the files are different using "git diff" |
| 32 | +Future<void> processFile(File approvedFile, FileSystemEntity reviewFile) async { |
| 33 | + final resultString = gitDiffFiles(approvedFile, reviewFile); |
| 34 | + |
| 35 | + if (resultString.isNotEmpty || resultString.isNotEmpty) { |
| 36 | + final String fileNameWithoutExtension = approvedFile.path.split('/').last.split('.').first; |
| 37 | + printGitDiffs(fileNameWithoutExtension, resultString); |
| 38 | + |
| 39 | + String? firstCharacter; |
| 40 | + |
| 41 | + do { |
| 42 | + stdout.write('Accept changes? (y/N/[v]iew): '); |
| 43 | + final response = stdin.readLineSync()?.trim().toLowerCase(); |
| 44 | + |
| 45 | + if (response == null || response.isEmpty) { |
| 46 | + firstCharacter = null; |
| 47 | + } else { |
| 48 | + firstCharacter = response[0]; |
| 49 | + } |
| 50 | + |
| 51 | + if (firstCharacter == 'y') { |
| 52 | + await approvedFile.delete(); |
| 53 | + await reviewFile.rename(approvedFile.path); |
| 54 | + print('Approval test approved'); |
| 55 | + } else if (firstCharacter == 'v') { |
| 56 | + if (isCodeCommandAvailable()) { |
| 57 | + final approvedFilename = approvedFile.path; |
| 58 | + final reviewFilename = reviewFile.path; |
| 59 | + |
| 60 | + print("Executing 'code --diff $approvedFilename $reviewFilename'"); |
| 61 | + final processResult = Process.runSync('code', ['--diff', approvedFilename, reviewFilename]); |
| 62 | + print('processResult: ${processResult.toString()}'); |
| 63 | + } else { |
| 64 | + print(''' |
| 65 | +$topBar |
| 66 | + To enable the 'v' command, your system must be configured to run VSCode from the command line: |
| 67 | + 0. Install VSCode (if you haven't already) |
| 68 | + 1. Open Visual Studio Code. |
| 69 | + 2. Open the Command Palette by pressing Cmd + Shift + P. |
| 70 | + 3. Type ‘Shell Command’ into the Command Palette and look for the option ‘Install ‘code’ command in PATH’. |
| 71 | + 4. Select it and it should install the necessary scripts so that you can use code from the terminal. |
| 72 | +$bottomBar'''); |
| 73 | + } |
| 74 | + } else { |
| 75 | + print('Approval test rejected'); |
| 76 | + } |
| 77 | + } while (firstCharacter == 'v'); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +bool isCodeCommandAvailable() { |
| 82 | + final result = Process.runSync('which', ['code']); |
| 83 | + return result.exitCode == 0; |
| 84 | +} |
0 commit comments