Skip to content

Commit 183e0ef

Browse files
authored
feat: add --json flag to list scripts in json format (#953)
<!-- Thanks for contributing! Provide a description of your changes below and a general summary in the title Please look at the following checklist to ensure that your PR can be accepted quickly: --> Closes #435 ## Description Adds a --json flag to the run command that makes it possible to list all scripts defined in the melos workspace config in json format. This flag only works in combination with the --list flag and if no script name is defined. Added --list and --json to the documentation ## Type of Change <!--- Put an `x` in all the boxes that apply: --> - [x] ✨ `feat` -- New feature (non-breaking change which adds functionality) - [ ] 🛠️ `fix` -- Bug fix (non-breaking change which fixes an issue) - [ ] ❌ `!` -- Breaking change (fix or feature that would cause existing functionality to change) - [ ] 🧹 `refactor` -- Code refactor - [ ] ✅ `ci` -- Build configuration change - [x] 📝 `docs` -- Documentation - [ ] 🗑️ `chore` -- Chore
1 parent 9fa4bf5 commit 183e0ef

File tree

4 files changed

+102
-3
lines changed

4 files changed

+102
-3
lines changed

docs/commands/run.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,18 @@ applied.
2121
```bash
2222
melos run <name> --no-select
2323
```
24+
25+
## --list
26+
27+
Displays all scripts defined in the Melos workspace.
28+
29+
```bash
30+
melos run --list
31+
```
32+
33+
Use `--json` to output the list in JSON format.
34+
35+
```bash
36+
melos run --list --json
37+
```
38+

packages/melos/lib/src/command_runner/run.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ class RunCommand extends MelosCommand {
1616
negatable: false,
1717
help: 'Lists all scripts defined in the melos.yaml config file.',
1818
);
19+
20+
argParser.addFlag(
21+
'json',
22+
negatable: false,
23+
help:
24+
'Lists all scripts defined in the melos.yaml config file with '
25+
'description in json format.',
26+
);
1927
}
2028

2129
@override
@@ -38,6 +46,7 @@ class RunCommand extends MelosCommand {
3846
? argResults!.rest.skip(1).toList()
3947
: <String>[];
4048
final listScripts = argResults!['list'] as bool;
49+
final listScriptsAsJson = argResults!['json'] as bool;
4150

4251
try {
4352
return await melos.run(
@@ -46,6 +55,7 @@ class RunCommand extends MelosCommand {
4655
noSelect: noSelect,
4756
extraArgs: extraArgs,
4857
listScripts: listScripts,
58+
listScriptsAsJson: listScriptsAsJson,
4959
);
5060
} on NoPackageFoundScriptException catch (err) {
5161
logger.warning(err.toString(), label: false);

packages/melos/lib/src/commands/run.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ mixin _RunMixin on _Melos {
77
String? scriptName,
88
bool noSelect = false,
99
bool listScripts = false,
10+
bool listScriptsAsJson = false,
1011
List<String> extraArgs = const [],
1112
}) async {
1213
if (listScripts && scriptName == null) {
13-
logger.command('melos run --list');
14-
logger.newLine();
15-
config.scripts.forEach((_, script) => logger.log(script.name));
14+
_handleListScripts(listAsJson: listScriptsAsJson);
1615
return;
1716
}
17+
1818
if (config.scripts.keys.isEmpty) {
1919
throw NoScriptException._();
2020
}
@@ -91,6 +91,18 @@ mixin _RunMixin on _Melos {
9191
}
9292
}
9393

94+
void _handleListScripts({bool listAsJson = false}) {
95+
if (listAsJson) {
96+
logger.command('melos run --list --json');
97+
logger.newLine();
98+
logger.log(json.encode(config.scripts));
99+
} else {
100+
logger.command('melos run --list');
101+
logger.newLine();
102+
config.scripts.forEach((_, script) => logger.log(script.name));
103+
}
104+
}
105+
94106
/// Detects recursive script calls within the provided [script].
95107
///
96108
/// This method recursively traverses the steps of the script to check

packages/melos/test/commands/run_test.dart

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,5 +968,67 @@ SUCCESS
968968
);
969969
},
970970
);
971+
972+
test(
973+
'verifies that the --list flag in combination with '
974+
'--json lists all scripts in the config in json format',
975+
() async {
976+
final workspaceDir = await createTemporaryWorkspace(
977+
configBuilder: (path) => MelosWorkspaceConfig(
978+
path: path,
979+
name: 'test_package',
980+
packages: [
981+
createGlob('packages/**', currentDirectoryPath: path),
982+
],
983+
scripts: const Scripts({
984+
'test_script_1': Script(
985+
name: 'test_script',
986+
steps: [
987+
'absolute_bogus_command',
988+
'echo "test_script_2"',
989+
],
990+
),
991+
'test_script_2': Script(
992+
name: 'test_script',
993+
steps: [
994+
'absolute_bogus_command',
995+
'echo "test_script_2"',
996+
],
997+
),
998+
'test_script_3': Script(
999+
name: 'test_script',
1000+
steps: [
1001+
'absolute_bogus_command',
1002+
'echo "test_script_2"',
1003+
],
1004+
),
1005+
}),
1006+
),
1007+
workspacePackages: ['a'],
1008+
);
1009+
1010+
await createProject(workspaceDir, Pubspec('a'));
1011+
1012+
final logger = TestLogger();
1013+
final config = await MelosWorkspaceConfig.fromWorkspaceRoot(
1014+
workspaceDir,
1015+
);
1016+
final melos = Melos(
1017+
logger: logger,
1018+
config: config,
1019+
);
1020+
1021+
await melos.run(listScripts: true, listScriptsAsJson: true);
1022+
1023+
expect(
1024+
logger.output.normalizeLines().split('\n'),
1025+
containsAllInOrder([
1026+
'melos run --list --json',
1027+
'',
1028+
r'{"test_script_1":{"name":"test_script_1","run":null,"steps":["absolute_bogus_command","echo \"test_script_2\""]},"test_script_2":{"name":"test_script_2","run":null,"steps":["absolute_bogus_command","echo \"test_script_2\""]},"test_script_3":{"name":"test_script_3","run":null,"steps":["absolute_bogus_command","echo \"test_script_2\""]}}',
1029+
]),
1030+
);
1031+
},
1032+
);
9711033
});
9721034
}

0 commit comments

Comments
 (0)