Skip to content

Commit

Permalink
#18 Feature: add homepage option (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsashyn authored and ranyitz committed May 29, 2018
1 parent 0a95770 commit 981e910
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ eslint-plugin-mocha
eslint-plugin-react
└── 6.10.3
```
### homepage

Opens package "homepage" property in your browser.

## Contributing

Expand Down
2 changes: 2 additions & 0 deletions __tests__/actions/__snapshots__/get.spec.js.snap
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 10 additions & 0 deletions __tests__/actions/get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
14 changes: 13 additions & 1 deletion src/actions/get.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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);
};
3 changes: 2 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
7 changes: 7 additions & 0 deletions src/errors/not-found-homepage-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = class NotFoundHomepageleError extends Error {
constructor(name) {
const message = `Could not find homepage link for module "${name}".`;

super(message);
}
};

0 comments on commit 981e910

Please sign in to comment.