Skip to content

Commit

Permalink
feat: support for melos command within script steps (#683)
Browse files Browse the repository at this point in the history
* chore: add command recognition and script building for script steps

* chore: add test for command script steps support

* chore: update _isStepACommand method

In order to split the step by spaces to separate the command from its flags/arguments

* chore: include test for script calling commands with args

* chore: update scripts mdx file

* chore: Update the _buildScriptCommand method

In order to prioritize script names that are identical to melos commands. Additionally, add a test for this scenario.

* test: fix test in Windows environments

* test: fix test in Windows and Linux environments
  • Loading branch information
jessicatarra authored Mar 29, 2024
1 parent 8116d21 commit a1da197
Show file tree
Hide file tree
Showing 3 changed files with 357 additions and 18 deletions.
21 changes: 6 additions & 15 deletions docs/configuration/scripts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,19 @@ The command to execute.

## steps

Enables the combination of multiple scripts within a single script definition.
In the example below, the 'pre-commit' is configured to sequentially invoke the
format and analyze scripts.
Enables the combination of multiple scripts within a single script definition
for complex workflows. In the example below, the pre-commit script is
configured to sequentially invoke a simple command echo 'hello world',
followed by Melos commands: format and analyze, each with specific arguments.

```yaml
scripts:
pre-commit:
description: pre-commit git hook script
steps:
- echo 'hello world'
- format
- analyze
format:
description: Format Dart code.
run: dart format .
exec:
concurrency: 5
analyze:
description: Analyze Dart code
run: dart analyze .
exec:
concurrency: 5
- format --output none --set-exit-if-changed
- analyze --fatal-infos
```

Note: When utilizing the `steps`, it's important to understand that options
Expand Down
34 changes: 31 additions & 3 deletions packages/melos/lib/src/commands/run.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,44 @@ mixin _RunMixin on _Melos {
await _executeScriptSteps(steps, scripts, script, environment);
}

/// Checks if the given [step] is a recognized Melos command.
bool _isStepACommand(String step) {
// Split the step by spaces to separate the command from its flags/arguments.
final command = step.split(' ')[0];

const melosCommands = {
'analyze',
'format',
'bs',
'bootstrap',
'clean',
'list',
'publish',
};

return melosCommands.contains(command);
}

String _buildScriptCommand(String step, Scripts scripts) {
if (scripts.containsKey(step)) {
return 'melos run $step';
}

if (_isStepACommand(step)) {
return 'melos $step';
}

return step;
}

Future<void> _executeScriptSteps(
List<String> steps,
Scripts scripts,
Script script,
Map<String, String> environment,
) async {
for (final step in steps) {
final scriptCommand =
scripts.containsKey(step) ? 'melos run $step' : step;
final scriptCommand = _buildScriptCommand(step, scripts);

final scriptSourceCode = targetStyle(
step.withoutTrailing('\n'),
Expand Down Expand Up @@ -284,7 +313,6 @@ mixin _RunMixin on _Melos {

if (exitCode != 0) {
resultLogger.child(failedLabel);
throw ScriptException._(script.name);
} else {
resultLogger.child(successLabel);
}
Expand Down
Loading

0 comments on commit a1da197

Please sign in to comment.