From 4fd776b16a9fd644108d4a6aa892e19dfbc7eae2 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Mon, 21 May 2018 20:39:00 -0700 Subject: [PATCH] feat: Auto-detect readme filename BREAKING CHANGE: the --readme-file option now has a smart default value --- __tests__/__snapshots__/bin-readme.js.snap | 34 ++++++++++++++++++++++ __tests__/bin-readme.js | 23 +++++++++++++++ src/get-readme-file.js | 5 +--- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/__tests__/__snapshots__/bin-readme.js.snap b/__tests__/__snapshots__/bin-readme.js.snap index 2398500..535e3a0 100644 --- a/__tests__/__snapshots__/bin-readme.js.snap +++ b/__tests__/__snapshots__/bin-readme.js.snap @@ -1,5 +1,39 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`readme autodetection of different filenames updates readme.markdown 1`] = ` +"# A title + +# API + + + +### Table of Contents + +- [foo](#foo) +- [bar](#bar) + +## foo + +A function with documentation. + +**Parameters** + +- \`a\` {string} blah + +Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** answer + +## bar + +A second function with docs + +**Parameters** + +- \`b\` + +# Another section +" +`; + exports[`readme command --readme-file 1`] = ` "# A title diff --git a/__tests__/bin-readme.js b/__tests__/bin-readme.js index 19c26cd..51f6441 100644 --- a/__tests__/bin-readme.js +++ b/__tests__/bin-readme.js @@ -22,6 +22,29 @@ function documentation(args, options, parseJSON) { }); } +describe('readme autodetection of different filenames', function() { + const fixtures = path.join(__dirname, 'fixture/readme'); + const sourceFile = path.join(fixtures, 'index.js'); + let d; + let removeCallback; + + beforeEach(() => { + const dirEntry = tmp.dirSync({ unsafeCleanup: true }); + d = dirEntry.name; + fs.copySync( + path.join(fixtures, 'README.input.md'), + path.join(d, 'readme.markdown') + ); + fs.copySync(path.join(fixtures, 'index.js'), path.join(d, 'index.js')); + }); + + test('updates readme.markdown', async function() { + await documentation(['readme index.js -s API'], { cwd: d }); + const outputPath = path.join(d, 'readme.markdown'); + expect(fs.readFileSync(outputPath, 'utf-8')).toMatchSnapshot(); + }); +}); + describe('readme command', function() { const fixtures = path.join(__dirname, 'fixture/readme'); const sourceFile = path.join(fixtures, 'index.js'); diff --git a/src/get-readme-file.js b/src/get-readme-file.js index d2382d5..a4b497a 100644 --- a/src/get-readme-file.js +++ b/src/get-readme-file.js @@ -5,14 +5,11 @@ const path = require('path'); module.exports = function findReadme(dir: string) { const readmeFilenames = [ - 'README', 'README.markdown', 'README.md', - 'README.txt', 'Readme.md', 'readme.markdown', - 'readme.md', - 'readme.txt' + 'readme.md' ]; const readmeFile = fs.readdirSync(dir).find(function(filename) {