From dfe848f166030852833392d3116fad2b16e4dd50 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Thu, 10 Dec 2015 23:07:45 -0500 Subject: [PATCH] feat(readme): manpm can show and search README.md in the current folder, closes #20 --- README.md | 8 +++++++- src/get-readme.js | 26 ++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e10d9e1..febb705 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ for both variants: words or unicode symbols npm install -g manpm -## Show entire package or github readme +## Show the entire README from a package or github repo You can give NPM package name (like `manpm`), GitHub user / repo pair (like `bahmutov/manpm`) or full GitHub url (like `https://github.com/bahmutov/object-fitter` or `git@github.com:bahmutov/object-fitter.git`). @@ -64,6 +64,12 @@ The following search features are implemented I am still looking for a library capable of fuzzy text search. Maybe [lunr](https://github.com/olivernn/lunr.js)? +## Show and search local README + +Sometimes you just want to find a section in the local README file, right in the current directory. + + manpm . [optional search text] + ## Example: showing ES6 docs There is a great GitHub repo [ES6 Overview in 350 Bullet Points](https://github.com/bevacqua/es6) diff --git a/src/get-readme.js b/src/get-readme.js index 6772eea..f7c4bfc 100644 --- a/src/get-readme.js +++ b/src/get-readme.js @@ -86,19 +86,37 @@ function getReadmeFromGithub(name) { .then(toString); } +function getLocalReadmeFile() { + var fs = require('fs'); + var join = require('path').join; + var filename = join(process.cwd(), 'README.md'); + return new Promise(function (resolve, reject) { + if (!fs.existsSync(filename)) { + return reject(new Error('Cannot find local file ' + filename)); + } + return resolve(fs.readFileSync(filename, 'utf8')); + }); +} + function getReadme(name) { la(check.unemptyString(name), 'missing name'); + if (name === '.') { + log('fetching README in the current working folder'); + return getLocalReadmeFile(); + } + if (utils.maybeGithubRepoName(name)) { log('fetching README for github repo', name); return getReadmeFromGithub(name); - } else if (utils.maybeGithubRepoUrl(name)) { + } + if (utils.maybeGithubRepoUrl(name)) { log('fetching README for github url', name); return getReadmeFromGithub(name); - } else { - log('fetching README for package', name); - return getReadmeFile(name); } + + log('fetching README for package', name); + return getReadmeFile(name); } module.exports = getReadme;