diff --git a/README.md b/README.md index faa9cce..68c31df 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,9 @@ eslint-plugin-mocha eslint-plugin-react └── 6.10.3 ``` +### homepage + +Opens package "homepage" property in your browser. ## Contributing diff --git a/__tests__/actions/__snapshots__/get.spec.js.snap b/__tests__/actions/__snapshots__/get.spec.js.snap index 1a4a776..6845498 100644 --- a/__tests__/actions/__snapshots__/get.spec.js.snap +++ b/__tests__/actions/__snapshots__/get.spec.js.snap @@ -1,5 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`get --homepage should throw NotFoundHomepageError if there is no "homepage" in package.json 1`] = `"Could not find homepage link for module \\"test\\"."`; + exports[`get should disable colors 1`] = ` "test └─┬ another diff --git a/__tests__/actions/get.spec.js b/__tests__/actions/get.spec.js index a4d9c31..39b9dd7 100644 --- a/__tests__/actions/get.spec.js +++ b/__tests__/actions/get.spec.js @@ -72,6 +72,16 @@ describe('get', () => { expect(output).toMatchSnapshot(); }); + it('--homepage should throw NotFoundHomepageError if there is no "homepage" in package.json', () => { + const workspace = resolveWorkspace('single-module'); + + try { + getAction(workspace, 'test', { homepage: true }); + } catch (e) { + expect(e.message).toMatchSnapshot(); + } + }); + describe('--open', () => { it('should open module file directory when --open flag used', () => { const workspace = resolveWorkspace('three-levels-deep'); diff --git a/src/actions/get.js b/src/actions/get.js index 07673ff..6515147 100644 --- a/src/actions/get.js +++ b/src/actions/get.js @@ -1,12 +1,14 @@ const isEmpty = require('lodash/isEmpty'); +const opn = require('opn'); const getSuggestions = require('../suggest/get-suggestions'); const NotFoundModuleError = require('../errors/not-found-module-error'); +const NotFoundHomepageError = require('../errors/not-found-homepage-error'); const renderModuleOccurrences = require('../render/render-module-occurrences'); const openPackage = require('./helpers/open'); module.exports = (workspace, name, options = {}) => { const moduleOccurrences = workspace.getModuleOccurrences(name); - const { open } = options; + const { open, homepage } = options; if (isEmpty(moduleOccurrences)) { const modulesNames = workspace.getModulesNames(); @@ -19,5 +21,15 @@ module.exports = (workspace, name, options = {}) => { return openPackage(moduleOccurrences, workspace.root); } + if (homepage) { + // take only the first option + const [nodeModule] = workspace.getModuleOccurrences(name); + const homepageUrl = nodeModule.packageJson.homepage; + + if (!homepageUrl) throw new NotFoundHomepageError(name); + + return opn(homepageUrl, { wait: false }); + } + return renderModuleOccurrences(moduleOccurrences, options); }; diff --git a/src/cli.js b/src/cli.js index b7452aa..e04974b 100644 --- a/src/cli.js +++ b/src/cli.js @@ -21,7 +21,8 @@ try { ) .option('-d, --debug', 'see full error messages, mostly for debugging') .option('-o, --open', 'open editor at the module directory') - .option('--disable-colors', 'minimize color and styling usage in output'); + .option('--disable-colors', 'minimize color and styling usage in output') + .option('-h, --homepage', 'open the homepage of a certain module'); program .command('list') diff --git a/src/errors/not-found-homepage-error.js b/src/errors/not-found-homepage-error.js new file mode 100644 index 0000000..f5601b3 --- /dev/null +++ b/src/errors/not-found-homepage-error.js @@ -0,0 +1,7 @@ +module.exports = class NotFoundHomepageleError extends Error { + constructor(name) { + const message = `Could not find homepage link for module "${name}".`; + + super(message); + } +};