Skip to content

Commit

Permalink
feat: colorize thrown error
Browse files Browse the repository at this point in the history
  • Loading branch information
chenasraf committed Nov 30, 2023
1 parent 19a9f0a commit f9e936f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
9 changes: 8 additions & 1 deletion bin/script_runner.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import 'package:script_runner/src/base.dart';
import 'package:script_runner/src/utils.dart';

/// Main entrypoint for CMD script runner.
Future<void> main(List<String> args) async {
final scriptCmd = args.first;
final scriptArgs = args.sublist(1);
return runScript(scriptCmd, scriptArgs);
try {
print('Running script: $scriptCmd $scriptArgs');
await runScript(scriptCmd, scriptArgs);
} catch (e) {
printColor('$e', [TerminalColor.red]);
}
}

25 changes: 18 additions & 7 deletions lib/src/runnable_script.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,24 @@ class RunnableScript {
}) : _fileSystem = fileSystem ?? LocalFileSystem();

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

if (map['name'] == null && map.keys.length == 1) {
out['name'] = map.keys.first;
out['cmd'] = map.values.first;
} else {
out.addAll(map.cast<String, dynamic>());
out['args'] = (map['args'] as yaml.YamlList?)?.map((e) => e.toString()).toList();
out['args'] =
(map['args'] as yaml.YamlList?)?.map((e) => e.toString()).toList();
out['env'] = (map['env'] as yaml.YamlMap?)?.cast<String, String>();
}
try {
return RunnableScript.fromMap(out, fileSystem: fileSystem);
} catch (e) {
throw StateError('Failed to parse script, arguments: $map, $fileSystem. Error: $e');
throw StateError(
'Failed to parse script, arguments: $map, $fileSystem. Error: $e');
}
}

Expand Down Expand Up @@ -109,7 +112,8 @@ class RunnableScript {
appendNewline: appendNewline,
);
} catch (e) {
throw StateError('Failed to parse script, arguments: $map, $fileSystem. Error: $e');
throw StateError(
'Failed to parse script, arguments: $map, $fileSystem. Error: $e');
}
}

Expand Down Expand Up @@ -148,6 +152,8 @@ class RunnableScript {
);
throw e;
}
} catch (e) {
rethrow;
} finally {
await _fileSystem.file(scrPath).delete();
}
Expand All @@ -169,7 +175,8 @@ class RunnableScript {
return exitCode;
}

String _getScriptPath() => _fileSystem.path.join(_fileSystem.systemTempDirectory.path, 'script_runner_$name.sh');
String _getScriptPath() => _fileSystem.path
.join(_fileSystem.systemTempDirectory.path, 'script_runner_$name.sh');

String _getScriptContents(
ScriptRunnerConfig config, {
Expand All @@ -185,8 +192,12 @@ class RunnableScript {
].join('\n');
case OS.linux:
case OS.macos:
return [...preloadScripts.map((e) => "[[ ! \$(which ${e.name}) ]] && alias ${e.name}='scr ${e.name}'"), script]
.join('\n');
return [
...preloadScripts.map((e) =>
"[[ ! \$(which ${e.name}) ]] && alias ${e.name}='scr ${e.name}'"),
script
].join('\n');
}
}
}

8 changes: 8 additions & 0 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ List<String> chunks(
final chunks = <String>[];
var chunk = '';
for (final word in words) {
// if (chunk.contains('\n')) {
// final lines = chunk.split('\n');
// for (var i = 0; i < lines.length - 1; i++) {
// chunks.add(wrapLine(lines[i]));
// }
// chunk = '';
// }
final chunkLength = stripColors ? stripColor(chunk).length : chunk.length;
final wordLength = stripColors ? stripColor(word).length : word.length;
if (chunkLength + wordLength > maxLen) {
Expand Down Expand Up @@ -127,3 +134,4 @@ class TerminalColor {
static const TerminalColor bold = TerminalColor._(1);
static const TerminalColor underline = TerminalColor._(4);
}

7 changes: 7 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ script_runner:
- name: combined
cmd: echo 'test' && echo1 && echo2
- short: echo 'this is a short script'
- name: error
cmd: |-
_fn() {
return 1
}
_fn
description: imitate error

executables:
scr: script_runner

0 comments on commit f9e936f

Please sign in to comment.