-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
handling errors properly #62
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,59 +5,63 @@ export default function ({ locals = {}, filters = {}, useMetadata = false } = {} | |
const opts = arguments[0] || {} | ||
|
||
return (files, metalsmith, done) => { | ||
setImmediate(done) | ||
|
||
// extend with metalsmith global metadata | ||
if (useMetadata) { | ||
locals = Object.assign({}, locals, metalsmith.metadata()) | ||
} | ||
|
||
Object.keys(files).forEach((file) => { | ||
if (!/\.(pug|jade)/.test(path.extname(file))) { | ||
return | ||
} | ||
try { | ||
Object.keys(files).forEach((file) => { | ||
if (!/\.(pug|jade)/.test(path.extname(file))) { | ||
return | ||
} | ||
|
||
let data = files[file] | ||
let dir = path.dirname(file) | ||
let name = path.basename(file, path.extname(file)) | ||
let data = files[file] | ||
let dir = path.dirname(file) | ||
let name = path.basename(file, path.extname(file)) | ||
|
||
// do we need to add an extension? | ||
if (path.extname(name) === '') { | ||
name = path.basename(file, path.extname(file)) + '.html' | ||
} | ||
// do we need to add an extension? | ||
if (path.extname(name) === '') { | ||
name = path.basename(file, path.extname(file)) + '.html' | ||
} | ||
|
||
if (dir !== '.') { | ||
name = `${dir}/${name}` | ||
} | ||
if (dir !== '.') { | ||
name = `${dir}/${name}` | ||
} | ||
|
||
const filename = path.join(metalsmith.source(), file) | ||
const filename = path.join(metalsmith.source(), file) | ||
|
||
// also use the file's own data | ||
if (useMetadata) { | ||
locals = Object.assign(locals, data) | ||
} | ||
// also use the file's own data | ||
if (useMetadata) { | ||
locals = Object.assign(locals, data) | ||
} | ||
|
||
// assign filters | ||
if (filters) { | ||
Object.keys(filters).forEach((filter) => (pug.filters[filter] = filters[filter])) | ||
} | ||
// assign filters | ||
if (filters) { | ||
Object.keys(filters).forEach((filter) => (pug.filters[filter] = filters[filter])) | ||
} | ||
|
||
const options = Object.assign(opts, { | ||
filename, | ||
locals | ||
}) | ||
const options = Object.assign(opts, { | ||
filename, | ||
locals | ||
}) | ||
|
||
// render | ||
let str = pug.compile(data.contents.toString(), options)(locals) | ||
// render | ||
let str = pug.compile(data.contents.toString(), options)(locals) | ||
|
||
// convert to a buffer | ||
data.contents = new Buffer(str) | ||
// convert to a buffer | ||
data.contents = new Buffer(str) | ||
|
||
// remove from global files object | ||
delete files[file] | ||
// remove from global files object | ||
delete files[file] | ||
|
||
// assign the newly created file | ||
files[name] = data | ||
}) | ||
// assign the newly created file | ||
files[name] = data | ||
}) | ||
|
||
setImmediate(done) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can just call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope, |
||
} catch (err) { | ||
setImmediate(done, err) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
should*throw |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only wrap the actual pug calls, not the whole logic block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that was my initial approach too.
Buuut, the nice thing about
throw
is that it "breaks"forEach
likebreak
does with a classic loop.Hence I decided to wrap the whole block.
Otherwise we would need to accumulate errors in an array...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I prefer to bail-out on first error, but I can quickly change it to try all files.