Skip to content

Commit

Permalink
Create widgets directory if it doesn't exist when generating a use-case
Browse files Browse the repository at this point in the history
  • Loading branch information
FirentisTFW committed Jun 5, 2024
1 parent 943f66c commit aab461e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
9 changes: 8 additions & 1 deletion src/commands/generate_entries_for_directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -38,6 +41,10 @@ async function generateWidgetbookEntriesForDirectory(
customKnobs
);

await createDirectoryIfNotExists(
pathGenerator.prepareWidgetbookWidgetsDirectoryPath(filePath)
);

await writeWidgetbookEntry(
clazz,
filePath,
Expand Down
12 changes: 10 additions & 2 deletions src/commands/generate_entry_for_widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const activeEditor = vscode.window.activeTextEditor;
Expand All @@ -29,10 +32,15 @@ async function generateWidgetbookEntryForWidgetInScope(): Promise<void> {
clazz,
customKnobs
);
const filePath = activeEditor.document.fileName;

await createDirectoryIfNotExists(
pathGenerator.prepareWidgetbookWidgetsDirectoryPath(filePath)
);

await writeWidgetbookEntry(
clazz,
activeEditor.document.fileName,
filePath,
pathGenerator,
fileContentGenerator
);
Expand Down
4 changes: 3 additions & 1 deletion src/generators/path/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ interface PathGenerator {
prepareWidgetbookEntryFilePath(
className: string,
widgetFilePath: string
): string | null;
): string;

prepareWidgetbookWidgetsDirectoryPath(widgetFilePath: string): string;

prepareCustomKnobsFilePath(currentPath: string): string | null;
}
Expand Down
20 changes: 14 additions & 6 deletions src/generators/path/generator_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion src/util/file_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ async function writeWidgetbookEntry(
await formatAndSaveFile(filePath);
}

async function createDirectoryIfNotExists(
directoryPath: string
): Promise<void> {
const uri = vscode.Uri.file(directoryPath);

if (!existsSync(uri.fsPath)) {
await vscode.workspace.fs.createDirectory(uri);
}
}

async function showOverrideFileDialog(fileContent: string): Promise<boolean> {
const yesOption = "YES";
const noOption = `NO`;
Expand Down Expand Up @@ -92,4 +102,4 @@ function prepareWidgetbookEntry(
return output;
}

export { writeWidgetbookEntry };
export { createDirectoryIfNotExists, writeWidgetbookEntry };

0 comments on commit aab461e

Please sign in to comment.