Skip to content

Commit

Permalink
BREAKING: remove suppressNoFilesError in favor of debug.warn message
Browse files Browse the repository at this point in the history
BREAKING: remove suppressNoFilesError in favor of debug.warn message
  • Loading branch information
webketje committed Feb 14, 2024
1 parent c1f7c4c commit 29c3b96
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
17 changes: 0 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ You can pass options to `@metalsmith/layouts` with the [Javascript API](https://
- [directory](#directory): optional. The directory for the layouts. The default is `layouts`.
- [pattern](#pattern): optional. Only files that match this pattern will be processed. Accepts a string or an array of strings. The default is `**`.
- [engineOptions](#engineoptions): optional. Use this to pass options to the jstransformer that's rendering your layouts. The default is `{}`.
- [suppressNoFilesError](#suppressnofileserror): optional. By default `@metalsmith/layouts` will exit with an error if there aren't any files to process. Enabling this option will suppress that error.

#### `default`

Expand Down Expand Up @@ -123,21 +122,6 @@ metalsmith.use(

Would pass `{ "cache": false }` to the used jstransformer.

#### `suppressNoFilesError`

`@metalsmith/layouts` exits with [an error](#no-files-to-process) if it can’t find any files to process. If you’re doing any kind of incremental builds via something like `metalsmith-watch`, this is problematic as you’re likely only rebuilding files that have changed. This flag allows you to suppress that error.

Note that when this option is turned on, if you're logging [debug](#debug) messages, you’ll still see a message denoting when there aren't any files for `@metalsmith/layouts` to process.

There are several things that might cause you to get a `no files to process` error:

- Your [pattern](#pattern) does not match any files
- None of your files pass validation, validation fails for files that:
- Have no layout
- Have a layout without an extension
- Are not utf-8
- Have a layout that needs a jstransformer that hasn't been installed

### Debug

To enable debug logs, set the `DEBUG` environment variable to `@metalsmith/layouts`:
Expand All @@ -159,7 +143,6 @@ To use this plugin with the Metalsmith CLI, add `@metalsmith/layouts` to the `pl
"@metalsmith/layouts": {
"default": null,
"directory": "layouts",
"suppressNoFilesError": false,
"engineOptions": {}
}
}
Expand Down
4 changes: 0 additions & 4 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ export type Options = {
* Pass options to [the jstransformer](https://github.com/jstransformers/jstransformer) that's rendering your layouts. The default is `{}`.
*/
engineOptions?: any;
/**
* By default `@metalsmith/layouts` will exit with an error if there aren't any files to process. Enabling this option will suppress that error.
*/
suppressNoFilesError?: boolean;
};
/**
* A metalsmith plugin for rendering layouts
Expand Down
16 changes: 4 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ let debug = () => {
* @property {string} [pattern] The directory for the layouts. The default is `layouts`.
* @property {string|string[]} [directory] Only files that match this pattern will be processed. Accepts a string or an array of strings. The default is `**` (all).
* @property {Object} [engineOptions] Pass options to [the jstransformer](https://github.com/jstransformers/jstransformer) that's rendering your layouts. The default is `{}`.
* @property {boolean} [suppressNoFilesError] By default `@metalsmith/layouts` will exit with an error if there aren't any files to process. Enabling this option will suppress that error.
*/

/**
Expand Down Expand Up @@ -86,7 +85,6 @@ function normalizeOptions(options, transform) {
pattern: '**',
directory: 'layouts',
engineOptions: {},
suppressNoFilesError: false,
...options,
transform
}
Expand Down Expand Up @@ -210,17 +208,11 @@ function layouts(options) {
// Filter files by validity, pass basename to avoid dots in folder path
const validFiles = matchedFiles.filter((filename) => validate({ filename, files, options }))

// Let the user know when there are no files to process, unless the check is suppressed
// Let the user know when there are no files to process
if (validFiles.length === 0) {
const message =
'no files to process. See https://www.npmjs.com/package/@metalsmith/layouts#suppressnofileserror'

if (options.suppressNoFilesError) {
debug.error(message)
return done()
}

return done(new Error(message))
debug.warn('No valid files to process.')
done()
return
}

// Map all files that should be processed to an array of promises and call done when finished
Expand Down
42 changes: 34 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Metalsmith from 'metalsmith'
import equal from 'assert-dir-equal'
import path from 'path'
import { rejects, strictEqual } from 'assert'
import { rejects, strictEqual, ok } from 'assert'
import plugin from '../src/index.js'

function fixture(dir) {
Expand All @@ -15,6 +15,28 @@ function fixture(dir) {
}
}

function patchDebug() {
const output = []
const Debugger = (...args) => {
output.push(['log', ...args])
}
Object.assign(Debugger, {
info: (...args) => {
output.push(['info', ...args])
},
warn: (...args) => {
output.push(['warn', ...args])
},
error: (...args) => {
output.push(['error', ...args])
}
})
return function patchDebug(files, ms) {
ms.debug = () => Debugger
ms.metadata({ logs: output })
}
}

describe('@metalsmith/layouts', () => {
it('should apply a single layout to a single file', async () => {
const { dir, actual, expected } = fixture('single-file')
Expand Down Expand Up @@ -148,14 +170,18 @@ describe('@metalsmith/layouts', () => {
equal(actual, expected)
})

it('should return an error when there are no valid files to process', async () => {
it('should log a warning when there are no valid files to process', async () => {
const { dir } = fixture('no-files')
rejects(
async () => {
await Metalsmith(dir).env('DEBUG', process.env.DEBUG).use(plugin()).build()
},
{ message: 'no files to process.' }
)
const ms = Metalsmith(dir)
await ms
.env('DEBUG', process.env.DEBUG)
.use(patchDebug())
.use(plugin({ transform: 'handlebars' }))
.build()
const log = ms
.metadata()
.logs.find(([type, msg]) => type === 'warn' && msg === 'No valid files to process.')
ok(log)
})

it('should return an error for an invalid pattern', async () => {
Expand Down

0 comments on commit 29c3b96

Please sign in to comment.