Skip to content

Commit

Permalink
Fix knobs for widgetbook 3.0.0, fix generating widgetbook entries for…
Browse files Browse the repository at this point in the history
… the whole directory at once
  • Loading branch information
FirentisTFW committed Nov 18, 2023
1 parent 54ed43a commit 3e1c579
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/commands/generate_entries_for_directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async function generateWidgetbookEntriesForDirectoryImpl(

const clazz = parseTextToClass(fileContentString);

await writeWidgetbookEntry(clazz);
await writeWidgetbookEntry(clazz, filePath);
} else if (fileType === vscode.FileType.Directory) {
const subdirectoryPath = path.join(directoryPath, fileName);

Expand Down
2 changes: 1 addition & 1 deletion src/commands/generate_entry_for_widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async function generateWidgetbookEntryForWidgetInScope(): Promise<void> {

const clazz = parseTextToClass(fileContentFromCurrentLine);

await writeWidgetbookEntry(clazz);
await writeWidgetbookEntry(clazz, activeEditor.document.fileName);
}

export { generateWidgetbookEntryForWidgetInScope };
2 changes: 1 addition & 1 deletion src/generators/file_content/base_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ abstract class BaseFileContentGenerator implements FileContentGenerator {
>([
[
"bool",
(fieldName) => `context.knobs.nullableBoolean(label: '${fieldName}')`,
(fieldName) => `context.knobs.booleanOrNull(label: '${fieldName}')`,
],
[
"String",
Expand Down
20 changes: 0 additions & 20 deletions src/generators/file_content/impl/3_2_0_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,12 @@ import { BaseFileContentGenerator } from "../base_generator";

class FileContentGenerator3_2_0 extends BaseFileContentGenerator {
applyMigrations(): void {
this.knobForType.set(
"String",
(fieldName) =>
`context.knobs.string(label: '${fieldName}', initialValue: '${fieldName}')`
);
this.knobForType.set("double", (fieldName) =>
this.numberKnob(fieldName, "")
);
this.knobForType.set("int", (fieldName) =>
this.numberKnob(fieldName, ".toInt()")
);

this.knobForNullableType.set(
"bool",
(fieldName) => `context.knobs.booleanOrNull(label: '${fieldName}')`
);
this.knobForNullableType.set(
"String",
(fieldName) => `context.knobs.stringOrNull(label: '${fieldName}')`
);
this.knobForNullableType.set("double", (fieldName) =>
this.nullableNumberKnob(fieldName, "")
);
this.knobForNullableType.set("int", (fieldName) =>
this.nullableNumberKnob(fieldName, "?.toInt()")
);
}

manualComponentDeclaration(): string {
Expand Down
5 changes: 4 additions & 1 deletion src/generators/path/generator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
interface PathGenerator {
prepareWidgetbookEntryFilePath(className: string): string | null;
prepareWidgetbookEntryFilePath(
className: string,
widgetFilePath: string
): string | null;
}

export { PathGenerator };
21 changes: 10 additions & 11 deletions src/generators/path/generator_impl.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { snakeCase } from "change-case";
import * as vscode from "vscode";
import { Configuration } from "../../configuration/configuration";
import { PathGenerator } from "./generator";

class PathGeneratorImpl implements PathGenerator {
prepareWidgetbookEntryFilePath(className: string): string | null {
const activeTextEditor = vscode.window.activeTextEditor;
if (!activeTextEditor) return null;

const currentPath = activeTextEditor.document.fileName;
const rootDir = Configuration.rootProjectDirectoryName();
const widgetbookWidgetsDir = Configuration.widgetsDirectoryPath();
prepareWidgetbookEntryFilePath(
className: string,
widgetFilePath: string
): string | null {
const rootDirectory = Configuration.rootProjectDirectoryName();
const widgetbookWidgetsDirectory = Configuration.widgetsDirectoryPath();
const classNameSnakeCase = snakeCase(className);
const projectRootPath = currentPath.substringUpToAndIncluding(rootDir);
const widgetFilePath = `${projectRootPath}/${widgetbookWidgetsDir}/${classNameSnakeCase}.dart`;
const projectRootPath =
widgetFilePath.substringUpToAndIncluding(rootDirectory);
const outputFilePath = `${projectRootPath}/${widgetbookWidgetsDirectory}/${classNameSnakeCase}.dart`;

return widgetFilePath;
return outputFilePath;
}
}
export { PathGeneratorImpl };
10 changes: 8 additions & 2 deletions src/util/file_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ import { PathGeneratorFactory } from "../generators/path/factory";

const ENCODING = "utf8";

async function writeWidgetbookEntry(clazz: DartClass): Promise<void> {
async function writeWidgetbookEntry(
clazz: DartClass,
widgetFilePath: string
): Promise<void> {
const pathGenerator = PathGeneratorFactory.create();

const filePath = pathGenerator.prepareWidgetbookEntryFilePath(clazz.name);
const filePath = pathGenerator.prepareWidgetbookEntryFilePath(
clazz.name,
widgetFilePath
);

if (filePath === null) {
// TODO Show error dialog or something
Expand Down

0 comments on commit 3e1c579

Please sign in to comment.