-
-
Notifications
You must be signed in to change notification settings - Fork 534
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
Add option to pass in custom handlebars templates #1268 #1995
Open
gersongoulart
wants to merge
2
commits into
ferdikoomen:main
Choose a base branch
from
provantagex:template-overrides
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { readFileSync } from 'fs'; | ||
import handlebars from 'handlebars'; | ||
|
||
import { preCompileTemplate } from './preCompileTemplate'; | ||
|
||
jest.mock('fs'); | ||
jest.mock('handlebars'); | ||
|
||
describe('preCompileTemplate', () => { | ||
it('returns undefined if no file is provided', () => { | ||
expect(preCompileTemplate()).toBeUndefined(); | ||
}); | ||
|
||
it('reads the file, trims it, and precompiles it', () => { | ||
(readFileSync as jest.Mock).mockReturnValue({ toString: () => ' mock template ' }); | ||
(handlebars.precompile as jest.Mock).mockReturnValue('"mock template spec"'); | ||
|
||
const result = preCompileTemplate('mock-file.hbs'); | ||
|
||
expect(readFileSync).toHaveBeenCalledWith('mock-file.hbs', 'utf8'); | ||
expect(handlebars.precompile).toHaveBeenCalledWith('mock template', { | ||
strict: true, | ||
noEscape: true, | ||
preventIndent: true, | ||
knownHelpersOnly: true, | ||
knownHelpers: { | ||
ifdef: true, | ||
equals: true, | ||
notEquals: true, | ||
containsSpaces: true, | ||
union: true, | ||
intersection: true, | ||
enumerator: true, | ||
escapeComment: true, | ||
escapeDescription: true, | ||
camelCase: true, | ||
}, | ||
}); | ||
expect(result).toBe('mock template spec'); | ||
}); | ||
}); |
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,35 @@ | ||
import { readFileSync } from 'fs'; | ||
import handlebars from 'handlebars'; | ||
import { extname } from 'path'; | ||
|
||
/** | ||
* Precompiles a Handlebars template from a given file. | ||
* | ||
* @param {string | undefined} file - The path to the file containing the Handlebars template. | ||
* @returns {TemplateSpecification | undefined} - The precompiled template as a string, ready to be exported. | ||
*/ | ||
export function preCompileTemplate(file?: string): TemplateSpecification | undefined { | ||
if (!file) return; | ||
|
||
const template = extname(file) === '.hbs' ? readFileSync(file, 'utf8').toString().trim() : file; | ||
const templateSpec = handlebars.precompile(template, { | ||
strict: true, | ||
noEscape: true, | ||
preventIndent: true, | ||
knownHelpersOnly: true, | ||
knownHelpers: { | ||
ifdef: true, | ||
equals: true, | ||
notEquals: true, | ||
containsSpaces: true, | ||
union: true, | ||
intersection: true, | ||
enumerator: true, | ||
escapeComment: true, | ||
escapeDescription: true, | ||
camelCase: true, | ||
}, | ||
}); | ||
|
||
return eval(`(function(){return ${templateSpec} }());`); | ||
} |
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 |
---|---|---|
@@ -1,21 +1,74 @@ | ||
import Handlebars from 'handlebars/runtime'; | ||
|
||
import { HttpClient } from '../HttpClient'; | ||
import * as preCompiler from './preCompileTemplate'; | ||
import { registerHandlebarTemplates } from './registerHandlebarTemplates'; | ||
|
||
jest.mock('handlebars/runtime', () => ({ | ||
template: jest.fn((file: string) => file), | ||
registerPartial: jest.fn((file: string) => file), | ||
registerHelper: jest.fn((file: string) => file), | ||
})); | ||
|
||
jest.mock('./preCompileTemplate', () => ({ | ||
preCompileTemplate: jest.fn((file: string) => file), | ||
})); | ||
|
||
describe('registerHandlebarTemplates', () => { | ||
it('should return correct templates', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should register handlebar templates', () => { | ||
registerHandlebarTemplates({ | ||
httpClient: HttpClient.FETCH, | ||
useOptions: false, | ||
useUnionTypes: false, | ||
}); | ||
|
||
expect(Handlebars.template).toHaveBeenCalled(); | ||
expect(Handlebars.registerPartial).toHaveBeenCalled(); | ||
expect(preCompiler.preCompileTemplate).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should return default templates', () => { | ||
const templates = registerHandlebarTemplates({ | ||
httpClient: HttpClient.FETCH, | ||
useOptions: false, | ||
useUnionTypes: false, | ||
}); | ||
|
||
expect(templates.index).toHaveProperty('compiler'); | ||
expect(templates.exports.model).toHaveProperty('compiler'); | ||
expect(templates.exports.schema).toHaveProperty('compiler'); | ||
expect(templates.exports.service).toHaveProperty('compiler'); | ||
expect(templates.core.settings).toHaveProperty('compiler'); | ||
expect(templates.core.apiError).toHaveProperty('compiler'); | ||
expect(templates.core.apiRequestOptions).toHaveProperty('compiler'); | ||
expect(templates.core.apiResult).toHaveProperty('compiler'); | ||
expect(templates.core.request).toHaveProperty('compiler'); | ||
}); | ||
|
||
it('should allow template overrides', () => { | ||
const templates = registerHandlebarTemplates({ | ||
httpClient: HttpClient.FETCH, | ||
useOptions: false, | ||
useUnionTypes: false, | ||
templateOverrides: { | ||
index: 'override', | ||
exportService: 'override', | ||
settings: 'override', | ||
}, | ||
}); | ||
expect(templates.index).toBeDefined(); | ||
expect(templates.exports.model).toBeDefined(); | ||
expect(templates.exports.schema).toBeDefined(); | ||
expect(templates.exports.service).toBeDefined(); | ||
expect(templates.core.settings).toBeDefined(); | ||
expect(templates.core.apiError).toBeDefined(); | ||
expect(templates.core.apiRequestOptions).toBeDefined(); | ||
expect(templates.core.apiResult).toBeDefined(); | ||
expect(templates.core.request).toBeDefined(); | ||
|
||
expect(templates.index).toBe('override'); | ||
expect(templates.exports.model).toHaveProperty('compiler'); | ||
expect(templates.exports.schema).toHaveProperty('compiler'); | ||
expect(templates.exports.service).toBe('override'); | ||
expect(templates.core.settings).toBe('override'); | ||
expect(templates.core.apiError).toHaveProperty('compiler'); | ||
expect(templates.core.apiRequestOptions).toHaveProperty('compiler'); | ||
expect(templates.core.apiResult).toHaveProperty('compiler'); | ||
expect(templates.core.request).toHaveProperty('compiler'); | ||
}); | ||
}); |
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.
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.
eval
seems safe enough, although I suspect code quality tools will complain.Here is an another (extremely hacky) alternative using a temp file which gets resolved
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.
Thanks for the observation. Yeah, code quality tools will warn about the safety of this to prevent developers from shipping code that could be exploited. Seeing this is a library and the provider of the template to be evaluated is the developer using the library, I think this is a safe implementation of the eval function — and the cleanest solution I could find. Writing and reading to disk on every precompilation would significantly affect performance and might encounter problems with disk access permissions.
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.
Same thoughts. I wish we could use some other api besides
handlebars.precompile
. But I could not find any that works.