Skip to content

Commit

Permalink
refactor: merge frameworkPlugin into workspace (#1943)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwouts committed Aug 19, 2023
1 parent a09062d commit 97c5504
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 60 deletions.
37 changes: 18 additions & 19 deletions chromeless/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,32 +128,33 @@ export async function startPreview({
},
async show(previewableId: string, propsAssignmentSource?: string) {
const filePath = previewableId.split(":")[0]!;
const { components, stories } = await workspace.crawlFiles({
filePaths: [filePath],
});
const { components, stories } = await workspace.crawlFiles([filePath]);
const matchingComponent = components.find((c) => previewableId === c.id);
const matchingStory = stories.find((c) => previewableId === c.id);
if (!matchingComponent && !matchingStory) {
throw new Error(
`Component may be previewable but was not detected by framework plugin: ${previewableId}`
);
}
const analyzeResponse = await workspace.analyze({
previewableIds: [previewableId],
const component = matchingComponent || matchingStory?.associatedComponent;
const { props, types } = await (component?.analyze() || {
props: { kind: "unknown" as const },
types: {},
});
const props = analyzeResponse.props[previewableId]!;
const autogenCallbackProps = await generateCallbackProps(
props,
analyzeResponse.types
const autogenCallbackProps = await generateCallbackProps(props, types);
const autogenCallbackPropsSource = transpile(
`autogenCallbackProps = ${autogenCallbackProps.source}`
);
if (!propsAssignmentSource) {
propsAssignmentSource = matchingStory
? "properties = null"
: await generatePropsAssignmentSource(
props,
autogenCallbackProps.keys,
analyzeResponse.types
);
if (matchingStory) {
propsAssignmentSource = "properties = null";
} else {
propsAssignmentSource = await generatePropsAssignmentSource(
props,
autogenCallbackProps.keys,
types
);
}
}
const donePromise = new Promise<void>((resolve, reject) => {
onRenderingDone = resolve;
Expand Down Expand Up @@ -181,9 +182,7 @@ export async function startPreview({
},
{
previewableId,
autogenCallbackPropsSource: transpile(
`autogenCallbackProps = ${autogenCallbackProps.source}`
),
autogenCallbackPropsSource,
propsAssignmentSource: transpile(propsAssignmentSource!),
}
);
Expand Down
31 changes: 3 additions & 28 deletions core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { decodePreviewableId } from "@previewjs/analyzer-api";
import type { RequestOf, ResponseOf, RPC } from "@previewjs/api";
import { RPCs } from "@previewjs/api";
import type {
CollectedTypes,
TypeAnalyzer,
ValueType,
} from "@previewjs/type-analyzer";
import type { CollectedTypes, ValueType } from "@previewjs/type-analyzer";
import { UNKNOWN_TYPE } from "@previewjs/type-analyzer";
import type { Reader } from "@previewjs/vfs";
import express from "express";
Expand Down Expand Up @@ -158,23 +153,10 @@ export async function createWorkspace({
},
});

async function localRpc<E extends RPC<any, any>>(
endpoint: E,
request: RequestOf<E>
): Promise<ResponseOf<E>> {
const result = await router.handle(endpoint.path, request);
if (result.kind === "success") {
return result.response as ResponseOf<E>;
}
throw new Error(result.message);
}

const workspace: Workspace = {
...frameworkPlugin,
rootDir,
reader,
typeAnalyzer: frameworkPlugin.typeAnalyzer,
crawlFiles: (options = {}) => localRpc(RPCs.CrawlFiles, options),
analyze: (options) => localRpc(RPCs.Analyze, options),
preview: {
start: async (allocatePort) => {
const port = await previewer.start(async () => {
Expand Down Expand Up @@ -221,16 +203,9 @@ export function findWorkspaceRoot(absoluteFilePath: string): string | null {
return null;
}

export interface Workspace {
export interface Workspace extends Omit<FrameworkPlugin, "dispose"> {
rootDir: string;
reader: Reader;
typeAnalyzer: Omit<TypeAnalyzer, "dispose">;
crawlFiles(
options?: RequestOf<typeof RPCs.CrawlFiles>
): Promise<ResponseOf<typeof RPCs.CrawlFiles>>;
analyze(
options: RequestOf<typeof RPCs.Analyze>
): Promise<ResponseOf<typeof RPCs.Analyze>>;
preview: {
start(allocatePort?: () => Promise<number>): Promise<Preview>;
};
Expand Down
18 changes: 8 additions & 10 deletions daemon/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,16 +329,14 @@ export async function startDaemon({
if (!workspace) {
throw new NotFoundError();
}
const { components, stories } = await workspace.crawlFiles({
filePaths: [
path
.relative(
workspace.rootDir,
transformAbsoluteFilePath(absoluteFilePath)
)
.replace(/\\/g, "/"),
],
});
const { components, stories } = await workspace.crawlFiles([
path
.relative(
workspace.rootDir,
transformAbsoluteFilePath(absoluteFilePath)
)
.replace(/\\/g, "/"),
]);
return {
previewables: [...components, ...stories].map((c) => ({
id: c.id,
Expand Down
4 changes: 1 addition & 3 deletions screenshot/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ export async function generateScreenshots({
cwd,
followSymbolicLinks: false,
});
const { components, stories } = await workspace.crawlFiles({
filePaths,
});
const { components, stories } = await workspace.crawlFiles(filePaths);
for (const previewable of [...components, ...stories]) {
const { filePath, name } = decodePreviewableId(previewable.id);
try {
Expand Down

0 comments on commit 97c5504

Please sign in to comment.