Skip to content

Commit f9e936f

Browse files
committed
feat: colorize thrown error
1 parent 19a9f0a commit f9e936f

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

bin/script_runner.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import 'package:script_runner/src/base.dart';
2+
import 'package:script_runner/src/utils.dart';
23

34
/// Main entrypoint for CMD script runner.
45
Future<void> main(List<String> args) async {
56
final scriptCmd = args.first;
67
final scriptArgs = args.sublist(1);
7-
return runScript(scriptCmd, scriptArgs);
8+
try {
9+
print('Running script: $scriptCmd $scriptArgs');
10+
await runScript(scriptCmd, scriptArgs);
11+
} catch (e) {
12+
printColor('$e', [TerminalColor.red]);
13+
}
814
}
15+

lib/src/runnable_script.dart

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,24 @@ class RunnableScript {
6565
}) : _fileSystem = fileSystem ?? LocalFileSystem();
6666

6767
/// Generate a runnable script from a yaml loaded map as defined in the config.
68-
factory RunnableScript.fromYamlMap(yaml.YamlMap map, {FileSystem? fileSystem}) {
68+
factory RunnableScript.fromYamlMap(yaml.YamlMap map,
69+
{FileSystem? fileSystem}) {
6970
final out = <String, dynamic>{};
7071

7172
if (map['name'] == null && map.keys.length == 1) {
7273
out['name'] = map.keys.first;
7374
out['cmd'] = map.values.first;
7475
} else {
7576
out.addAll(map.cast<String, dynamic>());
76-
out['args'] = (map['args'] as yaml.YamlList?)?.map((e) => e.toString()).toList();
77+
out['args'] =
78+
(map['args'] as yaml.YamlList?)?.map((e) => e.toString()).toList();
7779
out['env'] = (map['env'] as yaml.YamlMap?)?.cast<String, String>();
7880
}
7981
try {
8082
return RunnableScript.fromMap(out, fileSystem: fileSystem);
8183
} catch (e) {
82-
throw StateError('Failed to parse script, arguments: $map, $fileSystem. Error: $e');
84+
throw StateError(
85+
'Failed to parse script, arguments: $map, $fileSystem. Error: $e');
8386
}
8487
}
8588

@@ -109,7 +112,8 @@ class RunnableScript {
109112
appendNewline: appendNewline,
110113
);
111114
} catch (e) {
112-
throw StateError('Failed to parse script, arguments: $map, $fileSystem. Error: $e');
115+
throw StateError(
116+
'Failed to parse script, arguments: $map, $fileSystem. Error: $e');
113117
}
114118
}
115119

@@ -148,6 +152,8 @@ class RunnableScript {
148152
);
149153
throw e;
150154
}
155+
} catch (e) {
156+
rethrow;
151157
} finally {
152158
await _fileSystem.file(scrPath).delete();
153159
}
@@ -169,7 +175,8 @@ class RunnableScript {
169175
return exitCode;
170176
}
171177

172-
String _getScriptPath() => _fileSystem.path.join(_fileSystem.systemTempDirectory.path, 'script_runner_$name.sh');
178+
String _getScriptPath() => _fileSystem.path
179+
.join(_fileSystem.systemTempDirectory.path, 'script_runner_$name.sh');
173180

174181
String _getScriptContents(
175182
ScriptRunnerConfig config, {
@@ -185,8 +192,12 @@ class RunnableScript {
185192
].join('\n');
186193
case OS.linux:
187194
case OS.macos:
188-
return [...preloadScripts.map((e) => "[[ ! \$(which ${e.name}) ]] && alias ${e.name}='scr ${e.name}'"), script]
189-
.join('\n');
195+
return [
196+
...preloadScripts.map((e) =>
197+
"[[ ! \$(which ${e.name}) ]] && alias ${e.name}='scr ${e.name}'"),
198+
script
199+
].join('\n');
190200
}
191201
}
192202
}
203+

lib/src/utils.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ List<String> chunks(
6060
final chunks = <String>[];
6161
var chunk = '';
6262
for (final word in words) {
63+
// if (chunk.contains('\n')) {
64+
// final lines = chunk.split('\n');
65+
// for (var i = 0; i < lines.length - 1; i++) {
66+
// chunks.add(wrapLine(lines[i]));
67+
// }
68+
// chunk = '';
69+
// }
6370
final chunkLength = stripColors ? stripColor(chunk).length : chunk.length;
6471
final wordLength = stripColors ? stripColor(word).length : word.length;
6572
if (chunkLength + wordLength > maxLen) {
@@ -127,3 +134,4 @@ class TerminalColor {
127134
static const TerminalColor bold = TerminalColor._(1);
128135
static const TerminalColor underline = TerminalColor._(4);
129136
}
137+

pubspec.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ script_runner:
5050
- name: combined
5151
cmd: echo 'test' && echo1 && echo2
5252
- short: echo 'this is a short script'
53+
- name: error
54+
cmd: |-
55+
_fn() {
56+
return 1
57+
}
58+
_fn
59+
description: imitate error
5360

5461
executables:
5562
scr: script_runner

0 commit comments

Comments
 (0)