Skip to content

Commit

Permalink
Adds metalsmith.debug logs, removes setImmediate
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Dec 7, 2022
1 parent b5d43c7 commit b5a11b3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ yarn add @metalsmith/drafts

## Usage

Pass the plugin with any options to `Metalsmith.use`.
Pass the plugin with any options to `metalsmith.use`.

```js
import drafts from '@metalsmith/drafts'
Expand Down Expand Up @@ -65,6 +65,16 @@ metalsmith.use(
)
```

### Debug

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

```js
metalsmith.env('DEBUG', '@metalsmith/drafts*')
```

Alternatively you can set `DEBUG` to `@metalsmith/*` to debug all Metalsmith core plugins.

### CLI Usage

To use this plugin with the Metalsmith CLI, add `@metalsmith/drafts` to the `plugins` key in your `metalsmith.json` file:
Expand Down
29 changes: 24 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,32 @@ function drafts(options = defaultOptions) {
}

return function drafts(files, metalsmith, done) {
setImmediate(done)
if (options.include) return
Object.keys(files).forEach((path) => {
if (isDraft(files[path].draft, options.default)) {
delete files[path]
const debug = metalsmith.debug('@metalsmith/drafts')
debug('Running with options: %o', options)

if (options.include) {
return done()
}

const asDraft = Object.keys(files).filter((path) => isDraft(files[path].draft, options.default))

debug('Marked %s file(s) as draft', asDraft.length)

asDraft.forEach((path) => {
try {
const success = delete files[path]
// delete returns false in CJS non-strict mode
/* istanbul ignore if */
if (success === false) throw new Error()
debug.info('Removed draft "%s"', path)
// but throws in CJS strict-mode or ESM mode
} catch (err) {
/* istanbul ignore next */
debug.error('Failed to remove draft at "%s"', path)
}
})

done()
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const drafts = require('../lib/index.cjs')
describe('@metalsmith/drafts', function () {
it('should remove drafts from output (default behavior / ensure backwards-compatibility)', function (done) {
Metalsmith('test/fixtures/default')
.env('DEBUG', process.env.DEBUG)
.use(drafts())
.build(function (err) {
if (err) return done(err)
Expand All @@ -19,6 +20,7 @@ describe('@metalsmith/drafts', function () {

it('should allow a single (bool) param to include/omit drafts from output', function (done) {
Metalsmith('test/fixtures/bool-param')
.env('DEBUG', process.env.DEBUG)
.use(drafts(true))
.build(function (err) {
if (err) return done(err)
Expand All @@ -29,6 +31,7 @@ describe('@metalsmith/drafts', function () {

it('should accept numbers & strings as values to ease using env variables', function (done) {
Metalsmith('test/fixtures/input-types')
.env('DEBUG', process.env.DEBUG)
.use(drafts())
.build(function (err) {
if (err) return done(err)
Expand All @@ -39,6 +42,7 @@ describe('@metalsmith/drafts', function () {

it('should remove drafts from output (defaults=true)', function (done) {
Metalsmith('test/fixtures/defaults-to-true')
.env('DEBUG', process.env.DEBUG)
.use(drafts({ default: true }))
.build(function (err) {
if (err) return done(err)
Expand All @@ -49,6 +53,7 @@ describe('@metalsmith/drafts', function () {

it('should remove drafts from output (defaults=false)', function (done) {
Metalsmith('test/fixtures/defaults-to-false')
.env('DEBUG', process.env.DEBUG)
.use(drafts({ default: false }))
.build(function (err) {
if (err) return done(err)
Expand Down

0 comments on commit b5a11b3

Please sign in to comment.