Skip to content

Commit

Permalink
refactor: remove setupFrameworkPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
fwouts committed Aug 26, 2023
1 parent de6f100 commit b5febbc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
15 changes: 2 additions & 13 deletions chromeless/src/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FrameworkPluginFactory, Workspace } from "@previewjs/core";
import { createWorkspace, setupFrameworkPlugin } from "@previewjs/core";
import { createWorkspace } from "@previewjs/core";
import type { Reader } from "@previewjs/vfs";
import { createFileSystemReader } from "@previewjs/vfs";
import express from "express";
Expand Down Expand Up @@ -36,22 +36,11 @@ export async function createChromelessWorkspace({
) => ReturnType<typeof startPreview>;
}
> {
const frameworkPlugin = await setupFrameworkPlugin({
rootDir,
frameworkPlugins,
reader,
logger,
});
if (!frameworkPlugin) {
throw new Error(
`No compatible framework plugin found for directory: ${rootDir}`
);
}
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
const clientDirPath = path.join(__dirname, "..", "client", "dist");
const workspace = await createWorkspace({
rootDir,
frameworkPlugin,
frameworkPlugins,
logger,
reader,
onServerStart: async () => ({
Expand Down
39 changes: 30 additions & 9 deletions core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { decodePreviewableId } from "@previewjs/analyzer-api";
import { decodePreviewableId, type Analyzer } from "@previewjs/analyzer-api";
import { RPCs } from "@previewjs/api";
import type { CollectedTypes, ValueType } from "@previewjs/type-analyzer";
import type {
CollectedTypes,
TypeAnalyzer,
ValueType,
} from "@previewjs/type-analyzer";
import { UNKNOWN_TYPE } from "@previewjs/type-analyzer";
import type { Reader } from "@previewjs/vfs";
import express from "express";
Expand All @@ -10,7 +14,8 @@ import path from "path";
import type { Logger } from "pino";
import { crawlFiles } from "./crawl-files";
import { getFreePort } from "./get-free-port";
import type { FrameworkPlugin } from "./plugins/framework";
import type { FrameworkPluginFactory } from "./plugins/framework";
import { setupFrameworkPlugin } from "./plugins/setup-framework-plugin";
import type { OnServerStart } from "./preview-env";
import { Previewer } from "./previewer";
import { ApiRouter } from "./router";
Expand All @@ -19,7 +24,6 @@ export type {
FrameworkPlugin,
FrameworkPluginFactory,
} from "./plugins/framework";
export { setupFrameworkPlugin } from "./plugins/setup-framework-plugin";
export type { OnServerStart } from "./preview-env";

const require = createRequire(import.meta.url);
Expand All @@ -36,16 +40,28 @@ process.on("unhandledRejection", (e) => {
export async function createWorkspace({
rootDir,
reader,
frameworkPlugin,
frameworkPlugins,
logger,
onServerStart = () => Promise.resolve({}),
}: {
rootDir: string;
frameworkPlugin: FrameworkPlugin;
frameworkPlugins: FrameworkPluginFactory[];
logger: Logger;
reader: Reader;
onServerStart?: OnServerStart;
}): Promise<Workspace> {
}): Promise<Workspace | null> {
const frameworkPlugin = await setupFrameworkPlugin({
rootDir,
frameworkPlugins,
reader,
logger,
});
if (!frameworkPlugin) {
logger.debug(
`No compatible plugin found for workspace with root: ${rootDir}`
);
return null;
}
logger.debug(
`Creating workspace with framework plugin ${frameworkPlugin.name} from root: ${rootDir}`
);
Expand All @@ -63,7 +79,9 @@ export async function createWorkspace({
);
}
const workspace: Workspace = {
...frameworkPlugin,
frameworkPluginName: frameworkPlugin.name,
crawlFiles: frameworkPlugin.crawlFiles,
typeAnalyzer: frameworkPlugin.typeAnalyzer,
rootDir,
reader,
startServer: async ({ port } = {}) => {
Expand Down Expand Up @@ -200,9 +218,12 @@ export function findWorkspaceRoot(absoluteFilePath: string): string | null {
return null;
}

export interface Workspace extends Omit<FrameworkPlugin, "dispose"> {
export interface Workspace {
rootDir: string;
reader: Reader;
frameworkPluginName: string;
typeAnalyzer: Omit<TypeAnalyzer, "dispose">;
crawlFiles: Analyzer["crawlFiles"];
startServer: (options?: { port?: number }) => Promise<PreviewServer>;
dispose(): Promise<void>;
}
Expand Down
30 changes: 13 additions & 17 deletions loader/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,28 @@ export async function load({
return existingWorkspace;
}
const created = await locking(async () => {
const frameworkPlugin = await core.setupFrameworkPlugin({
rootDir,
frameworkPlugins,
reader,
logger,
});
if (!frameworkPlugin) {
logger.warn(
`No compatible Preview.js plugin for workspace: ${rootDir}`
);
return null;
}
logger.info(
`Creating Preview.js workspace (plugin: ${frameworkPlugin.name}) at ${rootDir}`
);
return await core.createWorkspace({
const workspace = await core.createWorkspace({
logger,
rootDir,
reader,
frameworkPlugin,
frameworkPlugins,
onServerStart: (options) =>
onServerStart({
versionCode,
...options,
}),
});
if (workspace) {
logger.info(
`Created Preview.js workspace (plugin: ${workspace.frameworkPluginName}) at ${rootDir}`
);
return workspace;
} else {
logger.warn(
`No compatible Preview.js plugin for workspace: ${rootDir}`
);
return null;
}
});
// Note: This caches the incompatibility of a workspace (i.e. caching null), which
// would be problematic especially when package.json is updated to a compatible
Expand Down

0 comments on commit b5febbc

Please sign in to comment.