Skip to content

Commit

Permalink
Add homepage option. Open browser with package homepage value.
Browse files Browse the repository at this point in the history
Add test if the is no homepage.
  • Loading branch information
vsashyn authored and ranyitz committed May 29, 2018
1 parent 1bacab7 commit cc41620
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
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);
};
22 changes: 0 additions & 22 deletions src/actions/homepage.js

This file was deleted.

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

0 comments on commit cc41620

Please sign in to comment.