diff --git a/.gitignore b/.gitignore index b32672d..a6f192e 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ node_modules npm-debug.log samples tmp_samples +!test/fixtures/*.log diff --git a/README.md b/README.md index 1e70dec..2b341e4 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/doctoc.js b/doctoc.js index d5247ec..50930c3 100755 --- a/doctoc.js +++ b/doctoc.js @@ -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; @@ -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.error('--stdout cannot 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.error('--stdout cannot 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); diff --git a/test/fixtures/invalid_stdout/readme.md b/test/fixtures/invalid_stdout/readme.md new file mode 100644 index 0000000..2534d7f --- /dev/null +++ b/test/fixtures/invalid_stdout/readme.md @@ -0,0 +1,18 @@ +# Hello, world! + +README to test doctoc with user-specified titles. + + + +## Table of Contents + +- [Installation](#installation) +- [API](#api) +- [License](#license) + + + + +## Installation +## API +## License diff --git a/test/fixtures/stdout.md b/test/fixtures/stdout.log similarity index 100% rename from test/fixtures/stdout.md rename to test/fixtures/stdout.log diff --git a/test/fixtures/stdout_run_on_directory.log b/test/fixtures/stdout_run_on_directory.log new file mode 100644 index 0000000..c0681dd --- /dev/null +++ b/test/fixtures/stdout_run_on_directory.log @@ -0,0 +1,11 @@ +StdOut cannot 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. diff --git a/test/transform-stdout.js b/test/transform-stdout.js index 85b4455..74a7df2 100644 --- a/test/transform-stdout.js +++ b/test/transform-stdout.js @@ -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() @@ -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(); + } else { + t.fail('process did not produce an error: ' + error); + t.end(); + } + }) +})