Skip to content

Commit 2d84fbe

Browse files
committed
Adds --debug and --dry-run options to metalsmith (build) command
1 parent 24fcffb commit 2d84fbe

File tree

9 files changed

+47
-9
lines changed

9 files changed

+47
-9
lines changed

bin/metalsmith

+17-9
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ program
3333
.command('build', { isDefault: true })
3434
.description('Run a metalsmith build')
3535
.option('-c, --config <path>', 'configuration file location', 'metalsmith.json')
36+
.option('--debug', 'Set or override debug namespaces')
37+
.option('--dry-run', 'Process metalsmith files without outputting to the file system')
3638
.action(buildCommand)
3739

38-
function buildCommand({ config }) {
40+
program.parse(process.argv)
41+
42+
function buildCommand({ config, ...options }) {
3943
const dir = process.cwd()
4044
const path = isAbsolute(config) ? config : resolve(dir, config)
4145

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

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

106-
/**
107-
* Build.
108-
*/
109-
110-
metalsmith.build(function (err) {
111-
if (err) fatal(err.message, err.stack)
112-
log('success', `successfully built to ${metalsmith.destination()}`)
113-
})
111+
function onBuild(message) {
112+
return (err) => {
113+
if (err) fatal(err.message, err.stack)
114+
log('success', message)
115+
}
116+
}
114117

118+
if (options.dryRun) {
119+
metalsmith.process(onBuild(`successfully ran a dry-run of the ${config} build`))
120+
} else {
121+
metalsmith.build(onBuild(`successfully built to ${metalsmith.destination()}`))
122+
}
115123
}
116124

117125
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"env": {
3+
"debug": false
4+
},
5+
"plugins": {
6+
"@metalsmith/markdown": {}
7+
}
8+
}

test/fixtures/cli-debug/src/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body

test/fixtures/cli-dry-run/expected/.gitkeep

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

test/fixtures/cli-dry-run/src/.gitkeep

Whitespace-only changes.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body

test/index.js

+18
Original file line numberDiff line numberDiff line change
@@ -1325,5 +1325,23 @@ describe('CLI', function () {
13251325
done()
13261326
})
13271327
})
1328+
1329+
it('should not output a build when the --dry-run option is used', function (done) {
1330+
exec(`${bin} --dry-run`, { cwd: fixture('cli-dry-run') }, function (err) {
1331+
assert.strictEqual(err, null)
1332+
fs.stat(fixture('cli-dry-run/build'), (err) => {
1333+
assert.strictEqual(err.code, 'ENOENT')
1334+
done()
1335+
})
1336+
})
1337+
})
1338+
1339+
it('should override metalsmith.json DEBUG env var when --debug option is used', function (done) {
1340+
exec(`${bin} --debug @metalsmith/markdown`, { cwd: fixture('cli-debug') }, function (err, stdout, stderr) {
1341+
const match = stderr.split('\n')[1].slice(stderr.indexOf(' ') + 1)
1342+
assert.strictEqual(match, '@metalsmith/markdown converting file: index.md')
1343+
done()
1344+
})
1345+
})
13281346
})
13291347
})

0 commit comments

Comments
 (0)