diff --git a/lib/render-html.js b/lib/render-html.js index 0bfa642c..c5065f80 100644 --- a/lib/render-html.js +++ b/lib/render-html.js @@ -1,26 +1,32 @@ -import path from 'path'; +const tryResolve = (...args) => { + try { + return require.resolve(...args); + } catch (err) { + return false; + } +}; export default async ({ resume, themePath }) => { const cwd = process.cwd(); - let resolvedThemePath = themePath; - try { - if (themePath[0] === '.') { - resolvedThemePath = path.join(process.cwd(), themePath, 'index.js'); - } else { - try { - resolvedThemePath = require.resolve(themePath, { - paths: process.cwd(), - }); - } catch (err) { - resolvedThemePath = require.resolve(`jsonresume-theme-${themePath}`, { - paths: process.cwd(), - }); - } - } - } catch (err) { - throw new Error(`Theme ${themePath} could not be resolved from ${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}`, + ); + } + if (!path) { + path = tryResolve(themePath, { paths: [cwd] }); + } + if (!path && /^[a-z0-9]/i.test(path)) { + path = tryResolve(`jsonresume-theme-${themePath}`, { paths: [cwd] }); + } + if (!path) { + throw new Error( + `theme path ${themePath} could not be resolved from current working directory`, + ); } - const theme = require(resolvedThemePath); + const theme = require(path); if (typeof theme?.render !== 'function') { throw new Error('theme.render is not a function'); }