From c70711253d8c92e5940b2fc07eaeae0fca81deb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CAndrew?= Date: Mon, 26 Mar 2018 20:26:38 +0300 Subject: [PATCH 1/4] homepage ranyitz/qnm#18 --- src/actions/homepage.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/actions/homepage.js diff --git a/src/actions/homepage.js b/src/actions/homepage.js new file mode 100644 index 0000000..cb9b4bb --- /dev/null +++ b/src/actions/homepage.js @@ -0,0 +1,16 @@ +const isEmpty = require('lodash/isEmpty'); +const getSuggestions = require('../suggest/get-suggestions'); +const NotFoundModuleError = require('../errors/not-found-module-error'); + +module.exports = (workspace, name) => { + const moduleOccurrences = workspace.getModuleOccurrences(name); + const modulesNames = workspace.getModulesNames(); + + if (isEmpty(moduleOccurrences)) { + const suggestions = getSuggestions(name, modulesNames); + + throw new NotFoundModuleError(name, suggestions); + } + + return moduleOccurrences[0].packageJson.homepage; +}; From 1bacab7bf59f45834a53aa29284db85a64ecf7da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CAndrew?= Date: Mon, 26 Mar 2018 20:41:19 +0300 Subject: [PATCH 2/4] homepage not found error ranyitz/qnm#18 --- src/actions/homepage.js | 8 +++++++- src/errors/not-found-homepage-error.js | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 src/errors/not-found-homepage-error.js diff --git a/src/actions/homepage.js b/src/actions/homepage.js index cb9b4bb..f6c3435 100644 --- a/src/actions/homepage.js +++ b/src/actions/homepage.js @@ -1,6 +1,7 @@ const isEmpty = require('lodash/isEmpty'); const getSuggestions = require('../suggest/get-suggestions'); const NotFoundModuleError = require('../errors/not-found-module-error'); +const NotFoundHomepageError = require('../errors/not-found-homepage-error'); module.exports = (workspace, name) => { const moduleOccurrences = workspace.getModuleOccurrences(name); @@ -11,6 +12,11 @@ module.exports = (workspace, name) => { throw new NotFoundModuleError(name, suggestions); } + const homepageLink = moduleOccurrences[0].packageJson.homepage; - return moduleOccurrences[0].packageJson.homepage; + if (!homepageLink) { + throw new NotFoundHomepageError(name); + } + + return homepageLink; }; 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); + } +}; From cc41620d4ea5549ad09ba8089b3189b006096441 Mon Sep 17 00:00:00 2001 From: vsashyn Date: Tue, 22 May 2018 21:10:01 +0300 Subject: [PATCH 3/4] Add homepage option. Open browser with package homepage value. Add test if the is no homepage. --- .../actions/__snapshots__/get.spec.js.snap | 2 ++ __tests__/actions/get.spec.js | 10 +++++++++ src/actions/get.js | 14 +++++++++++- src/actions/homepage.js | 22 ------------------- src/cli.js | 3 ++- 5 files changed, 27 insertions(+), 24 deletions(-) delete mode 100644 src/actions/homepage.js 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/actions/homepage.js b/src/actions/homepage.js deleted file mode 100644 index f6c3435..0000000 --- a/src/actions/homepage.js +++ /dev/null @@ -1,22 +0,0 @@ -const isEmpty = require('lodash/isEmpty'); -const getSuggestions = require('../suggest/get-suggestions'); -const NotFoundModuleError = require('../errors/not-found-module-error'); -const NotFoundHomepageError = require('../errors/not-found-homepage-error'); - -module.exports = (workspace, name) => { - const moduleOccurrences = workspace.getModuleOccurrences(name); - const modulesNames = workspace.getModulesNames(); - - if (isEmpty(moduleOccurrences)) { - const suggestions = getSuggestions(name, modulesNames); - - throw new NotFoundModuleError(name, suggestions); - } - const homepageLink = moduleOccurrences[0].packageJson.homepage; - - if (!homepageLink) { - throw new NotFoundHomepageError(name); - } - - return homepageLink; -}; 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') From 62f67d68ec05c53cb93b71b812e39fa04ec00536 Mon Sep 17 00:00:00 2001 From: vsashyn Date: Tue, 22 May 2018 21:12:44 +0300 Subject: [PATCH 4/4] Update README for homepage option. --- README.md | 3 +++ 1 file changed, 3 insertions(+) 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