-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin): create and bundle client templates (#420)
Co-authored-by: Matt Kilpatrick <mkilpatrick@yext.com> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
827c8db
commit ba4647a
Showing
19 changed files
with
314 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
import SourceFileParser, { | ||
createTsMorphProject, | ||
} from "../parsers/sourceFileParser.js"; | ||
import TemplateParser from "../parsers/templateParser.js"; | ||
import { ProjectStructure } from "../project/structure.js"; | ||
import { readdir } from "node:fs/promises"; | ||
import { logErrorAndExit } from "../../../util/logError.js"; | ||
|
||
/** | ||
* Creates a corresponding template.client.tsx for each template.tsx. | ||
* @param projectStructure | ||
*/ | ||
export const makeClientFiles = async (projectStructure: ProjectStructure) => { | ||
try { | ||
const templatePaths = projectStructure.getTemplatePaths(); | ||
for (const templatePath of templatePaths) { | ||
(await readdir(templatePath.getAbsolutePath(), { withFileTypes: true })) | ||
.filter((dirent) => !dirent.isDirectory()) | ||
.map((file) => file.name) | ||
.filter( | ||
(f) => | ||
f !== "_client17.tsx" && | ||
f !== "_client.tsx" && | ||
f !== "_server.tsx" && | ||
!f.includes(".client") | ||
) | ||
.forEach(async (template) => { | ||
const templeFilePath = path.join(templatePath.path, template); | ||
generateAndSaveClientHydrationTemplates(templeFilePath); | ||
}); | ||
} | ||
} catch (err) { | ||
logErrorAndExit("Failed to make client templates."); | ||
await removeHydrationClientFiles(projectStructure); | ||
} | ||
}; | ||
|
||
/** | ||
* Reads template file and parses code for the hydration client file. | ||
* Saves parsed code into file at path. | ||
* @param path src/templates/<templateName>.tsx | ||
*/ | ||
const generateAndSaveClientHydrationTemplates = (templatePath: string) => { | ||
const clientTemplatePath = getClientPath(templatePath); | ||
fs.writeFileSync(clientTemplatePath, ""); | ||
const sfp = new SourceFileParser(templatePath, createTsMorphProject()); | ||
const newSfp = new SourceFileParser( | ||
clientTemplatePath, | ||
createTsMorphProject() | ||
); | ||
const templateParser = new TemplateParser(sfp).makeClientTemplateFromSfp( | ||
newSfp | ||
); | ||
templateParser.sourceFile.save(); | ||
}; | ||
|
||
/** | ||
* @param templatePath /src/templates/location.tsx | ||
* @returns clientPath /src/templates/location.client.tsx | ||
*/ | ||
const getClientPath = (templatePath: string): string => { | ||
const parsedPath = path.parse(templatePath); | ||
parsedPath.name = parsedPath.name + ".client"; | ||
parsedPath.base = parsedPath.name + parsedPath.ext; | ||
return path.format(parsedPath); | ||
}; | ||
|
||
/** | ||
* Removes any generated [template].client files. | ||
* @param projectStructure | ||
*/ | ||
export const removeHydrationClientFiles = async ( | ||
projectStructure: ProjectStructure | ||
) => { | ||
const templatePaths = projectStructure.getTemplatePaths(); | ||
for (const templatePath of templatePaths) { | ||
(await readdir(templatePath.getAbsolutePath(), { withFileTypes: true })) | ||
.filter((dirent) => !dirent.isDirectory()) | ||
.map((file) => file.name) | ||
.filter( | ||
(f) => | ||
f !== "_client17.tsx" && | ||
f !== "_client.tsx" && | ||
f !== "_server.tsx" && | ||
f.includes(".client") | ||
) | ||
.forEach(async (template) => { | ||
const clientPath = path.join( | ||
projectStructure.config.rootFolders.source, | ||
projectStructure.config.subfolders.templates, | ||
template | ||
); | ||
if (fs.existsSync(clientPath)) { | ||
fs.rmSync(clientPath); | ||
} | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.