Skip to content
This repository has been archived by the owner on Nov 7, 2019. It is now read-only.

Commit

Permalink
refactor: return an AST instead of a string
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed May 15, 2016
1 parent 3393a2d commit 9aa87e3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -49,6 +49,7 @@ You'll get a license section with the license and author specified in the `packa
## <a name="dependencies">Dependencies</a> [![Dependency status for master](https://img.shields.io/david/zkochan/mos-plugin-license/master.svg?style=flat-square)](https://david-dm.org/zkochan/mos-plugin-license/master)

- [file-exists](https://github.com/scottcorgan/file-exists): Check if filepath exists and is a file
- [markdownscript](https://github.com/zkochan/markdownscript): Creates markdown Abstract Syntax Tree

<!--/@-->

Expand Down
24 changes: 15 additions & 9 deletions lib/render-license.js
@@ -1,19 +1,25 @@
'use strict'
const fileExists = require('file-exists')
const path = require('path')
const m = require('markdownscript')
const h2 = m.h2
const link = m.link
const paragraph = m.paragraph

module.exports = markdown => {
const licensePath = path.resolve(path.dirname(markdown.filePath), 'LICENSE')
const licenseFileExists = fileExists(licensePath)

return [
'## License',
'',
(licenseFileExists
? `[${markdown.pkg.license}](./LICENSE)`
: markdown.pkg.license) + ' © ' +
(markdown.pkg.author.url
? `[${markdown.pkg.author.name}](${markdown.pkg.author.url})`
: markdown.pkg.author.name),
].join('\n')
h2(['License']),
paragraph([
(licenseFileExists
? link({url: './LICENSE'}, [markdown.pkg.license])
: markdown.pkg.license),
' © ',
(markdown.pkg.author.url
? link({url: markdown.pkg.author.url}, [markdown.pkg.author.name])
: markdown.pkg.author.name),
]),
]
}
29 changes: 25 additions & 4 deletions lib/render-license.spec.js
Expand Up @@ -3,6 +3,11 @@ const describe = require('mocha').describe
const it = require('mocha').it
const expect = require('chai').expect
const path = require('path')
const m = require('markdownscript')
const h2 = m.h2
const p = m.paragraph
const link = m.link

const renderLicense = require('./render-license')

describe('renderLicense', () => {
Expand All @@ -15,8 +20,14 @@ describe('renderLicense', () => {
license: 'MIT',
}
const license = renderLicense({pkg})
expect(license).to
.eq('## License\n\n[MIT](./LICENSE) © [Zoltan Kochan](http://kochan.io)')
expect(license).to.eql([
h2(['License']),
p([
link({url: './LICENSE'}, ['MIT']),
' © ',
link({url: 'http://kochan.io'}, ['Zoltan Kochan']),
]),
])
})

it("should create license with author's name", () => {
Expand All @@ -27,7 +38,13 @@ describe('renderLicense', () => {
license: 'MIT',
}
const license = renderLicense({pkg})
expect(license).to.eq('## License\n\n[MIT](./LICENSE) © Zoltan Kochan')
expect(license).to.eql([
h2(['License']),
p([
link({url: './LICENSE'}, ['MIT']),
' © Zoltan Kochan',
]),
])
})

it("should create license with no link to license file when license file doesn't exist", () => {
Expand All @@ -41,6 +58,10 @@ describe('renderLicense', () => {
pkg,
filePath: path.resolve(__filename),
})
expect(license).to.eq('## License\n\nMIT © Zoltan Kochan')
expect(license).to.eql([
h2(['License']),
p(['MIT © Zoltan Kochan',
]),
])
})
})
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -38,7 +38,8 @@
},
"homepage": "https://github.com/zkochan/mos-plugin-license#readme",
"dependencies": {
"file-exists": "^1.0.0"
"file-exists": "^1.0.0",
"markdownscript": "^1.2.0"
},
"devDependencies": {
"chai": "^3.4.1",
Expand Down

0 comments on commit 9aa87e3

Please sign in to comment.