diff --git a/README.md b/README.md index e5ea958..0c379d0 100644 --- a/README.md +++ b/README.md @@ -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` @@ -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`: @@ -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": {} } } diff --git a/lib/index.d.ts b/lib/index.d.ts index 26f5d43..87af5dc 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -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 diff --git a/src/index.js b/src/index.js index ce342e3..8d29e40 100644 --- a/src/index.js +++ b/src/index.js @@ -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. */ /** @@ -86,7 +85,6 @@ function normalizeOptions(options, transform) { pattern: '**', directory: 'layouts', engineOptions: {}, - suppressNoFilesError: false, ...options, transform } @@ -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 diff --git a/test/index.js b/test/index.js index 12e3458..9dc639f 100644 --- a/test/index.js +++ b/test/index.js @@ -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) { @@ -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') @@ -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 () => {