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

4
.gitignore vendored
View File

@ -1,7 +1,7 @@
# https://dart.dev/guides/libraries/private-files
# Created by `dart pub`
.dart_tool/
.direnv/**
bin/**
!bin/dartboard_resume.dart
*.toml
*.pdf

View File

@ -1,2 +1,15 @@
A sample command-line application with an entrypoint in `bin/`, library code
in `lib/`, and example unit test in `test/`.
# Dartboard Resume
## Installing
TODO
## Running
`dart run ./bin/dartboard_resume.dart`
or
## Creating Your Resume

View File

@ -17,5 +17,5 @@ Future<void> main(List<String> arguments) async {
"Dartboard can hot reload if run with dart's VM service.\n`dart run --enable-vm-service bin/dartboard_resume.dart`",
);
}
dartboardRun(reloader);
dartboardRun(reloader, arguments);
}

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
}

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);
}
}

View File

@ -5,23 +5,23 @@ packages:
dependency: transitive
description:
name: _fe_analyzer_shared
sha256: "45cfa8471b89fb6643fe9bf51bd7931a76b8f5ec2d65de4fb176dba8d4f22c77"
sha256: "5aaf60d96c4cd00fe7f21594b5ad6a1b699c80a27420f8a837f4d68473ef09e3"
url: "https://pub.dev"
source: hosted
version: "73.0.0"
version: "68.0.0"
_macros:
dependency: transitive
description: dart
source: sdk
version: "0.3.2"
version: "0.1.0"
analyzer:
dependency: transitive
description:
name: analyzer
sha256: "4959fec185fe70cce007c57e9ab6983101dbe593d2bf8bbfb4453aaec0cf470a"
sha256: "21f1d3720fd1c70316399d5e2bccaebb415c434592d778cce8acb967b8578808"
url: "https://pub.dev"
source: hosted
version: "6.8.0"
version: "6.5.0"
archive:
dependency: transitive
description:
@ -31,13 +31,13 @@ packages:
source: hosted
version: "3.6.1"
args:
dependency: transitive
dependency: "direct main"
description:
name: args
sha256: "7cf60b9f0cc88203c5a190b4cd62a99feea42759a7fa695010eb5de1c0b2252a"
sha256: bf9f5caeea8d8fe6721a9c358dd8a5c1947b27f1cfaa18b39c301273594919e6
url: "https://pub.dev"
source: hosted
version: "2.5.0"
version: "2.6.0"
async:
dependency: transitive
description:
@ -135,7 +135,7 @@ packages:
source: hosted
version: "2.1.2"
hotreloader:
dependency: "direct dev"
dependency: "direct main"
description:
name: hotreloader
sha256: ed56fdc1f3a8ac924e717257621d09e9ec20e308ab6352a73a50a1d7a4d9158e
@ -218,10 +218,10 @@ packages:
dependency: transitive
description:
name: macros
sha256: "0acaed5d6b7eab89f63350bccd82119e6c602df0f391260d0e32b5e23db79536"
sha256: "12e8a9842b5a7390de7a781ec63d793527582398d16ea26c60fed58833c9ae79"
url: "https://pub.dev"
source: hosted
version: "0.1.2-main.4"
version: "0.1.0-main.0"
matcher:
dependency: transitive
description:

View File

@ -8,13 +8,13 @@ environment:
# Add regular dependencies here.
dependencies:
args: ^2.6.0
hotreloader: ^4.2.0
intl: ^0.19.0
lint: ^2.3.0
logging: ^1.2.0
pdf: ^3.11.1
toml: ^0.16.0
# path: ^1.8.0
dev_dependencies:
lints: ^3.0.0