@@ -12,7 +12,7 @@ import 'package:pub_updater/pub_updater.dart';
12
12
13
13
const executableName = 'ai_commit' ;
14
14
const packageName = 'ai_commit' ;
15
- const description = 'Dart CLI for generate commit messages with OpenAI.' ;
15
+ const description = 'Dart CLI for generated commit messages with OpenAI.' ;
16
16
17
17
/// {@template ai_commit_command_runner}
18
18
/// A [CommandRunner] for the CLI.
@@ -106,15 +106,18 @@ Format the commit message according to the Conventional Commits specification.''
106
106
// On format errors, show the commands error message, root usage and
107
107
// exit with an error code
108
108
_logger
109
- ..err (e.message)..err ('$stackTrace ' )
110
- ..info ('' )..info (usage);
109
+ ..err (e.message)
110
+ ..err ('$stackTrace ' )
111
+ ..info ('' )
112
+ ..info (usage);
111
113
return ExitCode .usage.code;
112
114
} on UsageException catch (e) {
113
115
// On usage errors, show the commands usage message and
114
116
// exit with an error code
115
117
_logger
116
118
..err (e.message)
117
- ..info ('' )..info (e.usage);
119
+ ..info ('' )
120
+ ..info (e.usage);
118
121
return ExitCode .usage.code;
119
122
}
120
123
}
@@ -128,7 +131,9 @@ Format the commit message according to the Conventional Commits specification.''
128
131
}
129
132
130
133
// Verbose logs
131
- _logger..detail ('Argument information:' )..detail (' Top level options:' );
134
+ _logger
135
+ ..detail ('Argument information:' )
136
+ ..detail (' Top level options:' );
132
137
for (final option in topLevelResults.options) {
133
138
if (topLevelResults.wasParsed (option)) {
134
139
_logger.detail (' - $option : ${topLevelResults [option ]}' );
@@ -203,72 +208,69 @@ Run ${lightCyan.wrap('$executableName update')} to update''',
203
208
required dynamic exclude,
204
209
required dynamic conventional,
205
210
}) async {
206
- try {
207
- final apiKey = await getKey ();
208
- if (apiKey == null ) {
209
- _logger.info (
210
- '''No API key found. To generate one, run ${lightCyan .wrap ('$executableName config' )}''' ,
211
- );
212
- return ExitCode .software.code;
213
- }
214
-
215
- if (all == true ) await Process .run ('git' , ['add' , '--update' ]);
211
+ final apiKey = await getKey ();
212
+ if (apiKey == null ) {
213
+ _logger.info (
214
+ '''No API key found. To generate one, run ${lightCyan .wrap ('$executableName config' )}''' ,
215
+ );
216
+ return ExitCode .software.code;
217
+ }
216
218
217
- int ? msgCount ;
219
+ if (all == true ) await Process . run ( 'git' , [ 'add' , '--update' ]) ;
218
220
219
- if (count != null ) {
220
- final value = int .tryParse ('$count ' );
221
- if (value == null ) {
222
- _logger.err ('Count must be an integer.' );
221
+ int ? msgCount;
223
222
224
- return ExitCode .software.code;
225
- }
226
- if (value < 0 ) {
227
- _logger.err ('Count must be an greater than 0.' );
228
- return ExitCode .software.code;
229
- }
223
+ if (count != null ) {
224
+ final value = int .tryParse ('$count ' );
225
+ if (value == null ) {
226
+ _logger.err ('Count must be an integer.' );
230
227
231
- if (value > 5 ) {
232
- _logger.err ('Count must be less than or equal to 5.' );
233
- return ExitCode .software.code;
234
- }
228
+ return ExitCode .software.code;
229
+ }
230
+ if (value < 0 ) {
231
+ _logger.err ('Count must be an greater than 0.' );
232
+ return ExitCode .software.code;
233
+ }
235
234
236
- msgCount = value;
235
+ if (value > 5 ) {
236
+ _logger.err ('Count must be less than or equal to 5.' );
237
+ return ExitCode .software.code;
237
238
}
238
239
239
- var excludeFiles = < String > [];
240
+ msgCount = value;
241
+ }
240
242
241
- if (exclude != null ) {
242
- excludeFiles = [
243
- for (final e in exclude.toString ().split (',' )) e.trim ()
244
- ];
245
- }
243
+ var excludeFiles = < String > [];
246
244
247
- final detectingFiles = _logger.progress ('Detecting staged files' );
245
+ if (exclude != null ) {
246
+ excludeFiles = [for (final e in exclude.toString ().split (',' )) e.trim ()];
247
+ }
248
248
249
- final staged = await getStagedDiff (excludeFiles : excludeFiles );
249
+ final detectingFiles = _logger. progress ( 'Detecting staged files' );
250
250
251
- if (staged.isEmpty) {
252
- detectingFiles.complete ('Detecting staged files' );
253
- _logger.info (
254
- '''
251
+ final staged = await getStagedDiff (excludeFiles: excludeFiles);
252
+
253
+ if (staged.isEmpty) {
254
+ detectingFiles.complete ('Detecting staged files' );
255
+ _logger.info (
256
+ '''
255
257
No staged changes found. Stage your changes manually, or automatically stage all changes with the `--all` options.''' ,
256
- );
257
- return ExitCode .success.code;
258
- }
258
+ );
259
+ return ExitCode .success.code;
260
+ }
259
261
260
- final files = staged['files' ] as List <String >? ?? [];
262
+ final files = staged['files' ] as List <String >? ?? [];
261
263
262
- var message = getDetectedMessage (files: files);
264
+ var message = getDetectedMessage (files: files);
263
265
264
- detectingFiles.complete (
265
- '$message :\n ${files .map ((e ) => ' $e ' ).join ('\n ' )}' ,
266
- );
266
+ detectingFiles.complete (message);
267
+ _logger.info (files.map ((e) => ' $e ' ).join ('\n ' ));
267
268
268
- final s = _logger.progress ('The AI is analyzing your changes' );
269
+ final s = _logger.progress ('The AI is analyzing your changes' );
269
270
270
- var messages = < String > [];
271
+ var messages = < String > [];
271
272
273
+ try {
272
274
final locale = await getLocale ();
273
275
final maxLength = await getMaxLength ();
274
276
@@ -297,47 +299,38 @@ No staged changes found. Stage your changes manually, or automatically stage all
297
299
diff: staged['diff' ] as String ,
298
300
isConventional: isConventional,
299
301
);
302
+ } finally {
303
+ s.complete ('Changes analyzed' );
304
+ }
300
305
301
- s.fail ('Changes analyzed' );
302
-
303
- if (messages.isEmpty) {
304
- _logger.info ('No commit messages were generated. Try again.' );
305
- return ExitCode .success.code;
306
- }
306
+ if (messages.isEmpty) {
307
+ _logger.info ('No commit messages were generated. Try again.' );
308
+ return ExitCode .success.code;
309
+ }
307
310
308
- if (messages.length == 1 ) {
309
- message = messages.first;
311
+ if (messages.length == 1 ) {
312
+ message = messages.first;
310
313
311
- final confirmed =
312
- _logger.confirm ('Use this commit message?\n\n $message \n ' );
314
+ final confirmed =
315
+ _logger.confirm ('\n Use this commit message?\n\n $message \n \n ' );
313
316
314
- if (! confirmed) {
315
- _logger.info ('Commit message canceled.' );
316
- return ExitCode .software.code;
317
- }
318
- } else {
319
- final selected = _logger.chooseOne (
320
- 'Pick a commit message to use: ' ,
321
- choices: messages,
322
- );
323
-
324
- if (selected.isEmpty) {
325
- _logger.info ('Commit message canceled.' );
326
- return ExitCode .software.code;
327
- }
328
-
329
- message = selected;
317
+ if (! confirmed) {
318
+ s.fail ('Commit message canceled.' );
319
+ return ExitCode .software.code;
330
320
}
321
+ } else {
322
+ final selected = _logger.chooseOne (
323
+ 'Pick a commit message to use: ' ,
324
+ choices: messages,
325
+ );
331
326
332
- await Process .run ('git' , ['commit' , '-m' , message]);
327
+ message = selected;
328
+ }
333
329
334
- s. complete ( 'Successfully committed' );
330
+ await Process . run ( 'git' , [ 'commit' , '-m' , message] );
335
331
336
- return ExitCode .success.code;
337
- } catch (e) {
338
- print (e.runtimeType);
339
- print (e);
340
- exit (0 );
341
- }
332
+ s.complete ('Successfully committed' );
333
+
334
+ return ExitCode .success.code;
342
335
}
343
336
}
0 commit comments