From 5d41ae1572bb7118daa7ae7e799affb26cbe9b44 Mon Sep 17 00:00:00 2001 From: Gorniaky Date: Fri, 4 Sep 2026 21:05:25 -0300 Subject: [PATCH] refactor: improve directory handling and markdown conversion for better performance --- scripts/docs/directory_extension.dart | 14 ++++++++------ scripts/docs/generate.dart | 2 +- scripts/docs/generate/commands.dart | 12 ++++++++---- scripts/docs/markdown_to_html_converter.dart | 4 ++-- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/scripts/docs/directory_extension.dart b/scripts/docs/directory_extension.dart index 7baa4b5..850b2fd 100644 --- a/scripts/docs/directory_extension.dart +++ b/scripts/docs/directory_extension.dart @@ -19,7 +19,10 @@ extension DirectoryExtension on Directory { final Directory dir = .new(filePath); - await for (final file in dir.list(recursive: recursive)) { + await for (final file in dir.list( + recursive: recursive, + followLinks: false, + )) { if (file is! File) continue; filesToPreserveContents[file] = await file.readAsBytes(); } @@ -37,14 +40,13 @@ extension DirectoryExtension on Directory { } await delete(recursive: true); - await create(); - for (final entry in filesToPreserveContents.entries) { - if (!await entry.key.parent.exists()) { - await entry.key.parent.create(recursive: true); + for (final MapEntry(:key, :value) in filesToPreserveContents.entries) { + if (key.parent case final parent) { + if (!await parent.exists()) await parent.create(recursive: true); } - await entry.key.writeAsBytes(entry.value, flush: true); + await key.writeAsBytes(value, flush: true); } } } diff --git a/scripts/docs/generate.dart b/scripts/docs/generate.dart index 075d148..b6cd67b 100644 --- a/scripts/docs/generate.dart +++ b/scripts/docs/generate.dart @@ -32,7 +32,7 @@ void main() async { final converter = MarkdownToHtmlConverter(directory: docRootDir); - await for (final (file, content) in converter.convert()) { + await for (final (file, content) in converter.convert(recursive: true)) { await file.writeAsString(content); } } diff --git a/scripts/docs/generate/commands.dart b/scripts/docs/generate/commands.dart index 7123bba..7bdc440 100644 --- a/scripts/docs/generate/commands.dart +++ b/scripts/docs/generate/commands.dart @@ -48,18 +48,22 @@ Future _recursiveDocsGenerate({ }) async { final StringBuffer buffer = .new(header); - if (parentCommandName case final String name) { + final noSpaceCommandName = command.name.replaceAll(" ", "_"); + + if (parentCommandName case final String parentCommandName) { + final noSpaceParentCommandName = parentCommandName.replaceAll(" ", "_"); + buffer ..writeAll([ - "### [$name](commands_${name.replaceAll(" ", "_")}.md)", - "[${command.name}](commands_${name.replaceAll(" ", "_")}_${command.name.replaceAll(" ", "_")}.md)", + "### [$parentCommandName](commands_$noSpaceParentCommandName.md)", + "[${command.name}](commands_${noSpaceParentCommandName}_$noSpaceCommandName.md)", ], " / ") ..writeln() ..writeln(); } else { buffer ..writeln( - "### [${command.name}](commands_${command.name.replaceAll(" ", "_")}.md)", + "### [${command.name}](commands_$noSpaceCommandName.md)", ) ..writeln(); } diff --git a/scripts/docs/markdown_to_html_converter.dart b/scripts/docs/markdown_to_html_converter.dart index bec25d3..e399d73 100644 --- a/scripts/docs/markdown_to_html_converter.dart +++ b/scripts/docs/markdown_to_html_converter.dart @@ -10,9 +10,9 @@ final class MarkdownToHtmlConverter { final Directory directory; - Stream<(File file, String content)> convert() async* { + Stream<(File, String)> convert({bool recursive = false}) async* { final entities = await directory - .list(recursive: true) + .list(recursive: recursive, followLinks: false) .where((e) => e is File && extension(e.path) == _mdExt) .toList();