Throw a more user-friendly exception #107

Merged
merged 4 commits into from Nov 29, 2016

Projects

None yet

3 participants

@gillesdemey
Contributor

Additionally reads the package.json file once and passes it onto ui.js and index.js

Fixes #106

@SamVerschueren

Thanks for the PR @gillesdemey, much appreciated! Added some comments.

cli.js
@@ -28,6 +29,12 @@ const cli = meow(`
updateNotifier({pkg: cli.pkg}).notify();
+const pkg = readPkgUp.sync().pkg;
@SamVerschueren
SamVerschueren Nov 28, 2016 Collaborator

Actually, cli.pkg already has the package data. It uses read-pkg-up as well.

@gillesdemey
gillesdemey Nov 28, 2016 Contributor

When I inspected the contents of cli.pkg it seemed to contain the package info for np, not the package you were trying to publish. I'll double-check that though :)

@SamVerschueren
SamVerschueren Nov 28, 2016 Collaborator

Oh yes, you're right. It off course needs the np information for update-notifier. Seems I need some more coffee :).

cli.js
@@ -28,6 +29,12 @@ const cli = meow(`
updateNotifier({pkg: cli.pkg}).notify();
+const pkg = readPkgUp.sync().pkg;
+if (!pkg) {
+ console.error('\nNo package.json found, are you in the correct project?');
@SamVerschueren
SamVerschueren Nov 28, 2016 Collaborator

Why the newline? Just wondering. Maybe add [log-symbols](https://github.com/sindresorhus/log-symbols).error in front of the error message. I'm also not sure about the message itself, can't come up with something better though :).

@gillesdemey
gillesdemey Nov 28, 2016 Contributor

I'm just going with how other errors or exceptions are printed, ie. https://github.com/sindresorhus/np/blob/master/cli.js#L55

@SamVerschueren
SamVerschueren Nov 28, 2016 Collaborator

That's because we don't want the error/message being printed directly after the execution list.

@sindresorhus
sindresorhus Nov 29, 2016 Owner

Drop the newline.

Message:

${logSymbols.error} No package.json found. Make sure you're in the correct project.

cli.js
@@ -38,7 +45,7 @@ Promise
});
}
- return ui(cli.flags);
+ return ui(cli.flags, pkg);
@SamVerschueren
SamVerschueren Nov 28, 2016 Collaborator

I think I'd rather have it added to the options object Object.assign(cli.flags, {pkg}).

But wait before doing that untill @sindresorhus replies. Most of the time, he tends to have a strong opinion on parameters :).

cli.js
@@ -47,7 +54,7 @@ Promise
return options;
})
- .then(options => np(options.version, options))
+ .then(options => np(options.version, options, pkg))
@SamVerschueren
SamVerschueren Nov 28, 2016 Collaborator

Same as above

@sindresorhus
Owner

I don't really see much benefit in passing the pkg around. Getting it isn't very expensive, so it makes no practical difference, but complicates the code. I'd prefer to just leave it as is and just add the missing package.json warning.

@gillesdemey
Contributor

I've addressed your concerns, PTAL when you have some time

index.js
@@ -27,6 +28,9 @@ module.exports = (input, opts) => {
const runTests = !opts.yolo;
const runCleanup = !opts.skipCleanup && !opts.yolo;
const pkg = readPkgUp.sync().pkg;
+ if (!pkg) {
+ throw new Error(`${logSymbols.error} No package.json found. Make sure you're in the correct project.`);
+ }
@SamVerschueren
SamVerschueren Nov 29, 2016 Collaborator

I think it would be cleaner if we extracted this in something like lib/util.js

const pkg = util.readPkg();

@sindresorhus Any reason why we didn't added the error symbol yet in cli.js?

@sindresorhus
sindresorhus Nov 29, 2016 Owner

Any reason why we didn't added the error symbol yet in cli.js?

No reason. Can be done.

@SamVerschueren
SamVerschueren Nov 29, 2016 Collaborator

Oh, and also add a newline above the if statement :).

@@ -0,0 +1,11 @@
+const readPkgUp = require('read-pkg-up');
@sindresorhus
sindresorhus Nov 29, 2016 Owner

Should have a 'use strict'; at the top.

@gillesdemey
gillesdemey Nov 29, 2016 Contributor

Oops sorry, was expecting xo to catch these :)

@gillesdemey
Contributor

I've made the following changes:

  • Created a util file that exposes readPkg and throws an exception when it's not found.
  • Added logSymbols to cli.js that shows the error symbol on any execution exception.

Sadly, it still shows the newline when the package.json isn't found.

@SamVerschueren

The last one :)

@@ -0,0 +1,11 @@
+const readPkgUp = require('read-pkg-up');
@SamVerschueren
SamVerschueren Nov 29, 2016 Collaborator

'use strict';

@gillesdemey
Contributor

Done 👍

@sindresorhus sindresorhus merged commit 0787799 into sindresorhus:master Nov 29, 2016

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@sindresorhus
Owner

Very good. Thanks @gillesdemey :)

@SamVerschueren
Collaborator

Thanks @gillesdemey 🍻 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment