diff --git a/package.json b/package.json index 45fffb1..34ed5aa 100644 --- a/package.json +++ b/package.json @@ -35,8 +35,9 @@ "chalk": "^1.1.3", "find-parent-dir": "^0.3.0", "keypress": "^0.2.1", - "markdown-it": "^8.3.1", + "markdown-it": "^8.4.2", "meow": "^3.7.0", + "node-dir": "^0.1.17", "update-notifier": "^2.1.0" }, "devDependencies": { diff --git a/src/get-docs.js b/src/get-docs.js index 05ccc51..9b30488 100644 --- a/src/get-docs.js +++ b/src/get-docs.js @@ -1,15 +1,26 @@ +const dir = require('node-dir') const getDocs = root => { - const path = require('path') - const fs = require('fs') + const mds = [] return new Promise((resolve, reject) => { - fs.readFile(path.join(root, 'README.md'), 'utf8', (err, data) => { - if(err && err.code == 'ENOENT') return reject(new Error('NO_DOCS_AVAILABLE')) + dir.readFiles(root, { + match: /.md$/, + excludeDir: ['node_modules'], + exclude: /^\./ + }, + function(err, content, next) { + if (err) throw err + mds.push(content) + next() + }, + function(err, files) { + if (files && files.length === 0) return reject(new Error('NO_DOCS_AVAILABLE')) if (err) return reject(err) - resolve(data) + + resolve(mds.join('\n')) }) }) } -module.exports = getDocs \ No newline at end of file +module.exports = getDocs diff --git a/src/get-docs.test.js b/src/get-docs.test.js index 080a3cb..df5f3b2 100644 --- a/src/get-docs.test.js +++ b/src/get-docs.test.js @@ -1,4 +1,5 @@ -const path = require('path') +const fs = require('fs') +const dir = require('node-dir') describe('get-docs', () => { let getDocs @@ -13,51 +14,46 @@ describe('get-docs', () => { expect(getDocs('') instanceof Promise).toBe(true) }) - it('should load README from the passed root directory', () => { - jest.mock('fs') - const { readFile } = require('fs') - - const root = '/foo/bar/baz' - require('./get-docs')(root) - expect(readFile).toBeCalled() - const readme = path.join(root, 'README.md') - expect(readFile.mock.calls[0][0]).toEqual(readme) - }) it('should reject with NO_DOCS_AVAILABLE error if README not found', () => { - jest.mock('fs') - const { readFile } = require('fs') - - - readFile.mockImplementationOnce((path, encoding, cb) => { - const err = new Error() - err.code = 'ENOENT' - cb(err, '') - }) - const root = '/foo/bar/baz' - return require('./get-docs')(root) + return require('./get-docs')(__dirname) .catch( err => { expect(err.message).toBe('NO_DOCS_AVAILABLE') }) }) - it('should pass the exception if a different fs error occurs', () => { - jest.mock('fs') - const { readFile } = require('fs') + describe('when md files are present', () => { + const mockedContent = 'Hello markdown' + const createMdFile = (name) => + fs.writeFileSync(`${__dirname}/${name}.md`, mockedContent) + afterAll(() => { + dir.files(__dirname, (err, files) => { + if (err) throw err - readFile.mockImplementationOnce((path, encoding, cb) => { - const err = new Error() - err.code = 'Oh my god, it\'s a boo boo' - cb(err, '') - }) - const root = '/foo/bar/baz' - return require('./get-docs')(root) - .catch( err => { - expect(err.message).not.toBe('NO_DOCS_AVAILABLE') - expect(err.code).toBe('Oh my god, it\'s a boo boo') + files.forEach((mdFile) => { + /.md$/.test(mdFile) && fs.unlinkSync(mdFile) + }) }) + }) + + it('should load README from the passed root directory', () => { + createMdFile('README') + + return require('./get-docs')(__dirname) + .then( content => { + expect(content).toBe(mockedContent) + }) + }) + + it('should load all md files from the passed root directory', () => { + createMdFile('some-more') + return require('./get-docs')(__dirname) + .then( content => { + expect(content.length).toEqual(mockedContent.length * 2 + 1) + }) + }) }) -}) \ No newline at end of file +}) diff --git a/src/md2json.js b/src/md2json.js index 4c4ec92..aa6b7aa 100644 --- a/src/md2json.js +++ b/src/md2json.js @@ -1,4 +1,5 @@ var md = require('markdown-it')() + .disable([ 'link', 'image' ]) const md2json = mdString => { if(!mdString) throw new Error('Markdown string expected') diff --git a/yarn.lock b/yarn.lock index a3ae973..e03727d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1819,15 +1819,16 @@ map-obj@^1.0.0, map-obj@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/map-obj/-/map-obj-1.0.1.tgz#d933ceb9205d82bdcf4886f6742bdc2b4dea146d" -markdown-it@^8.3.1: - version "8.3.1" - resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-8.3.1.tgz#2f4b622948ccdc193d66f3ca2d43125ac4ac7323" +markdown-it@^8.4.2: + version "8.4.2" + resolved "https://registry.yarnpkg.com/markdown-it/-/markdown-it-8.4.2.tgz#386f98998dc15a37722aa7722084f4020bdd9b54" + integrity sha512-GcRz3AWTqSUphY3vsUqQSFMbgR38a4Lh3GWlHRh/7MRwz8mcu9n2IO7HOh+bXHrR9kOPDl5RNCaEsrneb+xhHQ== dependencies: argparse "^1.0.7" entities "~1.1.1" linkify-it "^2.0.0" mdurl "^1.0.1" - uc.micro "^1.0.3" + uc.micro "^1.0.5" mdurl@^1.0.1: version "1.0.1" @@ -1912,6 +1913,12 @@ natural-compare@^1.4.0: version "1.4.0" resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" +node-dir@^0.1.17: + version "0.1.17" + resolved "https://registry.yarnpkg.com/node-dir/-/node-dir-0.1.17.tgz#5f5665d93351335caabef8f1c554516cf5f1e4e5" + dependencies: + minimatch "^3.0.2" + node-int64@^0.4.0: version "0.4.0" resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b" @@ -2621,10 +2628,15 @@ typedarray@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" -uc.micro@^1.0.1, uc.micro@^1.0.3: +uc.micro@^1.0.1: version "1.0.3" resolved "https://registry.yarnpkg.com/uc.micro/-/uc.micro-1.0.3.tgz#7ed50d5e0f9a9fb0a573379259f2a77458d50192" +uc.micro@^1.0.5: + version "1.0.6" + resolved "https://registry.yarnpkg.com/uc.micro/-/uc.micro-1.0.6.tgz#9c411a802a409a91fc6cf74081baba34b24499ac" + integrity sha512-8Y75pvTYkLJW2hWQHXxoqRgV7qb9B+9vFEtidML+7koHUFapnVJAZ6cKs+Qjz5Aw3aZWHMC6u0wJE3At+nSGwA== + uglify-js@^2.6: version "2.8.8" resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.8.8.tgz#1a5cd145eb528b606fa2b86e578885272b597cd7"