Skip to content

Commit

Permalink
squash! test: unit tests for the cli (#459) (#469)
Browse files Browse the repository at this point in the history
fixing theme resolution on node after v10
  • Loading branch information
antialias authored Dec 17, 2020
1 parent 738387e commit 85e76be
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions lib/render-html.js
Original file line number Diff line number Diff line change
@@ -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');
}
Expand Down

0 comments on commit 85e76be

Please sign in to comment.