Skip to content

Commit

Permalink
feat: better jsdoc typehints & defaults mgmt
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Feb 15, 2022
1 parent 0392d6f commit ae06fb9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
37 changes: 27 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,31 @@ function validate({ filename, files }) {
}

/**
* Plugin, the main plugin used by metalsmith
* @typedef {Object} Options
* @property {string} [pattern='**'] (*optional*) Limit the files to process by 1 or more glob patterns. Defaults to `'**'` (all)
* @property {Object} [engineOptions={}] (*optional*) Pass options to the jstransformer templating engine that's rendering your files. The default is `{}`
* @property {boolean} [suppressNoFilesError=false] (*optional*) Decide whether to ignore an error indicating that the plugin didn't find any matching files to process. The default is `false`
* @property {boolean} [setFilename=false] (*optional*) Some templating engines, like [pug](https://github.com/pugjs/pug), need a `filename` property to be present in the options to be able to process relative includes, extends, etc. Setting this option to `true` will add the current filename to the options passed to each jstransformer. The default is `false`
**/

/** @type {Options} */
const defaultOptions = {
pattern: '**',
engineOptions: {},
suppressNoFilesError: false,
setFilename: false
}

/**
* A metalsmith plugin for in-place templating
* @param {Options} options
* @returns {import('metalsmith').Plugin}
*/
function initializeInPlace(options = defaultOptions) {
const settings = Object.assign({}, defaultOptions, options)

module.exports = (options) =>
function inPlace(files, metalsmith, done) {
const defaults = {
pattern: '**',
engineOptions: {},
suppressNoFilesError: false,
setFilename: false
}
const settings = Object.assign({}, defaults, options)
return function inPlace(files, metalsmith, done) {
debug('Running with options %O', settings)

// Check whether the pattern option is valid
if (!(typeof settings.pattern === 'string' || Array.isArray(settings.pattern))) {
Expand All @@ -133,6 +146,7 @@ module.exports = (options) =>
}

// throw update error in case users didn't see the peerDependency warning
/* istanbul ignore next */
if (!metalsmith.match) {
throw new Error(
'This version of @metalsmith/in-place requires metalsmith^2.4.1\'s newly added match method\nPlease update metalsmith"'
Expand Down Expand Up @@ -161,3 +175,6 @@ module.exports = (options) =>
.then(() => done())
.catch(/* istanbul ignore next */ (error) => done(error))
}
}

module.exports = initializeInPlace
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"author": "ismay",
"description": "A metalsmith plugin for in-place templating.",
"description": "A metalsmith plugin for in-place templating",
"license": "MIT",
"keywords": [
"in-place",
Expand Down

0 comments on commit ae06fb9

Please sign in to comment.