Skip to content

Commit

Permalink
Merge 626d25b into 6604a57
Browse files Browse the repository at this point in the history
  • Loading branch information
ismay committed Apr 7, 2019
2 parents 6604a57 + 626d25b commit 5d66bc2
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 21 deletions.
2 changes: 2 additions & 0 deletions lib/__snapshots__/index.test.js.snap
@@ -1,5 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`metalsmith-layouts should prefix rendering errors with the filename 1`] = `"index.html: Something went wrong while rendering"`;

exports[`metalsmith-layouts should return an error for an invalid pattern 1`] = `"invalid pattern, the pattern option should be a string or array of strings. See https://www.npmjs.com/package/metalsmith-layouts#pattern"`;

exports[`metalsmith-layouts should return an error when there are no valid files to process 1`] = `"no files to process. See https://www.npmjs.com/package/metalsmith-layouts#no-files-to-process"`;
21 changes: 21 additions & 0 deletions lib/get-transformer.js
@@ -0,0 +1,21 @@
const jstransformer = require('jstransformer');
const toTransformer = require('inputformat-to-jstransformer');

/**
* Gets jstransformer for an extension, and caches them
*/

const cache = {};

function getTransformer(ext) {
if (ext in cache) {
return cache[ext];
}

const transformer = toTransformer(ext);
cache[ext] = transformer ? jstransformer(transformer) : false;

return cache[ext];
}

module.exports = getTransformer;
22 changes: 2 additions & 20 deletions lib/index.js
Expand Up @@ -2,25 +2,7 @@ const debug = require('debug')('metalsmith-layouts');
const match = require('multimatch');
const path = require('path');
const isUtf8 = require('is-utf8');
const jstransformer = require('jstransformer');
const toTransformer = require('inputformat-to-jstransformer');

/**
* Gets jstransformer for an extension, and caches them
*/

const cache = {};

function getTransformer(ext) {
if (ext in cache) {
return cache[ext];
}

const transformer = toTransformer(ext);
cache[ext] = transformer ? jstransformer(transformer) : false;

return cache[ext];
}
const getTransformer = require('./get-transformer');

/**
* Resolves layouts, in the following order:
Expand Down Expand Up @@ -68,7 +50,7 @@ function render({ filename, files, metadata, settings, metalsmith }) {
// Prepend error message with file path
// eslint-disable-next-line no-param-reassign
err.message = `${filename}: ${err.message}`;
return Promise.reject(err);
throw err;
});
}

Expand Down
22 changes: 22 additions & 0 deletions lib/index.test.js
Expand Up @@ -7,6 +7,10 @@ const path = require('path');
const plugin = require('./index');

describe('metalsmith-layouts', () => {
beforeEach(() => {
jest.resetModules();
});

it('should apply a single layout to a single file', done => {
const base = path.join(process.cwd(), 'test', 'fixtures', 'single-file');
const actual = path.join(base, 'build');
Expand Down Expand Up @@ -327,4 +331,22 @@ describe('metalsmith-layouts', () => {
return done();
});
});

it('should prefix rendering errors with the filename', done => {
jest.doMock('./get-transformer', () =>
jest.fn(() => ({
renderFileAsync: () => Promise.reject(new Error('Something went wrong while rendering'))
}))
);
const plugin = require('./index'); // eslint-disable-line global-require, no-shadow

const base = path.join(process.cwd(), 'test', 'fixtures', 'rendering-error');
const metalsmith = new Metalsmith(base);

return metalsmith.use(plugin()).build(err => {
expect(err).toBeInstanceOf(Error);
expect(err.message).toMatchSnapshot();
done();
});
});
});
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -43,7 +43,7 @@
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-import": "^2.13.0",
"husky": "^0.14.3",
"jest": "^23.5.0",
"jest": "^24.7.1",
"jstransformer-handlebars": "^1.0.0",
"jstransformer-qejs": "^0.2.0",
"lint-staged": "^7.0.0",
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/rendering-error/layouts/standard.hbs
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head></head>
<body>
{{{ contents }}}
</body>
</html>
4 changes: 4 additions & 0 deletions test/fixtures/rendering-error/src/index.html
@@ -0,0 +1,4 @@
---
layout: standard.hbs
---
<h1>The title</h1>

0 comments on commit 5d66bc2

Please sign in to comment.