Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaks if no package.json is present #6

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ const parentDir = path.dirname(module.parent.filename);
class Conf {
constructor(opts) {
const pkgPath = pkgUp.sync(parentDir);
opts = Object.assign({projectName: require(pkgPath).name}, opts);

if (!opts.projectName) {
// If the package.json was not found, avoid breaking with `require(null)`.
if (pkgPath) {
opts = Object.assign({projectName: require(pkgPath).name}, opts);
}

if (!opts || !opts.projectName) {
throw new Error('Project name could not be inferred. Please specify the `projectName` option.');
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would rather do:

const pkgPath = pkgUp.sync(parentDir);

opts = Object.assign({
    // if the package.json was not found, avoid breaking with `require(null)`.
    projectName: pkgPath && require(pkgPath).name
}, opts);

if (!opts.projectName) {
    throw new Error('Project name could not be inferred. Please specify the `projectName` option.');
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tons of ways to handle this null value. I will update it to match yours, nice and clean.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, totally subjective. Both work equally fine :)


Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';
import {serial as test} from 'ava';
import tempfile from 'tempfile';
import del from 'del';
import pkgUp from 'pkg-up';
import Conf from './';

const fixture = '🦄';
Expand Down Expand Up @@ -146,3 +147,15 @@ test('automatic `projectName` inference', t => {
t.true(conf.path.includes('conf'));
del.sync(conf.path, {force: true});
});

test('safely handle missing `project.json`', t => {
const pkgUpSyncOrig = pkgUp.sync;
pkgUp.sync = () => null;

let conf;
t.notThrows(() => {
conf = new Conf({projectName: 'conf-fixture-project-name'});
});
del.sync(conf.path, {force: true});
pkgUp.sync = pkgUpSyncOrig;
});