├── .npmignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── basic.js /.npmignore: -------------------------------------------------------------------------------- 1 | .travis.yml 2 | test/ 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - lts/* 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Feross Aboukhadijeh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # get-package-readme [![travis][travis-image]][travis-url] [![npm][npm-image]][npm-url] [![downloads][downloads-image]][downloads-url] [![javascript style guide][standard-image]][standard-url] 2 | 3 | [travis-image]: https://img.shields.io/travis/feross/get-package-readme/master.svg 4 | [travis-url]: https://travis-ci.org/feross/get-package-readme 5 | [npm-image]: https://img.shields.io/npm/v/get-package-readme.svg 6 | [npm-url]: https://npmjs.org/package/get-package-readme 7 | [downloads-image]: https://img.shields.io/npm/dm/get-package-readme.svg 8 | [downloads-url]: https://npmjs.org/package/get-package-readme 9 | [standard-image]: https://img.shields.io/badge/code_style-standard-brightgreen.svg 10 | [standard-url]: https://standardjs.com 11 | 12 | ### Get the GitHub readme for an npm package 13 | 14 | ## install 15 | 16 | ``` 17 | npm install get-package-readme 18 | ``` 19 | 20 | ## usage 21 | 22 | ```js 23 | var getPackageReadme = require('get-package-readme') 24 | 25 | getPackageReadme('webtorrent', function (err, readme) { 26 | if (err) throw err 27 | console.log(readme) 28 | }) 29 | ``` 30 | 31 | ## why this instead of [`readme-getter`](https://www.npmjs.com/package/readme-getter)? 32 | 33 | `get-package-readme` (this package) gets an npm packages' GitHub readme (from the `master` 34 | branch) and falls back to the npm readme if it exists. If you want just the npm readme, you should use `readme-getter`. 35 | 36 | ## license 37 | 38 | MIT. Copyright (c) [Feross Aboukhadijeh](http://feross.org). 39 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! get-package-readme. MIT License. Feross Aboukhadijeh */ 2 | module.exports = getPackageReadme 3 | 4 | var get = require('simple-get') 5 | var gh = require('github-url-to-object') 6 | 7 | var NPM_REGISTRY = 'https://registry.npmjs.org/%s' 8 | var GITHUB_README = 'https://raw.githubusercontent.com/%s' 9 | 10 | function getPackageReadme (pkgName, cb) { 11 | var npmUrl = NPM_REGISTRY.replace('%s', pkgName) 12 | get.concat(npmUrl, function (err, res, data) { 13 | if (err) return cb(err) 14 | try { 15 | data = JSON.parse(data.toString()) 16 | } catch (err) { 17 | return cb(new Error(pkgName + ': cannot parse registry data: ' + err.message)) 18 | } 19 | 20 | var checkFallback = function (err) { 21 | if (data.readme) { 22 | return cb(null, data.readme) 23 | } 24 | return cb(err) 25 | } 26 | 27 | var readmeFilename = data.readmeFilename 28 | if (!readmeFilename) return checkFallback(new Error(pkgName + ': package.json has no readmeFilename')) 29 | 30 | var repoUrl = data.repository && data.repository.url 31 | if (!repoUrl) return checkFallback(new Error(pkgName + ': package.json has no repository')) 32 | 33 | var repoObj = gh(repoUrl) 34 | var user = repoObj && repoObj.user 35 | var repo = repoObj && repoObj.repo 36 | if (!user || !repo) return checkFallback(new Error(pkgName + ': cannot parse repository url')) 37 | 38 | var readmePath = user + '/' + repo + '/master/' + readmeFilename 39 | var githubUrl = GITHUB_README.replace('%s', readmePath) 40 | 41 | get.concat(githubUrl, function (err, res, data) { 42 | if (err) return checkFallback(err) 43 | cb(null, data.toString()) 44 | }) 45 | }) 46 | } 47 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "get-package-readme", 3 | "description": "Get the GitHub readme for an npm package", 4 | "version": "1.4.0", 5 | "author": { 6 | "name": "Feross Aboukhadijeh", 7 | "email": "feross@feross.org", 8 | "url": "https://feross.org" 9 | }, 10 | "bugs": { 11 | "url": "https://github.com/feross/get-package-readme/issues" 12 | }, 13 | "dependencies": { 14 | "github-url-to-object": "^4.0.4", 15 | "simple-get": "^4.0.0" 16 | }, 17 | "devDependencies": { 18 | "standard": "*", 19 | "tape": "^5.0.1" 20 | }, 21 | "keywords": [ 22 | "npm", 23 | "package", 24 | "readme", 25 | "github", 26 | "package.json", 27 | "fetch readme", 28 | "get readme", 29 | "github readme", 30 | "get package readme", 31 | "module readme", 32 | "module", 33 | "get module readme" 34 | ], 35 | "license": "MIT", 36 | "main": "index.js", 37 | "repository": { 38 | "type": "git", 39 | "url": "git://github.com/feross/get-package-readme.git" 40 | }, 41 | "scripts": { 42 | "test": "standard && tape test/*.js" 43 | }, 44 | "funding": [ 45 | { 46 | "type": "github", 47 | "url": "https://github.com/sponsors/feross" 48 | }, 49 | { 50 | "type": "patreon", 51 | "url": "https://www.patreon.com/feross" 52 | }, 53 | { 54 | "type": "consulting", 55 | "url": "https://feross.org/support" 56 | } 57 | ] 58 | } 59 | -------------------------------------------------------------------------------- /test/basic.js: -------------------------------------------------------------------------------- 1 | var getPackageReadme = require('../') 2 | var test = require('tape') 3 | 4 | test('get package readme for "webtorrent"', function (t) { 5 | t.plan(4) 6 | getPackageReadme('webtorrent', function (err, readme) { 7 | t.error(err) 8 | t.ok(/webtorrent/i.test(readme)) 9 | t.ok(/torrent/i.test(readme)) 10 | t.ok(/features/i.test(readme)) 11 | }) 12 | }) 13 | 14 | test('get package readme for "browserify"', function (t) { 15 | t.plan(3) 16 | getPackageReadme('browserify', function (err, readme) { 17 | t.error(err) 18 | t.ok(/browserify/i.test(readme)) 19 | t.ok(/modules/i.test(readme)) 20 | }) 21 | }) 22 | 23 | test('get error for invalid package name', function (t) { 24 | t.plan(1) 25 | getPackageReadme('invalid-package-name-92342384', function (err, readme) { 26 | t.ok(err instanceof Error) 27 | }) 28 | }) 29 | --------------------------------------------------------------------------------