From 1f3c778aec2fc858140d9661720e2f65eb717fd1 Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Tue, 21 Mar 2023 21:51:59 -0500 Subject: [PATCH] fix(shorebird_cli): `shorebird init` always prompts for display name (#135) --- .../lib/src/commands/init_command.dart | 40 +++++++++++-------- .../test/src/commands/init_command_test.dart | 21 +++++----- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/packages/shorebird_cli/lib/src/commands/init_command.dart b/packages/shorebird_cli/lib/src/commands/init_command.dart index c617af37..0cf705ae 100644 --- a/packages/shorebird_cli/lib/src/commands/init_command.dart +++ b/packages/shorebird_cli/lib/src/commands/init_command.dart @@ -45,28 +45,33 @@ class InitCommand extends ShorebirdCommand return ExitCode.software.code; } - late final App app; + late final bool shorebirdYamlExists; try { - final pubspecYaml = getPubspecYaml()!; - app = await createApp(appName: pubspecYaml.name); - } catch (error) { - logger.err('$error'); + shorebirdYamlExists = hasShorebirdYaml; + } catch (_) { + logger.err('Error parsing "shorebird.yaml".'); return ExitCode.software.code; } - progress.update('Creating "shorebird.yaml"'); - - try { - if (hasShorebirdYaml) { - progress.update('"shorebird.yaml" already exists.'); - } else { - _addShorebirdYamlToProject(app.id); - progress.update('Generated a "shorebird.yaml".'); + late final String appId; + if (!shorebirdYamlExists) { + try { + final app = await createApp(); + appId = app.id; + } catch (error) { + logger.err('$error'); + return ExitCode.software.code; } - } catch (error) { - progress.fail(); - logger.err('Error creating "shorebird.yaml".\n$error'); - return ExitCode.software.code; + } else { + appId = getShorebirdYaml()!.appId; + } + + if (shorebirdYamlExists) { + progress.update('"shorebird.yaml" already exists.'); + } else { + progress.update('Creating "shorebird.yaml"'); + _addShorebirdYamlToProject(appId); + progress.update('Generated a "shorebird.yaml".'); } progress.update('Adding "shorebird.yaml" to "pubspec.yaml" assets'); @@ -84,6 +89,7 @@ class InitCommand extends ShorebirdCommand ${lightGreen.wrap('🐦 Shorebird initialized successfully!')} +✅ A shorebird app has been created. ✅ A "shorebird.yaml" has been created. ✅ The "pubspec.yaml" has been updated to include "shorebird.yaml" as an asset. diff --git a/packages/shorebird_cli/test/src/commands/init_command_test.dart b/packages/shorebird_cli/test/src/commands/init_command_test.dart index b1cc2194..8d8bedf1 100644 --- a/packages/shorebird_cli/test/src/commands/init_command_test.dart +++ b/packages/shorebird_cli/test/src/commands/init_command_test.dart @@ -29,6 +29,8 @@ name: $appName version: $version environment: sdk: ">=2.19.0 <3.0.0"'''; + const shorebirdYamlContent = ''' +app_id: $appId'''; const session = Session(apiKey: apiKey); late Auth auth; @@ -43,11 +45,12 @@ environment: logger = _MockLogger(); progress = _MockProgress(); command = InitCommand( - auth: auth, - buildCodePushClient: ({required String apiKey, Uri? hostedUri}) { - return codePushClient; - }, - logger: logger); + auth: auth, + buildCodePushClient: ({required String apiKey, Uri? hostedUri}) { + return codePushClient; + }, + logger: logger, + ); when(() => auth.currentSession).thenReturn(session); when( @@ -94,7 +97,6 @@ environment: File( p.join(tempDir.path, 'pubspec.yaml'), ).writeAsStringSync(pubspecYamlContent); - File(p.join(tempDir.path, 'shorebird.yaml')).createSync(); when( () => codePushClient.createApp(displayName: any(named: 'displayName')), ).thenThrow(error); @@ -102,6 +104,9 @@ environment: command.run, getCurrentDirectory: () => tempDir, ); + verify( + () => logger.prompt(any(), defaultValue: any(named: 'defaultValue')), + ).called(1); verify(() => logger.err('$error')).called(1); expect(exitCode, ExitCode.software.code); }); @@ -117,9 +122,7 @@ environment: getCurrentDirectory: () => tempDir, ); verify( - () => logger.err( - any(that: contains('Error creating "shorebird.yaml".')), - ), + () => logger.err('Error parsing "shorebird.yaml".'), ).called(1); expect(exitCode, ExitCode.software.code); });