Skip to content

Commit

Permalink
Merge pull request #32 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat(template): remove relative path
  • Loading branch information
jlenon7 committed Feb 25, 2024
2 parents a6d375d + c357b01 commit 1333ef6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/view",
"version": "4.16.0",
"version": "4.17.0",
"description": "The Athenna template engine. Built on top of Edge.js.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
28 changes: 24 additions & 4 deletions src/views/ViewImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,18 @@ export class ViewImpl {
* View.createComponentByPath('myTemplate', path)
* ```
*/
public createComponentByPath(name: string, componentPath: string): ViewImpl {
const file = new File(componentPath)
public createComponentByPath(name: string, path: string): ViewImpl {
if (!isAbsolute(path)) {
debug(
'Path %s for view disk is not absolute and is going to be resolved using cwd %s.',
path,
Path.pwd()
)

path = resolve(Path.pwd(), path)
}

const file = new File(path)

return this.createTemplate(name, file.getContentAsStringSync())
}
Expand Down Expand Up @@ -390,8 +400,18 @@ export class ViewImpl {
* View.createTemplateByPath('myTemplate', path)
* ```
*/
public createTemplateByPath(name: string, templatePath: string): ViewImpl {
const file = new File(templatePath, Buffer.from(''))
public createTemplateByPath(name: string, path: string): ViewImpl {
if (!isAbsolute(path)) {
debug(
'Path %s for view disk is not absolute and is going to be resolved using cwd %s.',
path,
Path.pwd()
)

path = resolve(Path.pwd(), path)
}

const file = new File(path, Buffer.from(''))

if (!file.fileExists) {
return this
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/views/ViewImplTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,20 @@ export default class ViewImplTest {
assert.isTrue(content.includes('lenon <lenon>'))
}

@Test()
public async shouldBeAbleToCreateAComponentByARelativeFilePath({ assert }: Context) {
View.createComponentByPath('ui.copyright', 'tests/fixtures/views/components/copyright.edge')

const content = await View.render('ui.copyright', {
package: 'view',
author: 'lenon',
email: 'lenon'
})

assert.isTrue(content.includes('@athenna/view'))
assert.isTrue(content.includes('lenon <lenon>'))
}

@Test()
public async shouldThrowAlreadyExistComponentExceptionIfTryingToCreateWithTheSameName({ assert }: Context) {
assert.throws(() => View.createComponent('button', ''), AlreadyExistComponentException)
Expand Down Expand Up @@ -353,6 +367,20 @@ export default class ViewImplTest {
assert.isTrue(content.includes('lenon <lenon>'))
}

@Test()
public async shouldBeAbleToCreateATemplateByRelativeFilePath({ assert }: Context) {
View.createTemplateByPath('ui.copyright', 'tests/fixtures/views/components/copyright.edge')

const content = await View.render('ui.copyright', {
package: 'view',
author: 'lenon',
email: 'lenon'
})

assert.isTrue(content.includes('@athenna/view'))
assert.isTrue(content.includes('lenon <lenon>'))
}

@Test()
public async shouldNotThrowErrorsWhenCreatingATemplateByFilePathThatDoesNotExist({ assert }: Context) {
assert.doesNotThrow(() => View.createTemplateByPath('not-found', Path.fixtures('not-found.edge')))
Expand Down

0 comments on commit 1333ef6

Please sign in to comment.