Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
23 changes: 17 additions & 6 deletions src/get-docs.js
Original file line number Diff line number Diff line change
@@ -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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we swap this for reject(err)? Using a throw statement here might break promise chaining/catching.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, can we cover this bit with a unit test case, i.e. mocking readFiles to throw an exception here?

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
module.exports = getDocs
68 changes: 32 additions & 36 deletions src/get-docs.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path')
const fs = require('fs')
const dir = require('node-dir')

describe('get-docs', () => {
let getDocs
Expand All @@ -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)
})
})
})
})
})
1 change: 1 addition & 0 deletions src/md2json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var md = require('markdown-it')()
.disable([ 'link', 'image' ])

const md2json = mdString => {
if(!mdString) throw new Error('Markdown string expected')
Expand Down
22 changes: 17 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down