Added cli options and help command, WIP readme update

This commit is contained in:
Nate Anderson
2024-12-19 16:48:03 -07:00
parent fe40731e09
commit 65922f515a
7 changed files with 94 additions and 29 deletions
+21 -12
View File
@@ -2,14 +2,19 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:dartboard_resume/render.dart';
import 'package:dartboard_resume/utils.dart';
import 'package:hotreloader/hotreloader.dart';
import 'package:toml/toml.dart';
StreamSubscription<FileSystemEvent>? fileStreamSub;
StreamSubscription<String>? stdinStreamSub;
Future<void> dartboardRun(HotReloader? reloader) async {
const String tomlFilePath = "resume.toml";
Future<void> dartboardRun(HotReloader? reloader, List<String> arguments) async {
final result = getTomlFromArgs(arguments);
if (!result.success) {
exitRunner(reloader);
}
final resumeTomlFile = result.file!;
if (reloader != null) {
stdout.writeln('Hot reload enabled!');
@@ -17,22 +22,18 @@ Future<void> dartboardRun(HotReloader? reloader) async {
ProcessSignal.sigint.watch().listen((_) {
stdout.writeln('SIGINT received. Exiting gracefully...');
fileStreamSub?.cancel();
stdinStreamSub?.cancel();
// Perform cleanup or other necessary actions here
reloader?.stop();
exit(0); // Exit with code 0 to indicate a successful termination
exitRunner(reloader);
});
stdinStreamSub = getUserInputStream().listen(
(event) {
if (event == "r") {
stdout.writeln("Triggering pdf render...");
createDocument(tomlFilePath);
createDocument(resumeTomlFile.path);
}
if (event == "p") {
stdout.writeln("Current toml map:");
stdout.writeln(TomlDocument.loadSync(tomlFilePath).toMap());
stdout.writeln(TomlDocument.loadSync(resumeTomlFile.path).toMap());
}
if (event == "q") {
stdout.writeln('Exiting...');
@@ -46,14 +47,14 @@ Future<void> dartboardRun(HotReloader? reloader) async {
);
if (FileSystemEntity.isWatchSupported) {
final fileStream = File(tomlFilePath).watch(events: FileSystemEvent.modify);
final fileStream = resumeTomlFile.watch(events: FileSystemEvent.modify);
fileStreamSub = fileStream.listen((e) {
createDocument(tomlFilePath);
createDocument(resumeTomlFile.path);
});
stdout.writeln('Watching for file changes.');
} else {
stdout.writeln('File watch is not supported. Exiting upon completion.');
createDocument(tomlFilePath);
createDocument(resumeTomlFile.path);
}
}
@@ -80,3 +81,11 @@ void createDocument(String tomlFilePath) {
renderPdf(tomlFilePath, force: true);
refreshViewer();
}
void exitRunner(HotReloader? reloader) {
fileStreamSub?.cancel();
stdinStreamSub?.cancel();
// Perform cleanup or other necessary actions here
reloader?.stop();
exit(0); // Exit with code 0 to indicate a successful termination
}
+43
View File
@@ -1,5 +1,48 @@
import 'dart:io';
import 'package:args/args.dart';
extension StringUtils on String {
String capitalize() {
return substring(0, 1).toUpperCase() + substring(1);
}
}
({bool success, File? file}) getTomlFromArgs(List<String> args) {
const String templateResumePath = 'assets/resume_template.toml';
final parser = ArgParser();
parser.addOption('input', abbr: 'i', defaultsTo: 'examples/resume.toml', help: 'Input file path');
parser.addFlag('help', abbr: 'h', help: 'Prints help info');
final parsedResults = parser.parse(args);
if (parsedResults.flag("help")) {
stdout.writeln(parser.usage);
return (success: false, file: null);
}
final inputFilePath = parsedResults.option('input');
if (inputFilePath == null) {
throw Exception('No valid input provided.');
}
final inputFile = File(inputFilePath);
if (!inputFile.existsSync()) {
stdout.writeln(
'Provided `$inputFilePath`, but it does not exist. Would you like to start with the template?\n\t($templateResumePath copied to $inputFilePath)\n(y/n): ',
);
final ans = stdin.readLineSync()?.toLowerCase() ?? 'n';
if (ans == 'y') {
stdout.writeln('Copying over...');
File(templateResumePath).copySync(inputFilePath);
final newFile = File(inputFilePath);
if (!newFile.existsSync()) {
return (success: false, file: null);
}
return (success: true, file: newFile);
} else {
stdout.writeln('Got it. Try again with an actual existing document. You got this 👍');
return (success: false, file: null);
}
} else {
return (success: true, file: inputFile);
}
}