Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support adding pages #73

Merged
merged 8 commits into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 104 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 3 additions & 10 deletions packages/studio-plugin/src/ParsingOrchestrator.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import path from "path";
import { FileMetadata, PageState } from "./types";
import { FileMetadata, PageState, UserPaths, StudioData } from "./types";
import fs from "fs";
import ComponentFile from "./sourcefiles/ComponentFile";
import ModuleFile from "./sourcefiles/ModuleFile";
import PageFile from "./sourcefiles/PageFile";
import SiteSettingsFile, { SiteSettings } from "./sourcefiles/SiteSettingsFile";
import { Project } from "ts-morph";
import typescript from "typescript";
import { StudioData } from "./types/StudioData";

export function createTsMorphProject() {
return new Project({
Expand All @@ -27,14 +26,7 @@ export default class ParsingOrchestrator {
private project: Project;

/** All paths are assumed to be absolute. */
constructor(
private paths: {
components: string;
pages: string;
modules: string;
siteSettings: string;
}
) {
constructor(private paths: UserPaths) {
this.project = createTsMorphProject();
this.getFileMetadata = this.getFileMetadata.bind(this);
this.filepathToFileMetadata = this.setFilepathToFileMetadata();
Expand All @@ -55,6 +47,7 @@ export default class ParsingOrchestrator {
pageNameToPageState,
UUIDToFileMetadata,
siteSettings,
userPaths: this.paths,
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/studio-plugin/src/parsers/ComponentTreeParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
StandardOrModuleComponentState,
} from "../types/State";
import { v4 } from "uuid";
import { FileMetadataKind, PropValues } from "../types";
import { FileMetadataKind } from "../types";
import StudioSourceFileParser from "./StudioSourceFileParser";
import StaticParsingHelpers from "./helpers/StaticParsingHelpers";
import TypeGuards from "./helpers/TypeGuards";
Expand Down
5 changes: 3 additions & 2 deletions packages/studio-plugin/src/parsers/getStudioPaths.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import path from "path";
import { UserPaths } from "../types";

/**
* Given an absolute path to the user's src folder, determine the filepaths Studio will
* use for parsing files.
*
* @param pathToSrc An absolute path to the src folder
* @param pathToSrc - An absolute path to the src folder
*/
export default function getStudioPaths(pathToSrc: string) {
export default function getStudioPaths(pathToSrc: string): UserPaths {
return {
pages: path.join(pathToSrc, "pages"),
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
modules: path.join(pathToSrc, "modules"),
Expand Down
2 changes: 2 additions & 0 deletions packages/studio-plugin/src/sourcefiles/PageFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ export default class PageFile {
absPathDefaultImports
);
const cssImports = this.studioSourceFileParser.parseCssImports();
const filepath = this.studioSourceFileParser.getFilepath();
return {
componentTree,
cssImports,
filepath,
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/studio-plugin/src/types/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PropValues } from "./PropValues";
export type PageState = {
componentTree: ComponentState[];
cssImports: string[];
filepath: string;
tatimblin marked this conversation as resolved.
Show resolved Hide resolved
};

export type ComponentState =
Expand Down
2 changes: 2 additions & 0 deletions packages/studio-plugin/src/types/StudioData.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { SiteSettings } from "../sourcefiles/SiteSettingsFile";
import { ComponentMetadata } from "./ComponentMetadata";
import { PageState } from "./State";
import { UserPaths } from "./UserPaths";

export interface StudioData {
pageNameToPageState: Record<string, PageState>;
UUIDToFileMetadata: Record<string, ComponentMetadata>;
siteSettings?: SiteSettings;
userPaths: UserPaths;
}
14 changes: 14 additions & 0 deletions packages/studio-plugin/src/types/UserPaths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Absolute paths for files and directories in the user's file system that are
* relevant for Studio.
*/
export interface UserPaths {
/** The absolute path to the directory with the user's components. */
components: string;
/** The absolute path to the directory with the user's pages. */
pages: string;
/** The absolute path to the directory with the user's modules. */
modules: string;
/** The absolute path to the file with the user's site settings. */
siteSettings: string;
}
1 change: 1 addition & 0 deletions packages/studio-plugin/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from "./ModuleMetadata";
export * from "./FileMetadata";
export * from "./State";
export * from "./StudioData";
export * from "./UserPaths";
2 changes: 2 additions & 0 deletions packages/studio-plugin/tests/ParsingOrchestrator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe("aggregates data as expected", () => {
}),
],
cssImports: [],
filepath: expect.anything(),
},
pageWithModules: {
componentTree: [
Expand All @@ -80,6 +81,7 @@ describe("aggregates data as expected", () => {
}),
],
cssImports: [],
filepath: expect.anything(),
},
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ describe("getPageState", () => {
]);
});

it("correctly gets filepath", () => {
const pageFile = createPageFile("shortFragmentSyntaxPage");
const result = pageFile.getPageState();

expect(result.filepath).toEqual(getPagePath("shortFragmentSyntaxPage"));
});

describe("throws errors", () => {
it("throws an error when no return statement is found in the default export", () => {
const pageFile = createPageFile("noReturnStatementPage");
Expand Down
Loading