diff --git a/example/main.dart b/example/main.dart index 26b3157..7669ec5 100644 --- a/example/main.dart +++ b/example/main.dart @@ -11,9 +11,6 @@ void main() async { ); await logger.progress( 'A progress message', - () async => Future.delayed( - const Duration(seconds: 3), - () => true, - ), + () async => Future.delayed(const Duration(seconds: 3), () => true), ); } diff --git a/lib/src/analytics/analytics.dart b/lib/src/analytics/analytics.dart index d0806d9..fb349da 100644 --- a/lib/src/analytics/analytics.dart +++ b/lib/src/analytics/analytics.dart @@ -10,9 +10,7 @@ abstract interface class Analytics { void cleanUp(); /// Track an event. - void track({ - required String event, - }); + void track({required String event}); } /// Analytics service for MixPanel. @@ -26,17 +24,15 @@ class MixPanelAnalytics implements Analytics { required String uniqueUserId, required String projectToken, required String version, - }) : _uniqueUserId = uniqueUserId, - _projectToken = projectToken, - _version = version; + }) : _uniqueUserId = uniqueUserId, + _projectToken = projectToken, + _version = version; @override void cleanUp() {} @override - void track({ - required String event, - }) { + void track({required String event}) { var payload = jsonEncode({ 'event': event, 'properties': { @@ -46,7 +42,7 @@ class MixPanelAnalytics implements Analytics { 'dart_version': Platform.version, 'is_ci': ci.isCI, 'version': _version, - } + }, }); _quietPost(payload); @@ -66,14 +62,16 @@ class MixPanelAnalytics implements Analytics { Future _quietPost(String payload) async { try { - await http.post( - Uri.parse(_endpoint), - body: 'data=$payload', - headers: { - 'Accept': 'text/plain', - 'Content-Type': 'application/x-www-form-urlencoded', - }, - ).timeout(const Duration(seconds: 2)); + await http + .post( + Uri.parse(_endpoint), + body: 'data=$payload', + headers: { + 'Accept': 'text/plain', + 'Content-Type': 'application/x-www-form-urlencoded', + }, + ) + .timeout(const Duration(seconds: 2)); } catch (e) { return; } diff --git a/lib/src/better_command_runner/better_command.dart b/lib/src/better_command_runner/better_command.dart index b26a661..b051b63 100644 --- a/lib/src/better_command_runner/better_command.dart +++ b/lib/src/better_command_runner/better_command.dart @@ -6,11 +6,9 @@ abstract class BetterCommand extends Command { final PassMessage? _logInfo; final ArgParser _argParser; - BetterCommand({ - PassMessage? logInfo, - int? wrapTextColumn, - }) : _logInfo = logInfo, - _argParser = ArgParser(usageLineLength: wrapTextColumn); + BetterCommand({PassMessage? logInfo, int? wrapTextColumn}) + : _logInfo = logInfo, + _argParser = ArgParser(usageLineLength: wrapTextColumn); @override ArgParser get argParser => _argParser; diff --git a/lib/src/better_command_runner/better_command_runner.dart b/lib/src/better_command_runner/better_command_runner.dart index d918c9c..46baf9b 100644 --- a/lib/src/better_command_runner/better_command_runner.dart +++ b/lib/src/better_command_runner/better_command_runner.dart @@ -5,9 +5,7 @@ import 'package:args/command_runner.dart'; import 'package:cli_tools/src/better_command_runner/exit_exception.dart'; /// A function type for executing code before running a command. -typedef OnBeforeRunCommand = Future Function( - BetterCommandRunner runner, -); +typedef OnBeforeRunCommand = Future Function(BetterCommandRunner runner); /// A function type for passing log messages. typedef PassMessage = void Function(String message); @@ -16,10 +14,11 @@ typedef PassMessage = void Function(String message); /// The [logLevel] is the log level to set. /// The [commandName] is the name of the command if custom rules for log /// levels are needed. -typedef SetLogLevel = void Function({ - required CommandRunnerLogLevel parsedLogLevel, - String? commandName, -}); +typedef SetLogLevel = + void Function({ + required CommandRunnerLogLevel parsedLogLevel, + String? commandName, + }); /// A function type for tracking events. typedef OnAnalyticsEvent = void Function(String event); @@ -64,18 +63,19 @@ class BetterCommandRunner extends CommandRunner { OnBeforeRunCommand? onBeforeRunCommand, OnAnalyticsEvent? onAnalyticsEvent, int? wrapTextColumn, - }) : _logError = logError, - _logInfo = logInfo, - _onBeforeRunCommand = onBeforeRunCommand, - _setLogLevel = setLogLevel, - _onAnalyticsEvent = onAnalyticsEvent, - _argParser = ArgParser(usageLineLength: wrapTextColumn) { + }) : _logError = logError, + _logInfo = logInfo, + _onBeforeRunCommand = onBeforeRunCommand, + _setLogLevel = setLogLevel, + _onAnalyticsEvent = onAnalyticsEvent, + _argParser = ArgParser(usageLineLength: wrapTextColumn) { argParser.addFlag( BetterCommandRunnerFlags.quiet, abbr: BetterCommandRunnerFlags.quietAbbr, defaultsTo: false, negatable: false, - help: 'Suppress all cli output. Is overridden by ' + help: + 'Suppress all cli output. Is overridden by ' ' -${BetterCommandRunnerFlags.verboseAbbr}, --${BetterCommandRunnerFlags.verbose}.', ); @@ -84,7 +84,8 @@ class BetterCommandRunner extends CommandRunner { abbr: BetterCommandRunnerFlags.verboseAbbr, defaultsTo: false, negatable: false, - help: 'Prints additional information useful for development. ' + help: + 'Prints additional information useful for development. ' 'Overrides --${BetterCommandRunnerFlags.quietAbbr}, --${BetterCommandRunnerFlags.quiet}.', ); @@ -140,31 +141,33 @@ class BetterCommandRunner extends CommandRunner { _onAnalyticsEvent = null; } - unawaited(Future(() async { - var command = topLevelResults.command; - if (command != null) { - // Command name can only be null for top level results. - // But since we are taking the name of a command from the top level - // results there should always be a name specified. - assert(command.name != null, 'Command name should never be null.'); - _onAnalyticsEvent?.call( - command.name ?? BetterCommandRunnerAnalyticsEvents.invalid, - ); - return; - } - - // Checks if the command is valid (i.e. no unexpected arguments). - // If there are unexpected arguments this will trigger a [UsageException] - // which will be caught in the try catch around the super.runCommand call. - // Therefore, this ensures that the help event is not sent for - // commands that are invalid. - // Note that there are other scenarios that also trigger a [UsageException] - // so the try/catch statement can't be fully compensated for handled here. - var noUnexpectedArgs = topLevelResults.rest.isEmpty; - if (noUnexpectedArgs) { - _onAnalyticsEvent?.call(BetterCommandRunnerAnalyticsEvents.help); - } - })); + unawaited( + Future(() async { + var command = topLevelResults.command; + if (command != null) { + // Command name can only be null for top level results. + // But since we are taking the name of a command from the top level + // results there should always be a name specified. + assert(command.name != null, 'Command name should never be null.'); + _onAnalyticsEvent?.call( + command.name ?? BetterCommandRunnerAnalyticsEvents.invalid, + ); + return; + } + + // Checks if the command is valid (i.e. no unexpected arguments). + // If there are unexpected arguments this will trigger a [UsageException] + // which will be caught in the try catch around the super.runCommand call. + // Therefore, this ensures that the help event is not sent for + // commands that are invalid. + // Note that there are other scenarios that also trigger a [UsageException] + // so the try/catch statement can't be fully compensated for handled here. + var noUnexpectedArgs = topLevelResults.rest.isEmpty; + if (noUnexpectedArgs) { + _onAnalyticsEvent?.call(BetterCommandRunnerAnalyticsEvents.help); + } + }), + ); await _onBeforeRunCommand?.call(this); diff --git a/lib/src/logger/helpers/progress.dart b/lib/src/logger/helpers/progress.dart index 8b5a4f5..0dd14c0 100644 --- a/lib/src/logger/helpers/progress.dart +++ b/lib/src/logger/helpers/progress.dart @@ -48,7 +48,7 @@ class ProgressAnimation { '⠦', '⠧', '⠇', - '⠏' + '⠏', ]; /// The list of animation frames. @@ -64,8 +64,8 @@ class Progress { this._message, this._stdout, { ProgressOptions options = const ProgressOptions(), - }) : _stopwatch = Stopwatch(), - _options = options { + }) : _stopwatch = Stopwatch(), + _options = options { _stopwatch ..reset() ..start(); @@ -121,7 +121,8 @@ class Progress { void fail([String? update]) { _timer?.cancel(); _write( - '$_clearLine${AnsiStyle.red.wrap('✗')} ${update ?? _message} $_time\n'); + '$_clearLine${AnsiStyle.red.wrap('✗')} ${update ?? _message} $_time\n', + ); _stopwatch.stop(); } diff --git a/lib/src/logger/logger.dart b/lib/src/logger/logger.dart index c168a3e..adda15e 100644 --- a/lib/src/logger/logger.dart +++ b/lib/src/logger/logger.dart @@ -13,29 +13,17 @@ abstract class Logger { /// Display debug [message] to the user. /// Commands should use this for information that is important for /// debugging purposes. - void debug( - String message, { - bool newParagraph, - LogType type, - }); + void debug(String message, {bool newParagraph, LogType type}); /// Display a normal [message] to the user. /// Command should use this as the standard communication channel for /// success, progress or information messages. - void info( - String message, { - bool newParagraph, - LogType type, - }); + void info(String message, {bool newParagraph, LogType type}); /// Display a warning [message] to the user. /// Commands should use this if they have important but not critical /// information for the user. - void warning( - String message, { - bool newParagraph, - LogType type, - }); + void warning(String message, {bool newParagraph, LogType type}); /// Display an error [message] to the user. /// Commands should use this if they want to inform a user that an error @@ -86,15 +74,7 @@ enum LogLevel { final String name; } -enum TextLogStyle { - init, - normal, - hint, - header, - bullet, - command, - success, -} +enum TextLogStyle { init, normal, hint, header, bullet, command, success } abstract class LogType { const LogType(); @@ -110,10 +90,7 @@ class RawLogType extends LogType { /// If [title] is set the box will have a title row. class BoxLogType extends LogType { final String? title; - const BoxLogType({ - this.title, - bool newParagraph = true, - }); + const BoxLogType({this.title, bool newParagraph = true}); } /// Abstract style console formatting. diff --git a/lib/src/logger/loggers/std_out_logger.dart b/lib/src/logger/loggers/std_out_logger.dart index 0ea4532..e6fee1f 100644 --- a/lib/src/logger/loggers/std_out_logger.dart +++ b/lib/src/logger/loggers/std_out_logger.dart @@ -17,7 +17,7 @@ class StdOutLogger extends Logger { final Map? _replacements; StdOutLogger(super.logLevel, {Map? replacements}) - : _replacements = replacements; + : _replacements = replacements; @override int? get wrapTextColumn => stdout.hasTerminal ? stdout.terminalColumns : null; @@ -36,13 +36,7 @@ class StdOutLogger extends Logger { type, ); } else { - _log( - message, - LogLevel.debug, - newParagraph, - type, - prefix: 'DEBUG: ', - ); + _log(message, LogLevel.debug, newParagraph, type, prefix: 'DEBUG: '); } } @@ -69,13 +63,7 @@ class StdOutLogger extends Logger { type, ); } else { - _log( - message, - LogLevel.warning, - newParagraph, - type, - prefix: 'WARNING: ', - ); + _log(message, LogLevel.warning, newParagraph, type, prefix: 'WARNING: '); } } @@ -87,20 +75,9 @@ class StdOutLogger extends Logger { LogType type = TextLogType.normal, }) { if (ansiSupported) { - _log( - AnsiStyle.red.wrap(message), - LogLevel.error, - newParagraph, - type, - ); + _log(AnsiStyle.red.wrap(message), LogLevel.error, newParagraph, type); } else { - _log( - message, - LogLevel.error, - newParagraph, - type, - prefix: 'ERROR: ', - ); + _log(message, LogLevel.error, newParagraph, type, prefix: 'ERROR: '); } if (stackTrace != null) { @@ -128,12 +105,7 @@ class StdOutLogger extends Logger { // Write an empty line before the progress message if a new paragraph is // requested. if (newParagraph) { - write( - '', - LogLevel.info, - newParagraph: false, - newLine: true, - ); + write('', LogLevel.info, newParagraph: false, newLine: true); } var progress = Progress(message, stdout); @@ -217,9 +189,9 @@ class StdOutLogger extends Logger { message = switch (_replacements) { null => message, Map replacements => replacements.entries.fold( - message, - (String acc, entry) => acc.replaceAll(entry.key, entry.value), - ), + message, + (String acc, entry) => acc.replaceAll(entry.key, entry.value), + ), }; _stopAnimationInProgress(); @@ -295,8 +267,9 @@ String _formatAsBox({ var maxTextWidthPerLine = wrapColumn - kEdges - kPaddingLeftRight * 2; var lines = _wrapText(message, maxTextWidthPerLine).split('\n'); var lineWidth = lines.map((String line) => line.length).toList(); - var maxColumnSize = - lineWidth.reduce((int currLen, int maxLen) => math.max(currLen, maxLen)); + var maxColumnSize = lineWidth.reduce( + (int currLen, int maxLen) => math.max(currLen, maxLen), + ); var textWidth = math.min(maxColumnSize, maxTextWidthPerLine); var textWithPaddingWidth = textWidth + kPaddingLeftRight * 2; diff --git a/lib/src/package_version/package_version.dart b/lib/src/package_version/package_version.dart index 0f0a9b1..7370f7f 100644 --- a/lib/src/package_version/package_version.dart +++ b/lib/src/package_version/package_version.dart @@ -20,7 +20,7 @@ abstract class PackageVersion { /// attempting to fetch the latest version again. static Future fetchLatestPackageVersion({ required Future Function(PackageVersionData versionArtefact) - storePackageVersionData, + storePackageVersionData, required Future Function() loadPackageVersionData, required Future Function() fetchLatestPackageVersion, }) async { @@ -47,7 +47,7 @@ abstract class PackageVersion { static Future _storePubDevVersion( Version? version, { required Future Function(PackageVersionData versionArtefact) - storePackageVersionData, + storePackageVersionData, }) async { PackageVersionData versionArtefact; if (version != null) { @@ -81,7 +81,7 @@ class PackageVersionData { ); Map toJson() => { - 'version': version.toString(), - 'valid_until': validUntil.millisecondsSinceEpoch - }; + 'version': version.toString(), + 'valid_until': validUntil.millisecondsSinceEpoch, + }; } diff --git a/lib/src/package_version/pub_api_client.dart b/lib/src/package_version/pub_api_client.dart index 8e947f4..0169556 100644 --- a/lib/src/package_version/pub_api_client.dart +++ b/lib/src/package_version/pub_api_client.dart @@ -13,8 +13,8 @@ class PubApiClient { PubApiClient({ http.Client? httpClient, requestTimeout = const Duration(seconds: 2), - }) : _pubClient = PubClient(client: httpClient), - _requestTimeout = requestTimeout; + }) : _pubClient = PubClient(client: httpClient), + _requestTimeout = requestTimeout; /// Tries to fetch the latest stable version, version does not include '-' or '+', /// for the package named [packageName]. diff --git a/lib/src/prompts/confirm.dart b/lib/src/prompts/confirm.dart index 69cb487..7c7c4cb 100644 --- a/lib/src/prompts/confirm.dart +++ b/lib/src/prompts/confirm.dart @@ -10,9 +10,10 @@ Future confirm( bool? defaultValue, required Logger logger, }) async { - var prompt = defaultValue == null - ? '[y/n]' - : defaultValue + var prompt = + defaultValue == null + ? '[y/n]' + : defaultValue ? '[Y/n]' : '[y/N]'; diff --git a/lib/src/prompts/select.dart b/lib/src/prompts/select.dart index ef622e6..4f955b9 100644 --- a/lib/src/prompts/select.dart +++ b/lib/src/prompts/select.dart @@ -22,8 +22,7 @@ Future