Skip to content

Commit

Permalink
feat: use pnpm and git after generation
Browse files Browse the repository at this point in the history
  • Loading branch information
KernelPanic92 committed May 29, 2024
1 parent dcd8d48 commit 7b00f3d
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 25 deletions.
34 changes: 34 additions & 0 deletions lib/src/models/addon_command.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import 'package:devmy_cli/src/models/brick_command.dart';
import 'package:devmy_cli/src/models/brick_command_with_questions.dart';
import 'package:mason/mason.dart';

import '../constants/brick_variables.dart';

class AddonCommand extends BrickCommandWithQuestions {
AddonCommand({
Expand All @@ -9,4 +13,34 @@ class AddonCommand extends BrickCommandWithQuestions {
super.questions = const [],
super.aliases = const [],
});

@override
String getCommitMessage({
required Map<String, dynamic> environment,
required BrickCommand brickCommand,
}) {
String name = '';
if (environment.containsKey(kBrickApplicationNameEnvironmentVariable)) {
name = (environment[kBrickApplicationNameEnvironmentVariable] as String)
.paramCase;
}

if (environment.containsKey(kBrickWorkspaceNameEnvironmentVariable)) {
name = (environment[kBrickWorkspaceNameEnvironmentVariable] as String)
.paramCase;
}

if (environment.containsKey(kBrickLibraryNameEnvironmentVariable)) {
name = (environment[kBrickLibraryNameEnvironmentVariable] as String)
.paramCase;
}

if (name.isEmpty) {
throw UnimplementedError(
'invalid command to run an addon: missing brick environment name',
);
}

return 'chore($name): added ${brickCommand.name}';
}
}
13 changes: 13 additions & 0 deletions lib/src/models/application_command.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io';
import 'package:devmy_cli/src/models/brick_command.dart';
import 'package:devmy_cli/src/models/brick_command_with_questions.dart';
import 'package:mason/mason.dart';

Expand Down Expand Up @@ -36,4 +37,16 @@ class ApplicationCommand extends BrickCommandWithQuestions {

return directory;
}

@override
String getCommitMessage({
required Map<String, dynamic> environment,
required BrickCommand brickCommand,
}) {
final applicationName =
(environment[kBrickApplicationNameEnvironmentVariable] as String)
.paramCase;

return 'chore($applicationName): added ${brickCommand.name}';
}
}
83 changes: 58 additions & 25 deletions lib/src/models/brick_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';

import 'package:args/command_runner.dart';
import 'package:chalkdart/chalk.dart';
import 'package:devmy_cli/devmy_cli.dart';
import 'package:devmy_cli/src/models/brick_context.dart';
import 'package:devmy_cli/src/utilities/utilities.dart';
import 'package:interact/interact.dart';
Expand All @@ -29,31 +30,6 @@ abstract class BrickCommand extends Command<void> {
this.aliases = const [],
});

@override
FutureOr<void> run() async {
final brickContext = await loadBrickContext(brick);

final environment = await loadEnvironment();

print('prompt command variables');
await promptBundleVariables(
environment: environment,
bundle: brickContext.bundle,
);
print('prompt command variables');
await promptCommandVariables(environment: environment);

Directory workingDirectory =
await getWorkingDirectory(environment: environment);

await runBrick(
workingDirectory: workingDirectory,
brickContext: brickContext,
environment: environment,
brickCommand: this,
);
}

FutureOr<Directory> getWorkingDirectory({
required Map<String, dynamic> environment,
}) {
Expand Down Expand Up @@ -97,6 +73,13 @@ abstract class BrickCommand extends Command<void> {
vars: environment,
workingDirectory: workingDirectory.path,
);

await runPnpmInstall();

await _commitChanges(
brickCommand: brickCommand,
environment: environment,
);
}

FutureOr<BrickContext> loadBrickContext(GitPath brickPath) async {
Expand Down Expand Up @@ -148,4 +131,54 @@ abstract class BrickCommand extends Command<void> {
environment.update(entry.key, (_) => entry.value);
}
}

String getCommitMessage({
required Map<String, dynamic> environment,
required BrickCommand brickCommand,
});

Future<void> runPnpmInstall() async {
print('📦 Running pnpm i');
try {
await Process.run('pnpm', ['i']);
print(chalk.green('📦 pnpm configured successfully 🚀'));
} catch (_) {
print(chalk.yellowBright('⚠️ failed pnpm i'));
}
}

Future<void> _commitChanges({
required Map<String, dynamic> environment,
required BrickCommand brickCommand,
}) async {
print('📚 Staging initial files...');
Directory cwd = Directory.current;
String? workspaceName = environment[kBrickWorkspaceNameEnvironmentVariable];

if (workspaceName != null) {
cwd = Directory.fromUri(cwd.uri.resolve(workspaceName.paramCase));
}

await Process.run(
'git',
['add', '.'],
workingDirectory: cwd.path,
);

final commitMessage = brickCommand.getCommitMessage(
environment: environment,
brickCommand: brickCommand,
);

print(
'📚 Committing "$commitMessage"...',
);

await Process.run(
'git',
['commit', '-m', '"$commitMessage"'],
);

print(chalk.green('📚 Git commited successfully! 🚀'));
}
}
11 changes: 11 additions & 0 deletions lib/src/models/library_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,15 @@ class LibraryCommand extends BrickCommand {

return directory;
}

@override
String getCommitMessage({
required Map<String, dynamic> environment,
required BrickCommand brickCommand,
}) {
final libraryName =
(environment[kBrickLibraryNameEnvironmentVariable] as String).paramCase;

return 'chore($libraryName): added ${brickCommand.name}';
}
}
37 changes: 37 additions & 0 deletions lib/src/models/new_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,31 @@ class NewCommand extends BrickCommand {
brick: brick,
);

@override
FutureOr<void> run() async {
final brickContext = await loadBrickContext(brick);

final environment = await loadEnvironment();

print('prompt command variables');
await promptBundleVariables(
environment: environment,
bundle: brickContext.bundle,
);
print('prompt command variables');
await promptCommandVariables(environment: environment);

Directory workingDirectory =
await getWorkingDirectory(environment: environment);

await runBrick(
workingDirectory: workingDirectory,
brickContext: brickContext,
environment: environment,
brickCommand: this,
);
}

@override
FutureOr<Directory> getWorkingDirectory({
required Map<String, dynamic> environment,
Expand All @@ -39,4 +64,16 @@ class NewCommand extends BrickCommand {

return directory;
}

@override
String getCommitMessage({
required Map<String, dynamic> environment,
required BrickCommand brickCommand,
}) {
final workspaceName =
(environment[kBrickWorkspaceNameEnvironmentVariable] as String)
.paramCase;

return 'chore($workspaceName): added ${brickCommand.name}';
}
}

0 comments on commit 7b00f3d

Please sign in to comment.