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
This change is potentially breaking if users relied on suppressNoFilesError to invalidate invalid builds
  • Loading branch information
webketje committed Jul 2, 2023
1 parent 8c98954 commit 993f8ca
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 50 deletions.
19 changes: 0 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ You can pass options to `@metalsmith/in-place` with the [Javascript API](https:/

- [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 files. The default is `{}`.
- [suppressNoFilesError](#suppressnofileserror): optional. The no-files-to-process error will be suppressed. The default is `false`.
- [setFilename](#setfilename): 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`.

### `pattern`
Expand Down Expand Up @@ -82,24 +81,6 @@ Use this to pass options to the jstransformer that's rendering your templates. S

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

### `suppressNoFilesError`

`@metalsmith/in-place` 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. So this `metalsmith.json`:

```json
{
"source": "src",
"destination": "build",
"plugins": {
"@metalsmith/in-place": {
"suppressNoFilesError": true
}
}
}
```

Would suppress the error if there aren't any files to process. Note that when this option is turned on, if you're logging [debug messages](#errors-and-debugging), you’ll still see a message denoting when there aren't any files for metalsmith-layouts to process.

### `setFilename`

Set this option to `true` if you want to pass the current filename to each jstransformer. The default is `false`. So this `metalsmith.json`:
Expand Down
18 changes: 5 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ function validate({ filename, files }) {

// Files without an extension cannot be processed
if (!extensions.length) {
debug(`validation failed, ${filename} does not have an extension`)
debug.warn(`validation failed, ${filename} does not have an extension`)
return false
}

// Files that are not utf8 are ignored
if (!isUtf8(files[filename].contents)) {
debug(`validation failed, ${filename} is not utf-8`)
debug.warn(`validation failed, ${filename} is not utf-8`)
return false
}

Expand All @@ -105,7 +105,7 @@ function validate({ filename, files }) {
const transformer = getTransformer(extension)

if (!transformer) {
debug(`validation failed, no jstransformer found for last extension of ${filename}`)
debug.warn(`validation failed, no jstransformer found for last extension of ${filename}`)
}

return transformer
Expand All @@ -115,15 +115,13 @@ function validate({ filename, files }) {
* @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
}

Expand Down Expand Up @@ -155,14 +153,8 @@ function initializeInPlace(options = defaultOptions) {

// Let the user know when there are no files to process, usually caused by missing jstransformer
if (validFiles.length === 0) {
const message = 'no files to process. See https://www.npmjs.com/package/@metalsmith/in-place#no-files-to-process'

if (settings.suppressNoFilesError) {
debug(message)
return done()
}

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

// Map all files that should be processed to an array of promises and call done when finished
Expand Down
49 changes: 31 additions & 18 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-env node, mocha */

const { strictEqual } = require('assert')
const { strictEqual, deepStrictEqual } = require('assert')
const Metalsmith = require('metalsmith')
const equal = require('assert-dir-equal')
const { name } = require('../package.json')
Expand Down Expand Up @@ -82,24 +82,37 @@ describe('@metalsmith/in-place', () => {
})

it('should return an error when there are no valid files to process', (done) => {
Metalsmith(fixture('no-files'))
.use(plugin())
.build((err) => {
strictEqual(err instanceof Error, true)
strictEqual(
err.message,
'no files to process. See https://www.npmjs.com/package/@metalsmith/in-place#no-files-to-process'
)
done()
const ms = Metalsmith(fixture('no-files')),
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])
}
})
ms.env('DEBUG', '*:warn')
.use(() => {
ms.debug = () => Debugger
})
})

it('should suppress the no files error when flag is set', (done) => {
Metalsmith(fixture('no-files'))
.use(plugin({ suppressNoFilesError: true }))
.build((err) => {
strictEqual(err, null)
done()
.use(plugin())
.build(() => {
try {
deepStrictEqual(output.slice(output.length - 2), [
['warn', 'validation failed, index does not have an extension'],
['warn', 'No valid files to process.']
])
done()
} catch (err) {
done(err)
}
})
})

Expand Down

0 comments on commit 993f8ca

Please sign in to comment.