Skip to content

Commit 789a94a

Browse files
refactor: moved all icons inside a separate file
* getInput now has option to change leading and trailing icons * prompts at various places are replaced by getInput * added tests: for getInput, snake_to_pascal_case,
1 parent 0a470ec commit 789a94a

14 files changed

+89
-54
lines changed

packages/gazelle_cli/lib/commands/codegen/codegen.dart

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:args/command_runner.dart';
44
import 'package:cli_spin/cli_spin.dart';
55
import 'package:path/path.dart';
66

7+
import '../../commons/entities/input_icons.dart';
78
import '../../commons/functions/load_project_configuration.dart';
89
import 'analyze_entities.dart';
910
import 'generate_client.dart';
@@ -62,7 +63,7 @@ class _CodegenModelsCommand extends Command {
6263
);
6364

6465
spinner.success(
65-
"Models generated 🚀\nAdd ${models.modelProvider.path.split("/").last} to your GazelleApp!",
66+
"Models generated ${InputIcons.rocket}${InputIcons.rocket}\nAdd ${models.modelProvider.path.split("/").last} to your GazelleApp!",
6667
);
6768
} on LoadProjectConfigurationGazelleNotFoundError catch (e) {
6869
spinner.fail(e.errorMessage);
@@ -114,7 +115,7 @@ class _CodegenClientCommand extends Command {
114115
);
115116

116117
spinner.success(
117-
"Client generated 🚀\nImport the client in your Dart frontend application!");
118+
"Client generated ${InputIcons.rocket}\nImport the client in your Dart frontend application!");
118119
} on LoadProjectConfigurationGazelleNotFoundError catch (e) {
119120
spinner.fail(e.errorMessage);
120121
exit(e.errorCode);

packages/gazelle_cli/lib/commands/codegen/generate_client.dart

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'package:dart_style/dart_style.dart';
66
import '../../commons/consts.dart';
77
import '../../commons/functions/get_latest_package_version.dart';
88
import '../../commons/functions/snake_to_pascal_case.dart';
9-
import '../../commons/functions/version.dart';
109

1110
String _getPubspecTemplate({
1211
required String gazelleClientVersion,

packages/gazelle_cli/lib/commands/create/create.dart

+16-20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:io';
33
import 'package:args/command_runner.dart';
44
import 'package:cli_spin/cli_spin.dart';
55

6+
import '../../commons/entities/input_icons.dart';
67
import '../../commons/functions/get_input.dart';
78
import '../../commons/functions/load_project_configuration.dart';
89
import 'create_handler.dart';
@@ -44,15 +45,13 @@ class _CreateProjectCommand extends Command {
4445

4546
@override
4647
void run() async {
47-
stdout.writeln("✨ What would you like to name your new project? 🚀");
48-
String? projectName = stdin.readLineSync();
49-
while (projectName == null || projectName == "") {
50-
stdout.writeln("⚠ Please provide a name for your project to proceed:");
51-
projectName = stdin.readLineSync();
52-
}
53-
stdout.writeln();
48+
final projectName = getInput(
49+
"What would you like to name your new project?",
50+
onEmpty: "Please provide a name for your project to proceed!",
51+
onValidated: (input) =>
52+
input.replaceAll(RegExp(r'\s+'), "_").toLowerCase(),
53+
);
5454

55-
projectName = projectName.replaceAll(RegExp(r'\s+'), "_").toLowerCase();
5655
final fullstack = argResults?.flag("flutter") != null ? true : false;
5756

5857
final spinner = CliSpin(
@@ -68,7 +67,7 @@ class _CreateProjectCommand extends Command {
6867
);
6968

7069
spinner.success(
71-
"$projectName project created 🚀\n💡To navigate to your project run \"cd $projectName\"\n💡Then, use \"gazelle run\" to execute it!",
70+
"$projectName project created ${InputIcons.rocket}\n💡To navigate to your project run \"cd $projectName\"\n💡Then, use \"gazelle run\" to execute it!",
7271
);
7372
} on CreateProjectError catch (e) {
7473
spinner.fail(e.message);
@@ -96,15 +95,12 @@ class _CreateRouteCommand extends Command {
9695
CliSpin spinner = CliSpin();
9796
try {
9897
await loadProjectConfiguration();
99-
stdout.writeln("✨ What would you like to name your new route? 🚀");
100-
String? routeName = stdin.readLineSync();
101-
while (routeName == null || routeName == "") {
102-
stdout.writeln("⚠ Please provide a name for your route to proceed:");
103-
routeName = stdin.readLineSync();
104-
}
105-
stdout.writeln();
106-
107-
routeName = routeName.replaceAll(RegExp(r'\s+'), "_").toLowerCase();
98+
final routeName = getInput(
99+
"What would you like to name your new route?",
100+
onEmpty: "Please provide a name for your route to proceed!",
101+
onValidated: (input) =>
102+
input.replaceAll(RegExp(r'\s+'), "_").toLowerCase(),
103+
);
108104

109105
spinner = CliSpin(
110106
text: "Creating $routeName route...",
@@ -120,7 +116,7 @@ class _CreateRouteCommand extends Command {
120116
);
121117

122118
spinner.success(
123-
"$routeName route created 🚀",
119+
"$routeName route created ${InputIcons.rocket}",
124120
);
125121
} on LoadProjectConfigurationGazelleNotFoundError catch (e) {
126122
spinner.fail(e.errorMessage);
@@ -198,7 +194,7 @@ class _CreatHandlerCommand extends Command {
198194
path: path,
199195
);
200196

201-
spinner.success("$handlerName handler created 🚀");
197+
spinner.success("$handlerName handler created ${InputIcons.rocket}");
202198
} on LoadProjectConfigurationGazelleNotFoundError catch (e) {
203199
spinner.fail(e.errorMessage);
204200
exit(e.errorCode);

packages/gazelle_cli/lib/commands/create/create_models.dart

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import 'dart:io';
33
import '../../commons/consts.dart';
44
import '../../commons/functions/get_latest_package_version.dart';
55
import '../../commons/functions/snake_to_pascal_case.dart';
6-
import '../../commons/functions/version.dart';
76

87
String _getPubspecTemplate(String gazelleSerializationVersion) => """
98
name: models

packages/gazelle_cli/lib/commands/create/create_server.dart

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import 'package:dart_style/dart_style.dart';
55
import '../../commons/consts.dart';
66
import '../../commons/functions/get_latest_package_version.dart';
77
import '../../commons/functions/snake_to_pascal_case.dart';
8-
import '../../commons/functions/version.dart';
98
import 'create_route.dart';
109

1110
const _gitignore = """

packages/gazelle_cli/lib/commands/delete/delete.dart

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import 'dart:io';
2+
23
import 'package:args/command_runner.dart';
34
import 'package:cli_spin/cli_spin.dart';
5+
6+
import '../../commons/entities/input_icons.dart';
47
import '../../commons/entities/project_configuration.dart';
5-
import '../../commons/functions/confirmation.dart';
8+
import '../../commons/functions/get_input.dart';
69
import '../../commons/functions/load_project_configuration.dart';
710
import 'delete_project.dart';
811

@@ -43,9 +46,15 @@ class DeleteCommand extends Command {
4346
spinner.fail(e.toString());
4447
exit(2);
4548
}
46-
final answer = getConfirmation(
47-
'\nAre you sure you want to delete the project ${projectConfiguration.name}');
48-
if (!answer) {
49+
final answer = getInput(
50+
'\nAre you sure you want to delete the project ${projectConfiguration.name}? (y/n)',
51+
onValidated: (input) =>
52+
['yes', 'y'].contains(input.toLowerCase()) ? 'y' : 'n',
53+
leading: InputIcons.warning,
54+
trailing: InputIcons.none,
55+
defaultValue: 'n',
56+
);
57+
if (answer == 'y') {
4958
spinner.fail("Project deletion aborted.");
5059
return;
5160
}

packages/gazelle_cli/lib/commands/run/run_project.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import 'dart:convert';
22
import 'dart:io';
33

4+
import '../../commons/consts.dart';
45
import '../../commons/entities/stdin_broadcast.dart';
5-
import '../../commons/functions/version.dart';
66

77
String _getPubspecTemplate(String projectName) => """
88
name: temp_project

packages/gazelle_cli/lib/commons/consts.dart

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:io';
2+
13
/// Gazelle Core package name.
24
const gazelleCorePackageName = "gazelle_core";
35

@@ -6,3 +8,6 @@ const gazelleSerializationPackageName = "gazelle_serialization";
68

79
/// Gazelle Client package name.
810
const gazelleClientPackageName = "gazelle_client";
11+
12+
/// Returns the Currently Installed Dart SDK version.
13+
final String dartSdkVersion = Platform.version.split(' ').first;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// A class that contains leading icons to display before messages.
2+
class InputIcons {
3+
/// No Icon
4+
static const none = "";
5+
6+
/// ✨ Icon
7+
static const star = "✨";
8+
9+
/// 🚀 Icon
10+
static const rocket = "🚀";
11+
12+
/// ⚠ Icon
13+
static const warning = "⚠";
14+
15+
/// ❌ Icon
16+
static const error = "❌";
17+
}

packages/gazelle_cli/lib/commons/functions/confirmation.dart

-13
This file was deleted.

packages/gazelle_cli/lib/commons/functions/get_input.dart

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'dart:io';
22

3+
import '../entities/input_icons.dart';
4+
35
/// Gets user input from the command line.
46
String getInput(
57
/// The message to display to the user.
@@ -17,11 +19,17 @@ String getInput(
1719
/// This function is called just before returning the input.
1820
/// You can use this function to modify the input before returning it.
1921
String Function(String input)? onValidated,
22+
23+
/// The leading icon to display before the message.
24+
String leading = InputIcons.star,
25+
26+
/// The trailing icon to display after the message.
27+
String trailing = InputIcons.rocket,
2028
}) {
2129
String? input;
2230
while (true) {
2331
stdout.write(
24-
"✨ $message 🚀 ${defaultValue != null ? "($defaultValue)" : ""}: ");
32+
"${leading.isEmpty ? '' : '$leading '}$message ${trailing.isEmpty ? '' : '$trailing '}${defaultValue != null ? "($defaultValue)" : ""}: ");
2533
input = stdin.readLineSync();
2634
if (input == null) {
2735
print("Operation cancelled by the user.");
@@ -31,12 +39,11 @@ String getInput(
3139
input = defaultValue;
3240
}
3341
String? validationError = validator?.call(input);
34-
if (input == "" || validationError != null) {
35-
stdout.writeln(" ${validationError ?? onEmpty}");
42+
if (input.isEmpty || validationError != null) {
43+
stdout.writeln("${InputIcons.warning} ${validationError ?? onEmpty}");
3644
} else {
3745
break;
3846
}
3947
}
40-
input = onValidated?.call(input) ?? input;
41-
return input;
48+
return onValidated?.call(input) ?? input;
4249
}

packages/gazelle_cli/lib/commons/functions/version.dart

-4
This file was deleted.

packages/gazelle_cli/test/commons/functions/get_latest_package_version.dart_test.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ void main() {
1313
(response) => jsonDecode(response.body)['latest']['version'],
1414
);
1515

16-
final version = await getLatestPackageVersion('http');
16+
final expectedVersion = await getLatestPackageVersion('http');
1717

18-
expect(version, equals(originalVersion));
18+
expect(expectedVersion, equals(originalVersion));
1919
});
2020
});
2121
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'package:gazelle_cli/commons/functions/snake_to_pascal_case.dart';
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
test("snake to pascal case", () {
6+
final snakeCases = [
7+
"snake_case",
8+
"_snake_case",
9+
"snake_case_",
10+
"__snake_case__",
11+
"SNAKE_cAsE",
12+
];
13+
14+
final pascalCase = "SnakeCase";
15+
16+
for (final snakeCase in snakeCases) {
17+
expect(snakeToPascalCase(snakeCase), pascalCase);
18+
}
19+
});
20+
}

0 commit comments

Comments
 (0)