Skip to content

Commit

Permalink
Swaps out debug for metalsmith.debug & enhances debug log output
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Apr 3, 2023
1 parent bb39c56 commit f8cd111
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Expand Up @@ -3,7 +3,7 @@ env:
es6: true
extends:
- 'eslint:recommended'
- 'plugin:node/recommended'
- 'plugin:n/recommended'
- 'plugin:import/recommended'
- 'prettier'
parserOptions:
Expand Down
12 changes: 2 additions & 10 deletions README.md
Expand Up @@ -142,16 +142,8 @@ There are several things that might cause you to get a `no files to process` err

To enable debug logs, set the `DEBUG` environment variable to `@metalsmith/layouts`:

Linux/Mac:

```bash
DEBUG=@metalsmith/layouts
```

Windows:

```batch
set "DEBUG=@metalsmith/layouts"
```js
metalsmith.env('DEBUG', '@metalsmith/layouts*')
```

Alternatively you can set `DEBUG` to `@metalsmith/*` to debug all Metalsmith core plugins.
Expand Down
9 changes: 6 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Expand Up @@ -45,7 +45,6 @@
"test": "nyc mocha"
},
"dependencies": {
"debug": "^4.3.4",
"inputformat-to-jstransformer": "^1.4.0",
"is-utf8": "^0.2.1",
"jstransformer": "^1.0.0"
Expand Down
32 changes: 21 additions & 11 deletions src/index.js
@@ -1,9 +1,12 @@
import createDebug from 'debug'
import path from 'path'
import isUtf8 from 'is-utf8'
/* eslint-disable-next-line n/no-missing-import */
import getTransformer from './get-transformer'

const debug = createDebug('@metalsmith/layouts')
/* istanbul ignore next */
let debug = () => {
throw new Error('uninstantiated debug')
}

