Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ node_modules
npm-debug.log
samples
tmp_samples
!test/fixtures/*.log
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ This is useful CI environments where you want to check if your docs are up to da

You can print to stdout by using the `-s` or `--stdout` option.

This option is only applicable when specifying a single filename which doctoc is to run on. If you are specifying a folder or multiple files, the dry run option should be used instead.

### Only update existing ToC

Use `--update-only` or `-u` to only update the existing ToC. That is, the Markdown files without ToC will be left untouched. It is good if you want to use `doctoc` with `lint-staged`.
Expand Down
15 changes: 13 additions & 2 deletions doctoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ var title = argv.t || argv.title;
var notitle = argv.T || argv.notitle;
var entryPrefix = argv.entryprefix || '-';
var processAll = argv.all;
var stdOut = argv.s || argv.stdout
var stdOut = argv.s || argv.stdout || false;
var updateOnly = argv.u || argv['update-only']
var dryRun = argv.d || argv.dryrun || false;

Expand All @@ -125,10 +125,21 @@ else if (minHeaderLevel && minHeaderLevel > 2) { console.error('Min. heading lev

if (maxHeaderLevel && maxHeaderLevel < minHeaderLevel) { console.error('Max. heading level: ' + maxHeaderLevel + ' is less than the defined Min. heading level: ' + minHeaderLevel), printUsageAndExit(true); }

if (argv._.length > 1 && stdout) {
console.log('--stdout can not be used with multiple files/directories. Use --dryrun instead.');
process.exitCode = 2;
return;
}

for (var i = 0; i < argv._.length; i++) {
var target = cleanPath(argv._[i])
, stat = fs.statSync(target)
, stat = fs.statSync(target);

if (stat.isDirectory() && stdOut){
console.log('--stdout can not be used with multiple files/directories. Use --dryrun instead.');
process.exitCode = 2;
return;
}

if (stat.isDirectory()) {
console.log ('\nDocToccing "%s" and its sub directories for %s.', target, mode);
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/invalid_stdout/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Hello, world!

README to test doctoc with user-specified titles.

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
## Table of Contents

- [Installation](#installation)
- [API](#api)
- [License](#license)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->


## Installation
## API
## License
File renamed without changes.
11 changes: 11 additions & 0 deletions test/fixtures/stdout_run_on_directory.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
StdOut can not be used on a directory. Using dryrun instead.

DocToccing "test/fixtures/invalid_stdout" and its sub directories for github.com.

Found readme.md in "test/fixtures/invalid_stdout"

==================

"test/fixtures/invalid_stdout/readme.md" is up to date

Everything is OK.
17 changes: 15 additions & 2 deletions test/transform-stdout.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('\nshould print to stdout with --stdout option', function (t) {
return;
}
t.deepEqual(stdout
, fs.readFileSync(__dirname + '/fixtures/stdout.md', 'utf8')
, fs.readFileSync(__dirname + '/fixtures/stdout.log', 'utf8')
, 'spits out the correct table of contents')

t.end()
Expand All @@ -28,9 +28,22 @@ test('\nshould print to stdout with -s option', function (t) {
return;
}
t.deepEqual(stdout
, fs.readFileSync(__dirname + '/fixtures/stdout.md', 'utf8')
, fs.readFileSync(__dirname + '/fixtures/stdout.log', 'utf8')
, 'spits out the correct table of contents')

t.end()
})
})

test('\nshould exit with error code as --stdout option is not supported on a directory', function (t) {

exec('node doctoc.js test/fixtures/invalid_stdout --stdout', function (error, stdout, stderr) {
if (error) {
t.deepEqual(error.code, 2, 'process exited with error code 2 as expected');
t.end('process did have an error');
} else {
t.fail('process did not produce an error: ' + error);
t.end();
}
})
})
Loading