-
Notifications
You must be signed in to change notification settings - Fork 437
Export jinja files as TS module at build time #1296
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
src/snippets/templates.exported.ts |
This file contains hidden or 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 hidden or 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,54 @@ | ||
/* Script that export all jinja files from packages/inference/src/snippets/templates into a TS module. | ||
|
||
Templates are exported in packages/inference/src/snippets/templates.exported.ts. | ||
*/ | ||
import { readFileSync, writeFileSync, readdirSync } from "node:fs"; | ||
import path from "node:path"; | ||
|
||
const TEMPLATES_DIR = path.join(process.cwd(), "src", "snippets", "templates"); | ||
|
||
function exportTemplatesAsCode(): string { | ||
/// language -> client -> templateName -> templateContent | ||
const templates: Record<string, Record<string, Record<string, string>>> = {}; | ||
|
||
// Read all language directories | ||
const languages = readdirSync(TEMPLATES_DIR); | ||
for (const language of languages) { | ||
const languagePath = path.join(TEMPLATES_DIR, language); | ||
if (!readdirSync(languagePath).length) continue; | ||
|
||
templates[language] = {}; | ||
|
||
// Read all client directories | ||
const clients = readdirSync(languagePath); | ||
for (const client of clients) { | ||
const clientPath = path.join(languagePath, client); | ||
if (!readdirSync(clientPath).length) continue; | ||
|
||
templates[language][client] = {}; | ||
|
||
// Read all template files | ||
const files = readdirSync(clientPath); | ||
for (const file of files) { | ||
if (!file.endsWith(".jinja")) continue; | ||
|
||
const templatePath = path.join(clientPath, file); | ||
const templateContent = readFileSync(templatePath, "utf8"); | ||
const templateName = path.basename(file, ".jinja"); | ||
|
||
templates[language][client][templateName] = templateContent; | ||
} | ||
} | ||
} | ||
|
||
const templatesJson = JSON.stringify(templates, null, 2); | ||
return `// Generated file - do not edit directly | ||
export const templates: Record<string, Record<string, Record<string, string>>> = ${templatesJson} as const; | ||
`; | ||
} | ||
|
||
// Generate and write the templates file | ||
const output = exportTemplatesAsCode(); | ||
const outputPath = path.join(process.cwd(), "src", "snippets", "templates.exported.ts"); | ||
writeFileSync(outputPath, output); | ||
console.log("Templates exported successfully! 🚀"); |
This file contains hidden or 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 was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could still be committed
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel that committing it would not bring much value but could be annoying in case of merge conflicts etc. (and adds a bigger diff in PRs)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm yeah i'd rather ignore it