|
1 | | -import path from 'path'; |
| 1 | +const tryResolve = (...args) => { |
| 2 | + try { |
| 3 | + return require.resolve(...args); |
| 4 | + } catch (err) { |
| 5 | + return false; |
| 6 | + } |
| 7 | +}; |
2 | 8 |
|
3 | 9 | export default async ({ resume, themePath }) => { |
4 | 10 | const cwd = process.cwd(); |
5 | | - let resolvedThemePath = themePath; |
6 | | - try { |
7 | | - if (themePath[0] === '.') { |
8 | | - resolvedThemePath = path.join(process.cwd(), themePath, 'index.js'); |
9 | | - } else { |
10 | | - try { |
11 | | - resolvedThemePath = require.resolve(themePath, { |
12 | | - paths: process.cwd(), |
13 | | - }); |
14 | | - } catch (err) { |
15 | | - resolvedThemePath = require.resolve(`jsonresume-theme-${themePath}`, { |
16 | | - paths: process.cwd(), |
17 | | - }); |
18 | | - } |
19 | | - } |
20 | | - } catch (err) { |
21 | | - throw new Error(`Theme ${themePath} could not be resolved from ${cwd}`); |
| 11 | + let path; |
| 12 | + if (themePath[0] === '.') { |
| 13 | + path = tryResolve(path.join(cwd, themePath), { paths: [cwd] }); |
| 14 | + throw new Error( |
| 15 | + `Theme ${themePath} could not be resolved relative to ${cwd}`, |
| 16 | + ); |
| 17 | + } |
| 18 | + if (!path) { |
| 19 | + path = tryResolve(themePath, { paths: [cwd] }); |
| 20 | + } |
| 21 | + if (!path && /^[a-z0-9]/i.test(path)) { |
| 22 | + path = tryResolve(`jsonresume-theme-${themePath}`, { paths: [cwd] }); |
| 23 | + } |
| 24 | + if (!path) { |
| 25 | + throw new Error( |
| 26 | + `theme path ${themePath} could not be resolved from current working directory`, |
| 27 | + ); |
22 | 28 | } |
23 | | - const theme = require(resolvedThemePath); |
| 29 | + const theme = require(path); |
24 | 30 | if (typeof theme?.render !== 'function') { |
25 | 31 | throw new Error('theme.render is not a function'); |
26 | 32 | } |
|
0 commit comments