Skip to content

Commit

Permalink
fix: config factory
Browse files Browse the repository at this point in the history
chore: remove yaml dependency
  • Loading branch information
chenasraf committed Jan 16, 2024
1 parent 6b32489 commit 00c5ae4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
8 changes: 6 additions & 2 deletions bin/script_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import 'package:script_runner/src/utils.dart';

/// Main entrypoint for CMD script runner.
Future<void> main(List<String> args) async {
if (args.isEmpty) {
printColor('No script command provided. Use -h to see available commands.', [TerminalColor.red]);
return;
}
final scriptCmd = args.first;
final scriptArgs = args.sublist(1);
try {
await runScript(scriptCmd, scriptArgs);
} catch (e) {
printColor('$e', [TerminalColor.red]);
} catch (e, stack) {
printColor('$e\n$stack', [TerminalColor.red]);
}
}
32 changes: 9 additions & 23 deletions lib/src/runnable_script.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:file/local.dart';
import 'package:script_runner/src/config.dart';
// ignore: no_leading_underscores_for_library_prefixes
import 'package:script_runner/src/utils.dart' as _utils;
import 'package:yaml/yaml.dart' as yaml;

/// A runnable script with pre-defined name, cmd and args. May be run using the `run` command and optionally
/// supplying extra arguments to pass.
Expand Down Expand Up @@ -64,33 +63,20 @@ class RunnableScript {
this.appendNewline = false,
}) : _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}) {
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['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');
}
}

/// Generate a runnable script from a normal map as defined in the config.
factory RunnableScript.fromMap(
Map<String, dynamic> map, {
FileSystem? fileSystem,
}) {
if (map['name'] == null && map.keys.length == 1) {
map['name'] = map.keys.first;
map['cmd'] = map.values.first;
} else {
map.addAll(map.cast<String, dynamic>());
map['args'] =
(map['args'] as List?)?.map((e) => e.toString()).toList();
map['env'] = (map['env'] as Map?)?.cast<String, String>();
}
final name = map['name'] as String;
final rawCmd = map['cmd'] as String;
final cmd = rawCmd;
Expand Down
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ dependencies:
unaconfig: ^0.1.3
# unaconfig:
# path: ../unaconfig
yaml: ^3.1.2

dev_dependencies:
lints:
Expand Down

0 comments on commit 00c5ae4

Please sign in to comment.