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 all commits
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
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const parentDir = path.dirname(module.parent.filename);
class Conf {
constructor(opts) {
const pkgPath = pkgUp.sync(parentDir);
opts = Object.assign({projectName: require(pkgPath).name}, opts);

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

if (!opts.projectName && !opts.cwd) {
throw new Error('Project name could not be inferred. Please specify the `projectName` option.');
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 @@ -161,3 +162,15 @@ test('`cwd` option overrides `projectName` option', t => {
t.is(conf.get('foo'), fixture);
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;
});