Skip to content

Commit 85e76be

Browse files
authored
squash! test: unit tests for the cli (#459) (#469)
fixing theme resolution on node after v10
1 parent 738387e commit 85e76be

1 file changed

Lines changed: 25 additions & 19 deletions

File tree

lib/render-html.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1-
import path from 'path';
1+
const tryResolve = (...args) => {
2+
try {
3+
return require.resolve(...args);
4+
} catch (err) {
5+
return false;
6+
}
7+
};
28

39
export default async ({ resume, themePath }) => {
410
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+
);
2228
}
23-
const theme = require(resolvedThemePath);
29+
const theme = require(path);
2430
if (typeof theme?.render !== 'function') {
2531
throw new Error('theme.render is not a function');
2632
}

0 commit comments

Comments
 (0)