Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: path resolution for html export #494

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions lib/render-html.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
const tryResolve = (...args) => {
import { join } from 'path';

const tryRequire = (...args) => {
try {
return require.resolve(...args);
return require(...args);
} catch (err) {
return false;
}
};

export default async ({ resume, themePath }) => {
const cwd = process.cwd();
let path;
let theme;
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] });
theme = tryRequire(join(cwd, themePath));
} else {
theme = tryRequire(themePath);
}
if (!path && /^[a-z0-9]/i.test(path)) {
path = tryResolve(`jsonresume-theme-${themePath}`, { paths: [cwd] });
if (!theme && /^[a-z0-9-]+$/i.test(themePath)) {
theme = tryRequire(`jsonresume-theme-${themePath}`);
}
if (!path) {
if (!theme) {
throw new Error(
`theme path ${themePath} could not be resolved from current working directory`,
`theme path ${themePath} could not be resolved. cwd is ${cwd}`,
);
}
const theme = require(path);
if (typeof theme?.render !== 'function') {
throw new Error('theme.render is not a function');
}
Expand Down
53 changes: 32 additions & 21 deletions lib/render-html.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import renderHTML from './render-html';

describe('renderHTML', () => {
beforeAll(() => {
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;
}
return originalRequireResolve.apply(require, ...args);
jest.mock(
'jsonresume-theme-even',
() => {
return {
render: () => 'hello from theme even',
};
require.cache[mockThemePath] = {
render: () => 'here-is-your-mocked-theme',
},
{ virtual: true },
);
jest.mock(
'/the/mocked/cwd/some-local-theme',
() => {
return {
render: () => 'hello from the local mocked theme',
};
},
{ virtual: true },
);
describe('renderHTML', () => {
beforeAll(() => {
const localPath = '/the/mocked/cwd/';
process.cwd = jest.fn().mockReturnValue(localPath);
});
const resume = {
basics: {
Expand All @@ -31,17 +37,22 @@ describe('renderHTML', () => {
).rejects.toBeTruthy();
});

describe('should render html when theme is availlable', () => {
it('with long theme name', async () => {
describe('when theme is availlable', () => {
it('should resolve from cwd when themePath starts with a period', async () => {
expect(
await renderHTML({ resume, themePath: './some-local-theme' }),
).toMatchInlineSnapshot(`"hello from the local mocked theme"`);
});
it('should render html with long theme name', async () => {
expect(
await renderHTML({ resume, themePath: 'jsonresume-theme-even' }),
).toStartWith('<!doctype html>');
).toMatchInlineSnapshot(`"hello from theme even"`);
});

it('with short theme name', async () => {
expect(await renderHTML({ resume, themePath: 'even' })).toStartWith(
'<!doctype html>',
);
it('should render html with short theme name', async () => {
expect(
await renderHTML({ resume, themePath: 'even' }),
).toMatchInlineSnapshot(`"hello from theme even"`);
});
});
});