diff --git a/README.md b/README.md index e5c1855..084e565 100644 --- a/README.md +++ b/README.md @@ -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' @@ -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: diff --git a/src/index.js b/src/index.js index 3db1115..e9bd83c 100644 --- a/src/index.js +++ b/src/index.js @@ -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() } } diff --git a/test/index.cjs b/test/index.cjs index 91894ed..5cd2de7 100644 --- a/test/index.cjs +++ b/test/index.cjs @@ -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) @@ -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) @@ -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) @@ -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) @@ -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)