From c357b01b82215bf79964309a502be63f37c46a0a Mon Sep 17 00:00:00 2001 From: jlenon7 Date: Sun, 25 Feb 2024 20:26:53 +0000 Subject: [PATCH] feat(template): remove relative path --- package-lock.json | 4 ++-- package.json | 2 +- src/views/ViewImpl.ts | 28 ++++++++++++++++++++++++---- tests/unit/views/ViewImplTest.ts | 28 ++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0a7fe41..0e26fd4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/view", - "version": "4.16.0", + "version": "4.17.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/view", - "version": "4.16.0", + "version": "4.17.0", "license": "MIT", "dependencies": { "edge.js": "^6.0.1" diff --git a/package.json b/package.json index 7ab3d50..5ae9098 100644 --- a/package.json +++ b/package.json @@ -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 ", diff --git a/src/views/ViewImpl.ts b/src/views/ViewImpl.ts index 8577c78..36f0d2b 100644 --- a/src/views/ViewImpl.ts +++ b/src/views/ViewImpl.ts @@ -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()) } @@ -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 diff --git a/tests/unit/views/ViewImplTest.ts b/tests/unit/views/ViewImplTest.ts index 6140fb2..83a69b6 100644 --- a/tests/unit/views/ViewImplTest.ts +++ b/tests/unit/views/ViewImplTest.ts @@ -290,6 +290,20 @@ export default class ViewImplTest { assert.isTrue(content.includes('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 ')) + } + @Test() public async shouldThrowAlreadyExistComponentExceptionIfTryingToCreateWithTheSameName({ assert }: Context) { assert.throws(() => View.createComponent('button', ''), AlreadyExistComponentException) @@ -353,6 +367,20 @@ export default class ViewImplTest { assert.isTrue(content.includes('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 ')) + } + @Test() public async shouldNotThrowErrorsWhenCreatingATemplateByFilePathThatDoesNotExist({ assert }: Context) { assert.doesNotThrow(() => View.createTemplateByPath('not-found', Path.fixtures('not-found.edge')))