From aab461ec2cfaf3a03b34de38a1ad8fdc43dfa67a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Jakubowski?= Date: Wed, 5 Jun 2024 22:09:51 +0200 Subject: [PATCH] Create widgets directory if it doesn't exist when generating a use-case --- .../generate_entries_for_directory.ts | 9 ++++++++- src/commands/generate_entry_for_widget.ts | 12 +++++++++-- src/generators/path/generator.ts | 4 +++- src/generators/path/generator_impl.ts | 20 +++++++++++++------ src/util/file_util.ts | 12 ++++++++++- 5 files changed, 46 insertions(+), 11 deletions(-) diff --git a/src/commands/generate_entries_for_directory.ts b/src/commands/generate_entries_for_directory.ts index 36051a5..9830117 100644 --- a/src/commands/generate_entries_for_directory.ts +++ b/src/commands/generate_entries_for_directory.ts @@ -3,7 +3,10 @@ import { FileContentGeneratorFactory } from "../generators/file_content/factory" import { PathGeneratorFactory } from "../generators/path/factory"; import { CustomKnobsProvider } from "../providers/custom_knobs_provider"; import { parseTextToClass } from "../util/dart_class_parser"; -import { writeWidgetbookEntry } from "../util/file_util"; +import { + createDirectoryIfNotExists, + writeWidgetbookEntry, +} from "../util/file_util"; import path = require("path"); async function generateWidgetbookEntriesForDirectory( @@ -38,6 +41,10 @@ async function generateWidgetbookEntriesForDirectory( customKnobs ); + await createDirectoryIfNotExists( + pathGenerator.prepareWidgetbookWidgetsDirectoryPath(filePath) + ); + await writeWidgetbookEntry( clazz, filePath, diff --git a/src/commands/generate_entry_for_widget.ts b/src/commands/generate_entry_for_widget.ts index ce2a528..1adf015 100644 --- a/src/commands/generate_entry_for_widget.ts +++ b/src/commands/generate_entry_for_widget.ts @@ -3,7 +3,10 @@ import { FileContentGeneratorFactory } from "../generators/file_content/factory" import { PathGeneratorFactory } from "../generators/path/factory"; import { CustomKnobsProvider } from "../providers/custom_knobs_provider"; import { parseTextToClass } from "../util/dart_class_parser"; -import { writeWidgetbookEntry } from "../util/file_util"; +import { + createDirectoryIfNotExists, + writeWidgetbookEntry, +} from "../util/file_util"; async function generateWidgetbookEntryForWidgetInScope(): Promise { const activeEditor = vscode.window.activeTextEditor; @@ -29,10 +32,15 @@ async function generateWidgetbookEntryForWidgetInScope(): Promise { clazz, customKnobs ); + const filePath = activeEditor.document.fileName; + + await createDirectoryIfNotExists( + pathGenerator.prepareWidgetbookWidgetsDirectoryPath(filePath) + ); await writeWidgetbookEntry( clazz, - activeEditor.document.fileName, + filePath, pathGenerator, fileContentGenerator ); diff --git a/src/generators/path/generator.ts b/src/generators/path/generator.ts index 62bc549..0265330 100644 --- a/src/generators/path/generator.ts +++ b/src/generators/path/generator.ts @@ -2,7 +2,9 @@ interface PathGenerator { prepareWidgetbookEntryFilePath( className: string, widgetFilePath: string - ): string | null; + ): string; + + prepareWidgetbookWidgetsDirectoryPath(widgetFilePath: string): string; prepareCustomKnobsFilePath(currentPath: string): string | null; } diff --git a/src/generators/path/generator_impl.ts b/src/generators/path/generator_impl.ts index 9377e6e..9b39cf2 100644 --- a/src/generators/path/generator_impl.ts +++ b/src/generators/path/generator_impl.ts @@ -7,19 +7,27 @@ class PathGeneratorImpl implements PathGenerator { prepareWidgetbookEntryFilePath( className: string, widgetFilePath: string - ): string | null { + ): string { + const classNameSnakeCase = snakeCase(className); + const outputFilePath = path.join( + this.prepareWidgetbookWidgetsDirectoryPath(widgetFilePath), + `${classNameSnakeCase}.dart` + ); + + return outputFilePath; + } + + prepareWidgetbookWidgetsDirectoryPath(widgetFilePath: string): string { const rootDirectory = Configuration.rootProjectDirectoryName(); const widgetbookWidgetsDirectory = Configuration.widgetsDirectoryPath(); - const classNameSnakeCase = snakeCase(className); const projectRootPath = widgetFilePath.substringUpToAndIncluding(rootDirectory); - const outputFilePath = path.join( + const outputDirectoryPath = path.join( projectRootPath, - widgetbookWidgetsDirectory, - `${classNameSnakeCase}.dart` + widgetbookWidgetsDirectory ); - return outputFilePath; + return outputDirectoryPath; } prepareCustomKnobsFilePath(currentPath: string): string | null { diff --git a/src/util/file_util.ts b/src/util/file_util.ts index 783409a..5f4b1ef 100644 --- a/src/util/file_util.ts +++ b/src/util/file_util.ts @@ -40,6 +40,16 @@ async function writeWidgetbookEntry( await formatAndSaveFile(filePath); } +async function createDirectoryIfNotExists( + directoryPath: string +): Promise { + const uri = vscode.Uri.file(directoryPath); + + if (!existsSync(uri.fsPath)) { + await vscode.workspace.fs.createDirectory(uri); + } +} + async function showOverrideFileDialog(fileContent: string): Promise { const yesOption = "YES"; const noOption = `NO`; @@ -92,4 +102,4 @@ function prepareWidgetbookEntry( return output; } -export { writeWidgetbookEntry }; +export { createDirectoryIfNotExists, writeWidgetbookEntry };