From 16eae2807a30faa6626001bda3e4ab7c515a7d1e Mon Sep 17 00:00:00 2001 From: XuluWarrior Date: Sat, 19 Jun 2021 20:47:14 +0100 Subject: [PATCH] fix(theme): fix serving and exporting local themes (#537) --- lib/builder.js | 2 +- lib/render-html.js | 10 ++++++---- lib/render-html.test.js | 22 +++++++++++++++++----- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/builder.js b/lib/builder.js index efca364e..968decaa 100644 --- a/lib/builder.js +++ b/lib/builder.js @@ -3,7 +3,7 @@ const themeServer = const fs = require('fs'); const request = require('superagent'); const chalk = require('chalk'); -const renderHtml = require('./render-html'); +const renderHtml = require('./render-html').default; const denormalizeTheme = (value) => { return value.match(/jsonresume-theme-(.*)/)[1]; diff --git a/lib/render-html.js b/lib/render-html.js index c5065f80..373599ba 100644 --- a/lib/render-html.js +++ b/lib/render-html.js @@ -10,10 +10,12 @@ export default async ({ resume, themePath }) => { const cwd = process.cwd(); let path; if (themePath[0] === '.') { - path = tryResolve(path.join(cwd, themePath), { paths: [cwd] }); - throw new Error( - `Theme ${themePath} could not be resolved relative to ${cwd}`, - ); + path = tryResolve(require('path').join(cwd, themePath), { paths: [cwd] }); + if (!path) { + throw new Error( + `Theme ${themePath} could not be resolved relative to ${cwd}`, + ); + } } if (!path) { path = tryResolve(themePath, { paths: [cwd] }); diff --git a/lib/render-html.test.js b/lib/render-html.test.js index 78038ad9..b068d12f 100644 --- a/lib/render-html.test.js +++ b/lib/render-html.test.js @@ -5,9 +5,6 @@ describe('renderHTML', () => { const originalRequireResolve = require.resolve; const mockThemePath = 'mock/path/to/jsonresume-theme-even'; require.resolve = (...args) => { - if (args[0] === 'jsonresume-theme-even') { - return mockThemePath; - } if (args[0] === 'jsonresume-theme-even') { return mockThemePath; } @@ -25,13 +22,13 @@ describe('renderHTML', () => { }, }; - it('should reject when theme is not availlable', async () => { + it('should reject when theme is not available', async () => { await expect( renderHTML({ resume, themePath: 'unknown' }), ).rejects.toBeTruthy(); }); - describe('should render html when theme is availlable', () => { + describe('should render html when theme is available', () => { it('with long theme name', async () => { expect( await renderHTML({ resume, themePath: 'jsonresume-theme-even' }), @@ -43,5 +40,20 @@ describe('renderHTML', () => { '', ); }); + + it('should reject theme with invalid path', async () => { + await expect( + renderHTML({ resume, themePath: './unknown' }), + ).rejects.toBeTruthy(); + }); + + it('with local theme path', async () => { + expect( + await renderHTML({ + resume, + themePath: './node_modules/jsonresume-theme-even', + }), + ).toStartWith(''); + }); }); });