From 6ae3f92e89d06b8ef8b4a9e6604a4dbb0f3ee25f Mon Sep 17 00:00:00 2001 From: Jim Buck Date: Mon, 11 Jul 2016 17:32:25 -0400 Subject: [PATCH 1/2] Properly handled null package path. --- index.js | 8 ++++++-- test.js | 13 +++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f792b91..0d1a93c 100644 --- a/index.js +++ b/index.js @@ -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.'); } diff --git a/test.js b/test.js index d96d027..d7c6e13 100644 --- a/test.js +++ b/test.js @@ -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 = '🦄'; @@ -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; +}); From ffe1c8f0e1193d20f90a7225a485b4dd80a35994 Mon Sep 17 00:00:00 2001 From: Jim Buck Date: Mon, 11 Jul 2016 20:35:42 -0400 Subject: [PATCH 2/2] Fixed formatting issue. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 6b304c0..1bcaf78 100644 --- a/index.js +++ b/index.js @@ -97,4 +97,4 @@ class Conf { } } -module.exports = Conf; \ No newline at end of file +module.exports = Conf;