Skip to content

Commit bdd1f53

Browse files
author
Anand Thakker
committed
Exit nonzero from readme cmd if section is absent
1 parent 3ea2944 commit bdd1f53

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

lib/commands/readme.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ function readme(documentation, parsedArgs) {
5151
throw err;
5252
}
5353
var readmeContent = fs.readFileSync(readmeFile, 'utf8');
54-
mdast.use(plugin, { toInject: docsAst }).process(readmeContent, onInjected.bind(null, readmeContent));
54+
mdast.use(plugin, {
55+
section: readmeOptions.section,
56+
toInject: docsAst
57+
}).process(readmeContent, onInjected.bind(null, readmeContent));
5558
}
5659

5760
function onInjected(readmeContent, err, file, content) {
@@ -81,7 +84,9 @@ function readme(documentation, parsedArgs) {
8184
// wrap the inject utility as an mdast plugin
8285
function plugin(mdast, options) {
8386
return function transform(targetAst, file, next) {
84-
inject('API', targetAst, options.toInject);
87+
if (!inject(options.section, targetAst, options.toInject)) {
88+
return next(new Error('Heading ' + options.section + ' not found.'));
89+
}
8590
next();
8691
};
8792
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"jsdoc-inline-lex": "^1.0.1",
2929
"mdast": "^2.0.0",
3030
"mdast-toc": "^1.1.0",
31-
"mdast-util-inject": "^1.0.0",
31+
"mdast-util-inject": "^1.1.0",
3232
"micromatch": "^2.1.6",
3333
"mime": "^1.3.4",
3434
"module-deps": "^4.0.2",

test/bin-readme.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ test('readme command', function (group) {
8787
});
8888
});
8989

90+
group.test('errors if specified readme section is missing', function (t) {
91+
documentation(['readme index.js -s DUMMY'], {cwd: d}, function (err, stdout, stderr) {
92+
t.ok(err);
93+
t.ok(err.code !== 0, 'exit nonzero');
94+
t.end();
95+
});
96+
});
97+
9098
group.end();
9199
});
92100
});

0 commit comments

Comments
 (0)