Skip to content

Commit

Permalink
refactor: use "previewable" to describe "component or story" (#1932)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwouts committed Aug 17, 2023
1 parent 2464d65 commit f1f11ed
Show file tree
Hide file tree
Showing 109 changed files with 1,479 additions and 1,593 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
dist
component-analyzer/react/types
analyzer/react/types
framework-plugins/vue2/preview/modules
framework-plugins/vue3/preview/modules
framework-plugins/*/tests/apps
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release-components.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
fi
fi
- run: |
cd component-analyzer/api
cd analyzer/api
if ! pnpm publish --no-git-checks --access public 2>error.log; then
if grep -q -F 'You cannot publish over the previously published versions' error.log; then
echo "Package is already published. Ignoring the following:"
Expand All @@ -76,7 +76,7 @@ jobs:
fi
fi
- run: |
cd component-analyzer/react
cd analyzer/react
if ! pnpm publish --no-git-checks --access public 2>error.log; then
if grep -q -F 'You cannot publish over the previously published versions' error.log; then
echo "Package is already published. Ignoring the following:"
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@previewjs/component-analyzer-api",
"name": "@previewjs/analyzer-api",
"version": "0.0.1",
"license": "MIT",
"author": {
Expand Down
18 changes: 9 additions & 9 deletions component-analyzer/api/src/api.ts → analyzer/api/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ import type {
import type { Reader } from "@previewjs/vfs";
import type { Logger } from "pino";

export type ComponentAnalyzer = {
export type Analyzer = {
typeAnalyzer: Omit<TypeAnalyzer, "dispose">;
detectComponents: (filePaths: string[]) => Promise<{
analyze: (filePaths: string[]) => Promise<{
components: Component[];
stories: Story[];
}>;
dispose: () => void;
};

export type ComponentAnalyzerFactory = (options: {
export type AnalyzerFactory = (options: {
rootDir: string;
reader?: Reader;
logger?: Logger;
}) => ComponentAnalyzer;
}) => Analyzer;

export interface BaseComponent {
componentId: string;
export interface BasePreviewable {
id: string;
sourcePosition: FileSourcePosition;
}

export interface Component extends BaseComponent {
export interface Component extends BasePreviewable {
exported: boolean;
extractProps: () => Promise<ComponentProps>;
}
Expand All @@ -37,7 +37,7 @@ export interface ComponentProps {
types: CollectedTypes;
}

export interface Story extends BaseComponent {
export interface Story extends BasePreviewable {
extractArgs: () => Promise<StoryArgs | null>;
associatedComponent: BasicComponent | null;
}
Expand All @@ -52,4 +52,4 @@ export type FileSourcePosition = {
end: number;
};

export type BasicComponent = Pick<Component, "componentId" | "extractProps">;
export type BasicComponent = Pick<Component, "id" | "extractProps">;
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { createFileSystemReader } from "@previewjs/vfs";
import createLogger from "pino";
import prettyLogger from "pino-pretty";
import type { ComponentAnalyzer, ComponentAnalyzerFactory } from "./api";
import type { Analyzer, AnalyzerFactory } from "./api";

type ComponentAnalyzerFactoryOptions = Parameters<ComponentAnalyzerFactory>[0];
type AnalyzerFactoryOptions = Parameters<AnalyzerFactory>[0];

export function factoryWithDefaultOptions(
factory: (
options: Required<ComponentAnalyzerFactoryOptions>
) => ComponentAnalyzer
): ComponentAnalyzerFactory {
return (options: ComponentAnalyzerFactoryOptions) =>
factory: (options: Required<AnalyzerFactoryOptions>) => Analyzer
): AnalyzerFactory {
return (options: AnalyzerFactoryOptions) =>
factory({
...options,
reader: options.reader || createFileSystemReader(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from "./api";
export * from "./component-id";
export * from "./factory";
export * from "./previewable-id";
21 changes: 21 additions & 0 deletions analyzer/api/src/previewable-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function generatePreviewableId(options: {
filePath: string;
name: string;
}) {
return `${options.filePath.replace(/\\/g, "/")}:${options.name}`;
}

export function decodePreviewableId(id: string): {
filePath: string;
name: string;
} {
const colonPosition = id.lastIndexOf(":");
if (colonPosition === -1) {
throw new Error(`Invalid previewable ID: "${id}"`);
}
const [filePath, name] = id.split(":") as [string, string];
return {
filePath,
name,
};
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@previewjs/component-analyzer-react",
"name": "@previewjs/analyzer-react",
"version": "0.0.1",
"license": "MIT",
"author": {
Expand All @@ -24,7 +24,7 @@
"test": "vitest"
},
"dependencies": {
"@previewjs/component-analyzer-api": "^0.0.1",
"@previewjs/analyzer-api": "^0.0.1",
"@previewjs/serializable-values": "^7.0.3",
"@previewjs/storybook-helpers": "^4.0.0",
"@previewjs/type-analyzer": "^9.0.0",
Expand Down
Loading

0 comments on commit f1f11ed

Please sign in to comment.