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

Commit

Permalink
feat(plugin-installation): generate installation section for private …
Browse files Browse the repository at this point in the history
…packages
  • Loading branch information
zkochan committed May 1, 2016
1 parent c8f05f9 commit 1fb4c0e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
1 change: 1 addition & 0 deletions plugins/mos-plugin-installation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ You'll get an installation section in your README that will instruct how to inst
The plugin will use information from the `package.json` in order to figure out what should the installation command look like.

- If there is a `preferGlobal: true` specified in the `package.json`, the generated command will be `npm install --global`
- If there is a `private: true`, the command instruction will suggest to clone the repo and install its dependencies
- Otherwise, the generated command will be `npm install --save`

## API
Expand Down
2 changes: 1 addition & 1 deletion plugins/mos-plugin-installation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ const renderInstallation = require('./lib/render-installation')

function plugin (markdown) {
return {
installation: () => renderInstallation(markdown.pkg),
installation: () => renderInstallation(markdown),
}
}
43 changes: 24 additions & 19 deletions plugins/mos-plugin-installation/lib/render-installation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,29 @@ const text = m.text
const paragraph = m.paragraph
const code = m.code

module.exports = pkg => {
if (pkg.private) {
throw new Error('Cannot generate installation section for a private module')
}
return [
heading({ depth: 2 }, [
text({
value: 'Installation',
}),
]),
paragraph([
text({
value: 'This module is installed via npm:',
}),
]),
code({
lang: 'sh',
value: `npm install ${pkg.name} ${pkg.preferGlobal ? '--global' : '--save'}`,
module.exports = md => [
heading({ depth: 2 }, [
text({
value: 'Installation',
}),
]),
paragraph([
text({
value: 'This module is installed via npm:',
}),
]
]),
code({
lang: 'sh',
value: createCommand(md),
}),
]

function createCommand (md) {
if (md.pkg.private) {
return [
`git clone ${md.repo.clone_url} && cd ./${md.repo.repo}`,
'npm install',
].join('\n')
}
return `npm install ${md.pkg.name} ${md.pkg.preferGlobal ? '--global' : '--save'}`
}
28 changes: 24 additions & 4 deletions plugins/mos-plugin-installation/lib/render-installation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('createInstallation', () => {
name: 'foo',
preferGlobal: 'MIT',
}
const installation = renderInstallation(pkg)
const installation = renderInstallation({pkg})
expect(installation).to.eql([
heading({ depth: 2 }, [
text({
Expand All @@ -39,7 +39,7 @@ describe('createInstallation', () => {
const pkg = {
name: 'foo',
}
const installation = renderInstallation(pkg)
const installation = renderInstallation({pkg})
expect(installation).to.eql([
heading({ depth: 2 }, [
text({
Expand All @@ -58,11 +58,31 @@ describe('createInstallation', () => {
])
})

it('should throw exception for private package', () => {
it('should create installation section for private package', () => {
const pkg = {
name: 'foo',
private: true,
}
expect(() => renderInstallation(pkg)).to.throw(Error, 'Cannot generate installation section for a private module')
const repo = {
repo: 'foo',
clone_url: 'https://github.com/zkochan/foo',
}
const installation = renderInstallation({pkg, repo})
expect(installation).to.eql([
heading({ depth: 2 }, [
text({
value: 'Installation',
}),
]),
paragraph([
text({
value: 'This module is installed via npm:',
}),
]),
code({
lang: 'sh',
value: 'git clone https://github.com/zkochan/foo && cd ./foo\nnpm install',
}),
])
})
})

0 comments on commit 1fb4c0e

Please sign in to comment.