From 1225a739cea848f9c58ccda3de5b517258042d8d Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Sun, 24 Apr 2016 20:21:44 +0300 Subject: [PATCH] feat: create package --- .eslintrc | 7 +++ .gitattributes | 1 + .gitignore | 33 +++++++++++ .travis.yml | 17 ++++++ LICENSE | 21 +++++++ README.md | 80 ++++++++++++++++++++++++++ index.js | 17 ++++++ index.spec.js | 13 +++++ lib/create-shields-renderer.js | 42 ++++++++++++++ lib/create-shields-renderer.spec.js | 89 +++++++++++++++++++++++++++++ package.json | 70 +++++++++++++++++++++++ test/mocha.opts | 1 + 12 files changed, 391 insertions(+) create mode 100644 .eslintrc create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 index.js create mode 100644 index.spec.js create mode 100644 lib/create-shields-renderer.js create mode 100644 lib/create-shields-renderer.spec.js create mode 100644 package.json create mode 100644 test/mocha.opts diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..b9fc9f1 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,7 @@ +{ + "extends": "standard", + "rules": { + "comma-dangle": [2, "always-multiline"], + "arrow-parens": [2, "as-needed"] + } +} diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..fcadb2c --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text eol=lf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e920c16 --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +# Logs +logs +*.log +npm-debug.log* + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +node_modules + +# Optional npm cache directory +.npm + +# Optional REPL history +.node_repl_history diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..736df98 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,17 @@ +language: node_js +sudo: false +node_js: + - v4 + +cache: + directories: + - node_modules + +before_install: + - npm install -g npm@3 +install: + - npm install + +after_success: + - npm run coveralls + - npm run semantic-release diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8e51fe6 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2016 Zoltan Kochan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..64f30f7 --- /dev/null +++ b/README.md @@ -0,0 +1,80 @@ + +# mos-plugin-shields + + + +> A mos plugin for creating markdown shields + + + +[![NPM version](https://img.shields.io/npm/v/mos-plugin-shields.svg?style=flat-square)](https://www.npmjs.com/package/mos-plugin-shields) +[![Build status for master](https://img.shields.io/travis/zkochan/mos-plugin-shields/master.svg?style=flat-square)](https://travis-ci.org/zkochan/mos-plugin-shields) +[![Test coverage for master](https://img.shields.io/coveralls/zkochan/mos-plugin-shields/master.svg?style=flat-square)](https://coveralls.io/r/zkochan/mos-plugin-shields?branch=master) + + + +## Installation + +This module is installed via npm: + +``` sh +npm install mos-plugin-shields --save +``` + + +## Usage + +Add this code snippet to your `README.md` + +```md + + +``` + +Run `mos` in the terminal. + +You'll get the travis and david shields in your readme via [shield.io](http://shields.io/). + +## API + +- `shields(...shields)` +- `shields.flat(...shields)` +- `shields.flatSquare(...shields)` +- `shields.plastic(...shields)` + +The currently supported shields are: travis, coveralls, npm, deps, devDeps, peerDeps, gemnasium, climate. + +**NOTE:** The shields will be created for the master branch. + + +## License + +[MIT](./LICENSE) © [Zoltan Kochan](http://kochan.io) + + +* * * + + +## Dependencies [![Dependency status for master](https://img.shields.io/david/zkochan/mos-plugin-shields/master.svg?style=flat-square)](https://david-dm.org/zkochan/mos-plugin-shields/master) + +- [shields](https://github.com/kenany/shields): Generate shields for your current project's README + + + + +## Dev Dependencies [![devDependency status for master](https://img.shields.io/david/dev/zkochan/mos-plugin-shields/master.svg?style=flat-square)](https://david-dm.org/zkochan/mos-plugin-shields/master#info=devDependencies) + +- [chai](https://github.com/chaijs/chai): BDD/TDD assertion library for node.js and the browser. Test framework agnostic. +- [cz-conventional-changelog](https://github.com/commitizen/cz-conventional-changelog): Commitizen adapter following the conventional-changelog format. +- [eslint](https://github.com/eslint/eslint): An AST-based pattern checker for JavaScript. +- [eslint-config-standard](https://github.com/feross/eslint-config-standard): JavaScript Standard Style - ESLint Shareable Config +- [eslint-plugin-promise](https://github.com/xjamundx/eslint-plugin-promise): Enforce best practices for JavaScript promises +- [eslint-plugin-standard](https://github.com/xjamundx/eslint-plugin-standard): ESlint Plugin for the Standard Linter +- [ghooks](https://github.com/gtramontina/ghooks): Simple git hooks +- [istanbul](https://github.com/gotwarlost/istanbul): Yet another JS code coverage tool that computes statement, line, function and branch coverage with module loader hooks to transparently add coverage when running tests. Supports all JS coverage use cases including unit tests, server side functional tests +- [mocha](https://github.com/mochajs/mocha): simple, flexible, fun test framework +- [mos](https://github.com/zkochan/mos): A pluggable module that injects content into your markdown files via hidden JavaScript snippets +- [semantic-release](https://github.com/semantic-release/semantic-release): automated semver compliant package publishing +- [validate-commit-msg](https://github.com/kentcdodds/validate-commit-msg): Script to validate a commit message follows the conventional changelog standard + + diff --git a/index.js b/index.js new file mode 100644 index 0000000..3e3d903 --- /dev/null +++ b/index.js @@ -0,0 +1,17 @@ +'use strict' +module.exports = plugin + +const createShieldsRenderer = require('./lib/create-shields-renderer') + +function plugin (markdown) { + if (!markdown.repo || markdown.repo.host !== 'github.com') { + throw new Error('The shields plugin only works for github repos') + } + + return { + shields: createShieldsRenderer({ + github: markdown.repo, + pkg: markdown.pkg, + }), + } +} diff --git a/index.spec.js b/index.spec.js new file mode 100644 index 0000000..018e716 --- /dev/null +++ b/index.spec.js @@ -0,0 +1,13 @@ +'use strict' +const describe = require('mocha').describe +const it = require('mocha').it +const expect = require('chai').expect + +const mosPluginShields = require('.') + +describe('mosPluginShields', () => { + it('should throw error when package hosted not on GitHub', () => { + expect(() => mosPluginShields({ repo: { host: 'gitlab' } })) + .to.throw(Error, 'The shields plugin only works for github repos') + }) +}) diff --git a/lib/create-shields-renderer.js b/lib/create-shields-renderer.js new file mode 100644 index 0000000..165b57f --- /dev/null +++ b/lib/create-shields-renderer.js @@ -0,0 +1,42 @@ +'use strict' +const shielder = require('shields') + +const slice = Array.prototype.slice + +module.exports = opts => { + const github = opts.github + const pkg = opts.pkg + + const shields = styledShield('flat') + shields.flat = styledShield('flat') + shields.flatSquare = styledShield('flat-square') + shields.plastic = styledShield('plastic') + + return shields + + function styledShield (style) { + const getShieldProps = shielder({ style }) + const shieldOpts = { + repo: `${github.user}/${github.repo}`, + npmName: pkg.name, + branch: 'master', + } + return function () { + const shields = slice.call(arguments) + return shields + .map(shieldName => { + const shieldProps = getShieldProps(shieldName, shieldOpts) + if (!shieldProps) { + throw new Error('`' + shieldName + '` shield is not supported') + } + return shieldProps + }) + .map(renderShield) + .join('\n') + } + } +} + +function renderShield (shieldProps) { + return `[![${shieldProps.text}](${shieldProps.image})](${shieldProps.link})` +} diff --git a/lib/create-shields-renderer.spec.js b/lib/create-shields-renderer.spec.js new file mode 100644 index 0000000..42a4843 --- /dev/null +++ b/lib/create-shields-renderer.spec.js @@ -0,0 +1,89 @@ +'use strict' +const describe = require('mocha').describe +const it = require('mocha').it +const expect = require('chai').expect +const createShieldsRenderer = require('./create-shields-renderer') + +describe('createShieldsRenderer', () => { + it('should create flat shield by default', () => { + const shields = createShieldsRenderer({ + github: { + user: 'zkochan', + repo: 'mos', + }, + pkg: { + name: 'mos', + }, + }) + expect(shields('travis')).to.eq('[![Build status for master](https://img.shields.io/travis/zkochan/mos/master.svg?style=flat)](https://travis-ci.org/zkochan/mos)') + }) + + it('should create flat travis shield', () => { + const shields = createShieldsRenderer({ + github: { + user: 'zkochan', + repo: 'mos', + }, + pkg: { + name: 'mos', + }, + }) + expect(shields.flat('travis')).to.eq('[![Build status for master](https://img.shields.io/travis/zkochan/mos/master.svg?style=flat)](https://travis-ci.org/zkochan/mos)') + }) + + it('should create flat square travis shield', () => { + const shields = createShieldsRenderer({ + github: { + user: 'zkochan', + repo: 'mos', + }, + pkg: { + name: 'mos', + }, + }) + expect(shields.flatSquare('travis')).to.eq('[![Build status for master](https://img.shields.io/travis/zkochan/mos/master.svg?style=flat-square)](https://travis-ci.org/zkochan/mos)') + }) + + it('should create plastic travis shield', () => { + const shields = createShieldsRenderer({ + github: { + user: 'zkochan', + repo: 'mos', + }, + pkg: { + name: 'mos', + }, + }) + expect(shields.plastic('travis')).to.eq('[![Build status for master](https://img.shields.io/travis/zkochan/mos/master.svg?style=plastic)](https://travis-ci.org/zkochan/mos)') + }) + + it('should create several shields', () => { + const shields = createShieldsRenderer({ + github: { + user: 'zkochan', + repo: 'mos', + }, + pkg: { + name: 'mos', + }, + }) + expect(shields.plastic('travis', 'npm')).to.eq([ + '[![Build status for master](https://img.shields.io/travis/zkochan/mos/master.svg?style=plastic)](https://travis-ci.org/zkochan/mos)', + '[![NPM version](https://img.shields.io/npm/v/mos.svg?style=plastic)](https://www.npmjs.com/package/mos)', + ].join('\n')) + }) + + it('should throw exception if shield not supported', () => { + const shields = createShieldsRenderer({ + github: { + user: 'zkochan', + repo: 'mos', + }, + pkg: { + name: 'mos', + }, + }) + expect(() => shields('no-such-shield')) + .to.throw(Error, '`no-such-shield` shield is not supported') + }) +}) diff --git a/package.json b/package.json new file mode 100644 index 0000000..2c1e8d8 --- /dev/null +++ b/package.json @@ -0,0 +1,70 @@ +{ + "name": "mos-plugin-shields", + "version": "0.0.0-placeholder", + "description": "A mos plugin for creating markdown shields", + "files": [ + "index.js", + "lib" + ], + "main": "index.js", + "scripts": { + "test": "mocha && npm run lint && mos test", + "lint": "eslint lib/**/*.js index.js", + "commit": "git-cz", + "coverage": "istanbul cover -x \"**/*.spec.js\" node_modules/mocha/bin/_mocha -- -R spec", + "precoveralls": "istanbul cover -x \"**/*.spec.js\" node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && npm i coveralls@2", + "coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js", + "postcoveralls": "rm -rf ./coverage", + "md": "mos", + "semantic-release": "semantic-release pre && npm publish && semantic-release post" + }, + "repository": { + "type": "git", + "url": "https://github.com/zkochan/mos-plugin-shields" + }, + "keywords": [ + "mos-plugin", + "markdown", + "shields", + "badges" + ], + "author": { + "name": "Zoltan Kochan", + "email": "zoltan.kochan@gmail.com", + "url": "http://kochan.io" + }, + "license": "MIT", + "bugs": { + "url": "https://github.com/zkochan/mos-plugin-shields/issues" + }, + "homepage": "https://github.com/zkochan/mos-plugin-shields#readme", + "dependencies": { + "shields": "^2.1.0" + }, + "devDependencies": { + "chai": "^3.4.1", + "cz-conventional-changelog": "1.1.5", + "eslint": "^2.8.0", + "eslint-config-standard": "^5.2.0", + "eslint-plugin-promise": "^1.1.0", + "eslint-plugin-standard": "^1.3.2", + "ghooks": "^1.2.1", + "istanbul": "^0.4.2", + "mocha": "^2.3.4", + "mos": "^0.11.1", + "semantic-release": "^4.3.5", + "validate-commit-msg": "^2.6.1" + }, + "engines": { + "node": ">=4" + }, + "config": { + "commitizen": { + "path": "./node_modules/cz-conventional-changelog" + }, + "ghooks": { + "commit-msg": "node ./node_modules/validate-commit-msg/index.js", + "pre-commit": "npm test" + } + } +} diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..b4cc660 --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1 @@ +{lib/**/*.spec.js,index.spec.js}