Skip to content

Commit 3766e7f

Browse files
authored
fix: bad state error on no argument (#7)
* fix(cli): notify when new version available - Avoid `Bad state: No element` when no argument passed to the CLI - Refactor of the code for a cleaner implementation * fix(cli): add default value to version Added default value to version on getCurrentVersion to avoid LateInitializationError when no package found.
1 parent 2e75b70 commit 3766e7f

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

bin/stacked.dart

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ Future<void> main(List<String> arguments) async {
5454

5555
if (_handleAnalytics(argResults)) exit(0);
5656

57-
// Ignore notify version for compile and update commands
58-
if (arguments.first != 'compile' && arguments.first != 'update') {
59-
await _notifyNewVersionAvailable();
60-
}
57+
await _notifyNewVersionAvailable(arguments: arguments);
6158

6259
runner.run(arguments);
6360
} on InvalidStackedStructureException catch (e) {
@@ -119,7 +116,17 @@ Future<void> _handleFirstRun() async {
119116
analyticsService.enable(opt == 'y' || opt == 'yes');
120117
}
121118

122-
Future<void> _notifyNewVersionAvailable() async {
119+
/// Notifies new version of Stacked CLI is available
120+
Future<void> _notifyNewVersionAvailable({
121+
List<String> arguments = const [],
122+
List<String> ignored = const ['compile', 'update'],
123+
}) async {
124+
if (arguments.isEmpty) return;
125+
126+
for (var arg in ignored) {
127+
if (arguments.first == arg) return;
128+
}
129+
123130
if (await locator<PubService>().hasLatestVersion()) return;
124131

125132
stdout.writeln('''

lib/src/services/pub_service.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class PubService {
1414

1515
/// Returns current `stacked_cli` version installed on the system.
1616
Future<String> getCurrentVersion() async {
17-
late String version;
17+
String version = 'Current Version Not Available';
1818

1919
final packages = await _processService.runPubGlobalList();
2020
for (var package in packages) {

0 commit comments

Comments
 (0)