/**
* @typedef {Object} Options `@metalsmith/layouts` options
Expand Down Expand Up @@ -38,7 +41,7 @@ function render({ filename, files, metadata, settings, metalsmith }) {
const layout = getLayout({ file, settings })
const extension = layout.split('.').pop()

debug(`rendering ${filename} with layout ${layout}`)
debug.info('Rendering "%s" with layout "%s"', filename, layout)

// Stringify file contents
const contents = file.contents.toString()
Expand All @@ -53,7 +56,7 @@ function render({ filename, files, metadata, settings, metalsmith }) {
.then((rendered) => {
// Update file with results
file.contents = Buffer.from(rendered.body)
debug(`done rendering ${filename}`)
debug.info('Done rendering "%s"', filename)
})
.catch((err) => {
// Prepend error message with file path
Expand All @@ -70,23 +73,23 @@ function validate({ filename, files, settings }) {
const file = files[filename]
const layout = getLayout({ file, settings })

debug(`validating ${filename}`)
debug.info(`Validating ${filename}`)

// Files without a layout cannot be processed
if (!layout) {
debug(`validation failed, ${filename} does not have a layout set`)
debug.warn('Validation failed, "%s" does not have a layout set', filename)
return false
}

// Layouts without an extension cannot be processed
if (!layout.includes('.')) {
debug(`validation failed, layout for ${filename} does not have an extension`)
debug.warn('Validation failed, layout for "%s" does not have an extension', filename)
return false
}

// Files that are not utf8 are ignored
if (!isUtf8(file.contents)) {
debug(`validation failed, ${filename} is not utf-8`)
debug.warn('Validation failed, "%s" is not utf-8', filename)
return false
}

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

if (!transformer) {
debug(`validation failed, no jstransformer found for layout for ${filename}`)
debug.warn('Validation failed, no jstransformer found for layout for "%s"', filename)
}

return transformer
Expand All @@ -108,6 +111,8 @@ function validate({ filename, files, settings }) {
*/
function initLayouts(options) {
return function layouts(files, metalsmith, done) {
debug = metalsmith.debug('@metalsmith/layouts')

const metadata = metalsmith.metadata()
const defaults = {
pattern: '**',
Expand All @@ -117,6 +122,8 @@ function initLayouts(options) {
}
const settings = { ...defaults, ...options }

debug('Running with options: %o', settings)

// Check whether the pattern option is valid
if (!(typeof settings.pattern === 'string' || Array.isArray(settings.pattern))) {
return done(
Expand All @@ -138,7 +145,7 @@ function initLayouts(options) {
'no files to process. See https://www.npmjs.com/package/@metalsmith/layouts#suppressnofileserror'

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

Expand All @@ -149,7 +156,10 @@ function initLayouts(options) {
return Promise.all(
validFiles.map((filename) => render({ filename, files, metadata, settings, metalsmith }))
)
.then(() => done())
.then(() => {
debug('Finished rendering %s file%s', validFiles.length, validFiles.length > 1 ? 's' : '')
done()
})
.catch(/* istanbul ignore next */ (error) => done(error))
}
}
Expand Down
22 changes: 21 additions & 1 deletion test/index.cjs
@@ -1,7 +1,7 @@
const Metalsmith = require('metalsmith')
const equal = require('assert-dir-equal')
const path = require('path')
/* eslint-disable-next-line node/no-missing-require */
/* eslint-disable-next-line n/no-missing-require */
const plugin = require('..')
const { it, describe } = require('mocha')
const { doesNotThrow, strictEqual } = require('assert')
Expand All @@ -10,6 +10,7 @@ const fixture = path.resolve.bind(process.cwd(), 'test/fixtures')
describe('@metalsmith/layouts', () => {
it('should apply a single layout to a single file', (done) => {
Metalsmith(fixture('single-file'))
.env('DEBUG', process.env.DEBUG)
.use(plugin())
.build((err) => {
if (err) done(err)
Expand All @@ -20,6 +21,7 @@ describe('@metalsmith/layouts', () => {

it('should apply a single layout to a single file with an async jstransformer', (done) => {
Metalsmith(fixture('single-file-async'))
.env('DEBUG', process.env.DEBUG)
.use(plugin())
.build((err) => {
if (err) done(err)
Expand All @@ -32,6 +34,7 @@ describe('@metalsmith/layouts', () => {

it('should apply a single layout to multiple files', (done) => {
Metalsmith(fixture('multiple-files'))
.env('DEBUG', process.env.DEBUG)
.use(plugin())
.build((err) => {
if (err) done(err)
Expand All @@ -44,6 +47,7 @@ describe('@metalsmith/layouts', () => {

it('should apply multiple layouts to multiple files', (done) => {
Metalsmith(fixture('multiple-files-and-layouts'))
.env('DEBUG', process.env.DEBUG)
.use(plugin())
.build((err) => {
if (err) done(err)
Expand All @@ -59,6 +63,7 @@ describe('@metalsmith/layouts', () => {

it('should apply a default layout', (done) => {
Metalsmith(fixture('default-layout'))
.env('DEBUG', process.env.DEBUG)
.use(plugin({ default: 'standard.hbs' }))
.build((err) => {
if (err) done(err)
Expand All @@ -71,6 +76,7 @@ describe('@metalsmith/layouts', () => {

it('should override a default layout', (done) => {
Metalsmith(fixture('override-default-layout'))
.env('DEBUG', process.env.DEBUG)
.use(plugin({ default: 'standard.hbs' }))
.build((err) => {
if (err) done(err)
Expand All @@ -86,6 +92,7 @@ describe('@metalsmith/layouts', () => {

it('should apply a string pattern', (done) => {
Metalsmith(fixture('string-pattern'))
.env('DEBUG', process.env.DEBUG)
.use(plugin({ pattern: 'match.html' }))
.build((err) => {
if (err) done(err)
Expand All @@ -98,6 +105,7 @@ describe('@metalsmith/layouts', () => {

it('should apply an array of string patterns', (done) => {
Metalsmith(fixture('array-pattern'))
.env('DEBUG', process.env.DEBUG)
.use(plugin({ pattern: ['match.html', 'also.html'] }))
.build((err) => {
if (err) done(err)
Expand All @@ -108,6 +116,7 @@ describe('@metalsmith/layouts', () => {

it('should ignore binary files', (done) => {
Metalsmith(fixture('ignore-binary'))
.env('DEBUG', process.env.DEBUG)
.use(plugin({ default: 'standard.hbs' }))
.build((err) => {
if (err) done(err)
Expand All @@ -118,6 +127,7 @@ describe('@metalsmith/layouts', () => {

it('should accept an alternate directory for layouts', (done) => {
Metalsmith(fixture('directory'))
.env('DEBUG', process.env.DEBUG)
.use(plugin({ directory: 'templates' }))
.build((err) => {
if (err) done(err)
Expand All @@ -128,6 +138,7 @@ describe('@metalsmith/layouts', () => {

it('should process variables from the frontmatter', (done) => {
Metalsmith(fixture('variables'))
.env('DEBUG', process.env.DEBUG)
.use(plugin())
.build((err) => {
if (err) done(err)
Expand All @@ -138,6 +149,7 @@ describe('@metalsmith/layouts', () => {

it('should process variables from the metadata', (done) => {
Metalsmith(fixture('metadata'))
.env('DEBUG', process.env.DEBUG)
.metadata({ text: 'Some text' })
.use(plugin())
.build((err) => {
Expand All @@ -149,6 +161,7 @@ describe('@metalsmith/layouts', () => {

it('should override variables from the metadata with local ones', (done) => {
Metalsmith(fixture('override-metadata'))
.env('DEBUG', process.env.DEBUG)
.metadata({ text: 'Some text' })
.use(plugin())
.build((err) => {
Expand All @@ -162,6 +175,7 @@ describe('@metalsmith/layouts', () => {

it('should return an error when there are no valid files to process', (done) => {
Metalsmith(fixture('no-files'))
.env('DEBUG', process.env.DEBUG)
.use(plugin())
.build((err) => {
strictEqual(err instanceof Error, true)
Expand All @@ -175,6 +189,7 @@ describe('@metalsmith/layouts', () => {

it('should not return an error when there are no valid files to process and suppressNoFilesError is true', (done) => {
Metalsmith(fixture('no-files'))
.env('DEBUG', process.env.DEBUG)
.use(plugin({ suppressNoFilesError: true }))
.build((err) => {
strictEqual(err, null)
Expand All @@ -184,6 +199,7 @@ describe('@metalsmith/layouts', () => {

it('should return an error for an invalid pattern', (done) => {
Metalsmith(fixture('invalid-pattern'))
.env('DEBUG', process.env.DEBUG)
.use(plugin({ pattern: () => {} }))
.build((err) => {
strictEqual(err instanceof Error, true)
Expand All @@ -197,6 +213,7 @@ describe('@metalsmith/layouts', () => {

it('should ignore layouts without an extension', (done) => {
Metalsmith(fixture('ignore-invalid-layout'))
.env('DEBUG', process.env.DEBUG)
.use(plugin())
.build((err) => {
if (err) done(err)
Expand All @@ -209,6 +226,7 @@ describe('@metalsmith/layouts', () => {

it('should ignore layouts without a jstransformer', (done) => {
Metalsmith(fixture('ignore-layout-without-jstransformer'))
.env('DEBUG', process.env.DEBUG)
.use(plugin())
.build((err) => {
if (err) done(err)
Expand All @@ -224,6 +242,7 @@ describe('@metalsmith/layouts', () => {

it('should allow default layouts to be disabled from the frontmatter', (done) => {
Metalsmith(fixture('override-default'))
.env('DEBUG', process.env.DEBUG)
.use(plugin({ default: 'standard.hbs' }))
.build((err) => {
if (err) done(err)
Expand All @@ -236,6 +255,7 @@ describe('@metalsmith/layouts', () => {

it('should prefix rendering errors with the filename', (done) => {
Metalsmith(fixture('rendering-error'))
.env('DEBUG', process.env.DEBUG)
.use(plugin())
.build((err) => {
strictEqual(err instanceof Error, true)
Expand Down

0 comments on commit f8cd111

Please sign in to comment.