Skip to content

Commit

Permalink
feat: validate "melos exec" is not used in "run" along with "exec" (#438
Browse files Browse the repository at this point in the history
)

Co-authored-by: Gabriel Terwesten <gabriel@terwesten.net>
  • Loading branch information
BenVercammen and blaugold authored Jan 3, 2023
1 parent 70b0c73 commit 628f798
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/melos/lib/src/scripts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import 'common/utils.dart';
import 'common/validation.dart';
import 'package.dart';

// https://regex101.com/r/44dzaz/1
final _leadingMelosExecRegExp = RegExp(r'^\s*melos\s+exec');

/// Scripts to be executed before/after a melos command.
class LifecycleHook {
LifecycleHook._({required this.pre, required this.post});
Expand Down Expand Up @@ -83,6 +86,14 @@ class Scripts extends MapView<String, Script> {
);
}

/// Validates the scripts. Throws a [MelosConfigException] if any script is
/// invalid.
void validate() {
for (final script in values) {
script.validate();
}
}

Map<Object?, Object?> toJson() {
return {
for (final entry in entries) entry.key: entry.value.toJson(),
Expand Down Expand Up @@ -300,6 +311,21 @@ class Script {
return run;
}

/// Validates the script. Throws a [MelosConfigException] if the script is
/// invalid.
void validate() {
if (exec != null && run.startsWith(_leadingMelosExecRegExp)) {
throw MelosConfigException(
'Do not use "melos exec" in "run" when also providing options in '
'"exec". In this case the script in "run" is already being executed by '
'"melos exec".\n'
'For more information, see https://melos.invertase.dev/configuration/scripts#scriptsexec.\n'
'\n'
' run: $run',
);
}
}

Map<Object?, Object?> toJson() {
return {
'name': name,
Expand Down
2 changes: 2 additions & 0 deletions packages/melos/lib/src/workspace_configs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,8 @@ You must have one of the following to be a valid Melos workspace:
'repository must be specified if commands/version/linkToCommits is true',
);
}

scripts.validate();
}

/// Validates the physical workspace on the file system.
Expand Down
17 changes: 17 additions & 0 deletions packages/melos/test/workspace_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,23 @@ void main() {
throwsA(isA<MelosConfigException>()),
);
});

test('throws when using "melos exec" in "run" and specifying "exec"', () {
expect(
() => Scripts.fromYaml(
createYamlMap({
'a': {
'run': 'melos exec a',
'exec': {
'concurrency': 1,
},
},
}),
workspacePath: testWorkspacePath,
).validate(),
throwsA(isA<MelosConfigException>()),
);
});
});
});

Expand Down

0 comments on commit 628f798

Please sign in to comment.