Skip to content

Commit

Permalink
Adds --debug and --dry-run options to metalsmith (build) command
Browse files Browse the repository at this point in the history
  • Loading branch information
webketje committed Jan 10, 2023
1 parent 24fcffb commit 2d84fbe
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 9 deletions.
26 changes: 17 additions & 9 deletions bin/metalsmith
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ program
.command('build', { isDefault: true })
.description('Run a metalsmith build')
.option('-c, --config <path>', 'configuration file location', 'metalsmith.json')
.option('--debug', 'Set or override debug namespaces')
.option('--dry-run', 'Process metalsmith files without outputting to the file system')
.action(buildCommand)

function buildCommand({ config }) {
program.parse(process.argv)

function buildCommand({ config, ...options }) {
const dir = process.cwd()
const path = isAbsolute(config) ? config : resolve(dir, config)

Expand Down Expand Up @@ -67,6 +71,7 @@ function buildCommand({ config }) {
if (json.frontmatter != null) metalsmith.frontmatter(json.frontmatter)
if (json.ignore != null) metalsmith.ignore(json.ignore)
if (isObject(json.env)) metalsmith.env(expandEnvVars(json.env, process.env))
if (options.debug) metalsmith.env('DEBUG', options.debug)

// set a flag plugins can check to target CLI-specific behavior
metalsmith.env('CLI', true)
Expand Down Expand Up @@ -103,15 +108,18 @@ function buildCommand({ config }) {
}
})

/**
* Build.
*/

metalsmith.build(function (err) {
if (err) fatal(err.message, err.stack)
log('success', `successfully built to ${metalsmith.destination()}`)
})
function onBuild(message) {
return (err) => {
if (err) fatal(err.message, err.stack)
log('success', message)
}
}

if (options.dryRun) {
metalsmith.process(onBuild(`successfully ran a dry-run of the ${config} build`))
} else {
metalsmith.build(onBuild(`successfully built to ${metalsmith.destination()}`))
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/cli-debug/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body
8 changes: 8 additions & 0 deletions test/fixtures/cli-debug/metalsmith.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"env": {
"debug": false
},
"plugins": {
"@metalsmith/markdown": {}
}
}
1 change: 1 addition & 0 deletions test/fixtures/cli-debug/src/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/cli-dry-run/metalsmith.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/cli-dry-run/src/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body
18 changes: 18 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1325,5 +1325,23 @@ describe('CLI', function () {
done()
})
})

it('should not output a build when the --dry-run option is used', function (done) {
exec(`${bin} --dry-run`, { cwd: fixture('cli-dry-run') }, function (err) {
assert.strictEqual(err, null)
fs.stat(fixture('cli-dry-run/build'), (err) => {
assert.strictEqual(err.code, 'ENOENT')
done()
})
})
})

it('should override metalsmith.json DEBUG env var when --debug option is used', function (done) {
exec(`${bin} --debug @metalsmith/markdown`, { cwd: fixture('cli-debug') }, function (err, stdout, stderr) {
const match = stderr.split('\n')[1].slice(stderr.indexOf(' ') + 1)
assert.strictEqual(match, '@metalsmith/markdown converting file: index.md')
done()
})
})
})
})

0 comments on commit 2d84fbe

Please sign in to comment.