Skip to content

Commit

Permalink
refactor: refactor script execution and step validation
Browse files Browse the repository at this point in the history
- Refactor `_ValidateScriptExecOptions` to its own method
- Add `_checkStepSeparation` method for step validation
  • Loading branch information
jessicatarra committed May 13, 2024
1 parent 6f381e9 commit 4a8f856
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions packages/melos/lib/src/commands/run.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ mixin _RunMixin on _Melos {
}

if (script.steps != null && script.steps!.isNotEmpty) {
if (script.exec != null) {
throw ScriptExecOptionsException._(
scriptName,
);
}
_validateScriptExecOptions(script, scriptName);

_detectRecursiveScriptCalls(script);

_checkStepSeparation(script);

await _runMultipleScripts(
script,
global: global,
Expand Down Expand Up @@ -70,6 +68,14 @@ mixin _RunMixin on _Melos {
resultLogger.child(successLabel);
}

void _validateScriptExecOptions(Script script, String scriptName) {
if (script.exec != null) {
throw ScriptExecOptionsException._(
scriptName,
);
}
}

/// Detects recursive script calls within the provided [script].
///
/// This method recursively traverses the steps of the script to check
Expand Down Expand Up @@ -326,6 +332,14 @@ mixin _RunMixin on _Melos {
}
logger.newLine();
}

void _checkStepSeparation(Script script) {
final invalidSteps =
script.steps!.where((step) => step.contains('&&')).toList();
if (invalidSteps.isNotEmpty) {
throw InvalidStepFormatException._(invalidSteps.join(', '));
}
}
}

class NoPackageFoundScriptException implements MelosException {
Expand Down Expand Up @@ -421,3 +435,16 @@ class RecursiveScriptCallException implements MelosException {
'loop.';
}
}

class InvalidStepFormatException implements MelosException {
InvalidStepFormatException._(this.stepName);

final String stepName;

@override
String toString() {
return 'InvalidStepFormatException: The step $stepName'
'contains "&&". Split it into separate steps to enhance clarity and '
'error handling.';
}
}

0 comments on commit 4a8f856

Please sign in to comment.